結果

問題 No.359 門松行列
ユーザー ぴろずぴろず
提出日時 2016-04-17 23:01:59
言語 Java19
(openjdk 21)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 2,146 bytes
コンパイル時間 2,632 ms
コンパイル使用メモリ 78,756 KB
実行使用メモリ 57,616 KB
最終ジャッジ日時 2023-07-27 14:45:57
合計ジャッジ時間 7,095 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
55,864 KB
testcase_01 AC 126 ms
55,924 KB
testcase_02 AC 133 ms
55,548 KB
testcase_03 AC 217 ms
57,616 KB
testcase_04 AC 180 ms
56,132 KB
testcase_05 AC 214 ms
57,312 KB
testcase_06 AC 148 ms
55,540 KB
testcase_07 AC 211 ms
57,212 KB
testcase_08 AC 230 ms
57,408 KB
testcase_09 AC 218 ms
57,260 KB
testcase_10 AC 225 ms
57,104 KB
testcase_11 AC 226 ms
57,112 KB
testcase_12 AC 223 ms
57,548 KB
testcase_13 AC 225 ms
57,220 KB
testcase_14 AC 219 ms
57,252 KB
testcase_15 AC 215 ms
56,612 KB
testcase_16 AC 224 ms
56,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
		}
		@Override
		public 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]);
	}
}
0