結果

問題 No.43 野球の試合
ユーザー uafr_csuafr_cs
提出日時 2015-06-03 20:13:11
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,002 bytes
コンパイル時間 2,648 ms
コンパイル使用メモリ 82,208 KB
実行使用メモリ 62,104 KB
最終ジャッジ日時 2023-09-20 19:01:35
合計ジャッジ時間 5,183 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,840 KB
testcase_01 AC 122 ms
55,756 KB
testcase_02 AC 125 ms
55,708 KB
testcase_03 AC 122 ms
55,904 KB
testcase_04 AC 122 ms
55,652 KB
testcase_05 AC 126 ms
55,764 KB
testcase_06 AC 130 ms
55,708 KB
testcase_07 AC 181 ms
62,104 KB
testcase_08 WA -
testcase_09 AC 125 ms
56,008 KB
testcase_10 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	
	public static class Team implements Comparable<Team> {
		int degs, id;

		public Team(int degs, int id) {
			super();
			this.degs = degs;
			this.id = id;
		}

		@Override
		public int compareTo(Team o) {
			if(Integer.compare(o.degs, this.degs) != 0){
				return Integer.compare(o.degs, this.degs);
			}else{
				return Integer.compare(this.id, o.id);
			}
		}
		
		@Override
		public String toString(){
			return this.id + " : " + this.degs;
		}
	}
	
	public static int dfs(final int N, int x, int y, Team[] teams, boolean[][] undecided){
		if(y == N){
			Team[] sorted = new Team[N];
			System.arraycopy(teams, 0, sorted, 0, N);
			
			Arrays.sort(sorted);
			
			for(int i = 0; i < N; i++){
				if(sorted[i].id == 0){
					return i + 1;
				}
			}
			
			return N;
		}else if(!undecided[y][x]){
			return dfs(N, (x + 1) % N, x == N - 1 ? y + 1 : y, teams, undecided);
		}else{
			int ret = Integer.MAX_VALUE;
			{
				teams[y].degs++; teams[x].degs--;
				ret = Math.min(ret, dfs(N, x == N - 1 ? y + 1 : x + 1, (x == N - 1) ? y + 1 : y, teams, undecided));
				teams[y].degs--; teams[x].degs++;
			}
			{
				teams[y].degs--; teams[x].degs++;
				ret = Math.min(ret, dfs(N, x == N - 1 ? y + 1 : x + 1, (x == N - 1) ? y + 1 : y, teams, undecided));
				teams[y].degs++; teams[x].degs--;
			}
			
			return ret;
		}
	}
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		
		final int N = sc.nextInt();

		Team[] teams = new Team[N];
		for(int i = 0; i < N; i++){
			teams[i] = new Team(0, i);
		}
		boolean[][] undecided = new boolean[N][N];
		for(int i = 0; i < N; i++){
			char[] inputs = sc.next().toCharArray();
			
			for(int j = 0; j < N; j++){
				if(inputs[j] == 'o'){
					teams[i].degs++;
					teams[j].degs--;
				}else if(inputs[j] == '-'){
					undecided[i][j] = true;
				}
			}
		}
		
		System.out.println(dfs(N, 0, 0, teams, undecided));
		
	}
	
}
0