結果
問題 | No.43 野球の試合 |
ユーザー |
![]() |
提出日時 | 2015-06-03 20:28:59 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 160 ms / 5,000 ms |
コード長 | 2,163 bytes |
コンパイル時間 | 2,552 ms |
コンパイル使用メモリ | 85,136 KB |
実行使用メモリ | 57,052 KB |
最終ジャッジ日時 | 2024-07-06 13:58:10 |
合計ジャッジ時間 | 3,819 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 7 |
ソースコード
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;}@Overridepublic 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);}}@Overridepublic 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);int order = 1, prev_degs = sorted[0].degs;for(int i = 0; i < N; i++){if(sorted[i].degs < prev_degs){order++;prev_degs = sorted[i].degs;}if(sorted[i].id == 0){return order;}}return N;}else if(!undecided[y][x]){return dfs(N, (x == N - 1) ? y + 1 : x + 1, 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));}}