結果
問題 | No.359 門松行列 |
ユーザー |
![]() |
提出日時 | 2016-04-17 23:01:59 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 207 ms / 2,000 ms |
コード長 | 2,146 bytes |
コンパイル時間 | 2,590 ms |
コンパイル使用メモリ | 87,896 KB |
実行使用メモリ | 43,348 KB |
最終ジャッジ日時 | 2024-10-04 10:47:20 |
合計ジャッジ時間 | 5,920 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 17 |
ソースコード
package no359;import java.util.ArrayList;import java.util.Scanner;import java.util.TreeSet;public class Main {public static Scanner sc = new Scanner(System.in);public static void main(String[] args) {int t = sc.nextInt();for(int tt=0;tt<t;tt++) {solve();}}public static void solve() {int l = sc.nextInt();int[][] a = new int[3][3];TreeSet<Integer> ts = new TreeSet<>();int[] r = new int[2];int[] c = new int[2];int ind = 0;for(int i=0;i<3;i++) {for(int j=0;j<3;j++) {int h = sc.nextInt();a[i][j] = h;if (h == 0) {r[ind] = i;c[ind] = j;ind++;continue;}if (h < l) {ts.add(h);}if (l - h >= 1) {ts.add(l-h);}}}if (l >= 2) {ts.add(l / 2);ts.add((l+1) / 2);}ts.add(0);ts.add(l);ArrayList<Integer> ev1 = new ArrayList<>(ts);ArrayList<Pair> ev2 = new ArrayList<>();for(int i=1;i<ev1.size();i++) {int h = ev1.get(i);int h2 = ev1.get(i-1);if (h - h2 >= 2) {ev2.add(new Pair(h-1, h - h2 - 1));}if (i < ev1.size() - 1) {ev2.add(new Pair(h, 1));}}// System.out.println(ev2);int ans = 0;for(Pair p: ev2) {a[r[0]][c[0]] = p.x;a[r[1]][c[1]] = l - p.x;if (isKadomatsuMatrix(a)) {// System.out.println(p);ans += p.weight;}}System.out.println(ans);}static class Pair {int x;int weight;public Pair(int x, int weight) {super();this.x = x;this.weight = weight;}@Overridepublic String toString() {return "Pair [x=" + x + ", weight=" + weight + "]";}}public static boolean isKadomatsuSequence(long a,long b,long c) {if (a == b || b == c || a == c) {return false;}return b < a && b < c || b > a && b > c;}public static boolean isKadomatsuMatrix(int[][] a) {for(int i=0;i<3;i++) {if (!isKadomatsuSequence(a[i][0], a[i][1], a[i][2])) {return false;}if (!isKadomatsuSequence(a[0][i], a[1][i], a[2][i])) {return false;}}return isKadomatsuSequence(a[0][0], a[1][1], a[2][2]) && isKadomatsuSequence(a[0][2], a[1][1], a[2][0]);}}