結果

問題 No.43 野球の試合
ユーザー scachescache
提出日時 2014-12-01 15:35:24
言語 Java21
(openjdk 21)
結果
AC  
実行時間 188 ms / 5,000 ms
コード長 2,069 bytes
コンパイル時間 3,553 ms
コンパイル使用メモリ 75,104 KB
実行使用メモリ 60,168 KB
最終ジャッジ日時 2023-09-02 00:58:46
合計ジャッジ時間 5,854 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
56,308 KB
testcase_01 AC 122 ms
56,188 KB
testcase_02 AC 123 ms
55,908 KB
testcase_03 AC 121 ms
56,060 KB
testcase_04 AC 122 ms
56,116 KB
testcase_05 AC 122 ms
56,012 KB
testcase_06 AC 126 ms
55,880 KB
testcase_07 AC 188 ms
60,168 KB
testcase_08 AC 124 ms
56,684 KB
testcase_09 AC 125 ms
56,052 KB
testcase_10 AC 121 ms
56,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main43 {
	public static void main(String[] args) {
		Main43 p = new Main43();
	}

	public Main43() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] isWon = new int[n][n];
		for(int i=0;i<isWon.length;i++){
			String s = sc.next();
			for(int j=0;j<isWon[i].length;j++){
				if(s.charAt(j)=='o')
					isWon[i][j] = 1;
				else if(s.charAt(j)=='x')
					isWon[i][j] = -1;
				else if(s.charAt(j) == '#')
					isWon[i][j] = -1;

			}
		}
		solve(isWon);
	}

	public void solve(int[][] isWon) {
//		for(int i=1;i<isWon[0].length;i++){
//			if(isWon[0][i] == 0){
//				isWon[0][i] = 1;
//				isWon[i][0] = -1;
//			}
//		}
		
		System.out.println(rec(isWon, 0, 0));
		
	}
	
	private int rec(int[][] isWon, int cur1, int cur2){
//		System.out.println(cur1 + " " + cur2 );
		if(cur1==isWon.length)
			return calcRank(isWon);
		
		int res = Integer.MAX_VALUE;
		int ncur1 = 0;
		int ncur2 = 0;
		if(cur2+1 < isWon.length){
			ncur1 = cur1;
			ncur2 = cur2+1;
		}else{
			ncur1 = cur1+1;
			ncur2 = ncur1;
		}
		
		if(isWon[cur1][cur2] == 0){
			isWon[cur1][cur2] = 1;
			isWon[cur2][cur1] = -1;
			res = Math.min(res, rec(isWon, ncur1, ncur2));
			
			isWon[cur1][cur2] = -1;
			isWon[cur2][cur1] = 1;
			res = Math.min(res, rec(isWon, ncur1, ncur2));
			
			isWon[cur1][cur2] = 0;
			isWon[cur2][cur1] = 0;
		}else{
			res = Math.min(res, rec(isWon, ncur1, ncur2));
		}
					
		return res;
	}
	
	private int calcRank(int[][] isWon){
		int[] w = new int[isWon.length];
		for(int i=0;i<isWon.length;i++){
			for(int j=0;j<isWon[i].length;j++){
				if(isWon[i][j]==1)
					w[i]++;
			}
		}
		
		int playerWin = w[0];
		Arrays.sort(w);
		int last = w[w.length-1];
		int rank = 1;
		int res = -1;
		for(int i=w.length-1;i>=0;i--){
			if(last != w[i]){
				rank++;
				last = w[i];
			}
			
			if(w[i] == playerWin){
				res = rank;
				break;
			}			
		}
		
		return res;
		
	}
	

}
0