結果

問題 No.43 野球の試合
ユーザー jp_stejp_ste
提出日時 2016-05-06 23:43:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 478 ms / 5,000 ms
コード長 2,396 bytes
コンパイル時間 2,985 ms
コンパイル使用メモリ 80,936 KB
実行使用メモリ 53,584 KB
最終ジャッジ日時 2024-04-15 13:33:43
合計ジャッジ時間 4,934 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
41,388 KB
testcase_01 AC 130 ms
41,556 KB
testcase_02 AC 130 ms
41,340 KB
testcase_03 AC 137 ms
41,120 KB
testcase_04 AC 136 ms
41,480 KB
testcase_05 AC 138 ms
41,448 KB
testcase_06 AC 152 ms
41,968 KB
testcase_07 AC 478 ms
53,584 KB
testcase_08 AC 132 ms
41,832 KB
testcase_09 AC 131 ms
41,716 KB
testcase_10 AC 134 ms
41,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Comparator;
import java.util.Scanner;
import java.util.TreeSet;

public class Main {
    static int N;
    static char list[][];
    
    public static void main(String[] args) {
        try (Scanner scan = new Scanner(System.in)) {
            N = scan.nextInt();
            list = new char[N][N];
            for(int i=0; i<N; i++) {
                String line = scan.next();
                list[i] = line.toCharArray();
            }
            
            int count = 0;
            for(int i=0; i<N; i++) {
                for(int j=0; j<N; j++) {
                    if(i<j && list[i][j] == '-') count++;   
                }
            }
            
            int ans = N;
            for(int i=0; i<Math.pow(2, count); i++) {
                String tmp = Integer.toBinaryString(i);
                int zeroNum = count - tmp.length();
                for(int j=0; j<zeroNum; j++) tmp = "o" + tmp;
                tmp = tmp.replace('0', 'o');
                tmp = tmp.replace('1', 'x');

                char tmpList[][] = new char[N][N];
                for(int j=0; j<N; j++) tmpList[j] = list[j].clone();
                
                int look = 0;
                for(int j=0; j<N; j++) {
                    for(int k=0; k<N; k++) {
                        if(j<k && tmpList[j][k] == '-') {
                            char now = tmp.charAt(look);
                            tmpList[j][k] = now;
                            if(now == 'o') tmpList[k][j] = 'x';
                            else           tmpList[k][j] = 'o';
                            look++;
                        }
                    }
                }
                
                int myTeam = getWinNum(0, tmpList);
                TreeSet<Integer> set = new TreeSet<>(Comparator.reverseOrder());
                for(int j=0; j<N; j++) {
                    set.add(getWinNum(j, tmpList));
                }

                int tmpAns = 0;
                for(Integer v : set) {
                    tmpAns++;
                    if(v == myTeam) break;
                }
                ans = Math.min(ans, tmpAns);
            }
            System.out.println(ans);
        }
    }
    
    static int getWinNum(int i, char list[][]) {
        int count = 0;
        for(int j=0; j<N; j++) {
            if(list[i][j] == 'o') count++;
        }
        return count;
    }
}
0