結果

問題 No.359 門松行列
ユーザー ぴろずぴろず
提出日時 2016-04-17 23:01:59
言語 Java21
(openjdk 21)
結果
AC  
実行時間 233 ms / 2,000 ms
コード長 2,146 bytes
コンパイル時間 2,758 ms
コンパイル使用メモリ 81,384 KB
実行使用メモリ 55,228 KB
最終ジャッジ日時 2024-04-15 02:24:14
合計ジャッジ時間 6,739 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
53,976 KB
testcase_01 AC 140 ms
54,332 KB
testcase_02 AC 142 ms
54,128 KB
testcase_03 AC 220 ms
55,228 KB
testcase_04 AC 181 ms
54,420 KB
testcase_05 AC 220 ms
54,836 KB
testcase_06 AC 160 ms
54,232 KB
testcase_07 AC 212 ms
55,008 KB
testcase_08 AC 229 ms
54,748 KB
testcase_09 AC 233 ms
43,388 KB
testcase_10 AC 227 ms
43,568 KB
testcase_11 AC 217 ms
43,452 KB
testcase_12 AC 227 ms
43,352 KB
testcase_13 AC 231 ms
43,464 KB
testcase_14 AC 222 ms
43,596 KB
testcase_15 AC 218 ms
42,632 KB
testcase_16 AC 229 ms
43,260 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