結果

問題 No.43 野球の試合
ユーザー scachescache
提出日時 2014-12-01 15:03:50
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,071 bytes
コンパイル時間 3,593 ms
コンパイル使用メモリ 75,468 KB
実行使用メモリ 56,360 KB
最終ジャッジ日時 2023-09-02 00:58:39
合計ジャッジ時間 5,857 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,844 KB
testcase_01 AC 125 ms
55,828 KB
testcase_02 AC 126 ms
56,360 KB
testcase_03 AC 125 ms
55,852 KB
testcase_04 AC 125 ms
56,076 KB
testcase_05 AC 127 ms
55,512 KB
testcase_06 AC 128 ms
56,004 KB
testcase_07 AC 136 ms
56,128 KB
testcase_08 AC 125 ms
56,060 KB
testcase_09 AC 127 ms
55,908 KB
testcase_10 WA -
権限があれば一括ダウンロードができます

ソースコード

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;
					isWon[j][i] = -1;
				}else if(s.charAt(j)=='x'){
					isWon[i][j] = -1;
					isWon[j][i] = 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){
		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