結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー YamaKasaYamaKasa
提出日時 2018-09-04 00:01:10
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,333 bytes
コンパイル時間 2,179 ms
コンパイル使用メモリ 77,328 KB
実行使用メモリ 42,052 KB
最終ジャッジ日時 2024-04-18 09:20:45
合計ジャッジ時間 5,390 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
41,708 KB
testcase_01 AC 111 ms
41,412 KB
testcase_02 AC 111 ms
41,444 KB
testcase_03 AC 124 ms
41,760 KB
testcase_04 AC 125 ms
41,224 KB
testcase_05 AC 126 ms
41,100 KB
testcase_06 AC 126 ms
41,324 KB
testcase_07 AC 129 ms
41,320 KB
testcase_08 AC 113 ms
41,408 KB
testcase_09 AC 126 ms
41,472 KB
testcase_10 AC 132 ms
41,216 KB
testcase_11 AC 125 ms
41,440 KB
testcase_12 AC 126 ms
41,772 KB
testcase_13 AC 118 ms
41,600 KB
testcase_14 AC 124 ms
42,052 KB
testcase_15 AC 113 ms
41,600 KB
testcase_16 AC 137 ms
41,680 KB
testcase_17 WA -
testcase_18 AC 123 ms
41,668 KB
testcase_19 AC 129 ms
41,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int[][]p = new int[4][4];
		for(int i = 0; i < 4; i++) {
			for(int j = 0; j < 4; j++) {
				p[i][j] = scan.nextInt();
			}
		}
		scan.close();
		int r0 = 0;
		int c0 = 0;
		int cnt = 0;
		for(int i = 0; i < 4; i++) {
			for(int j = 0; j < 4; j++) {
				int t = p[i][j];
				if(t == 0) {
					r0 = i;
					c0 = j;
					continue;
				}
				int l = len(t, i, j);
				if(l >= 2) {
					System.out.println("No");
					System.exit(0);
				}else if(l == 1) {
					cnt ++;
				}
			}
		}
		int []dx = {1, -1, 0, 0};
		int []dy = {0, 0, 1, -1};
		boolean flag = true;
		for(int i = 0; i < cnt; i++) {
			flag = true;
			for(int j = 0; j < 4; j++) {
				int nr = dx[j] + r0;
				int nc = dy[j] + c0;
				if(nr < 0 || nc < 0 || nr > 3 || nc > 3) {
					continue;
				}
				int k = p[nr][nc];
				int l = len(k, nr, nc);
				if(l == 1) {
					r0 = nr;
					c0 = nc;
					flag = false;
					break;
				}
			}
			if(flag) {
				System.out.println("No");
				System.exit(0);
			}
		}
		System.out.println("Yes");
	}
	static int len(int n, int nr, int nc) {
		int r = n / 4;
		int c = n % 4;
		if(c == 0) {
			c = 3;
			r -= 1;
		}else {
			c -= 1;
		}
		int l = Math.abs(nr - r) + Math.abs(nc - c);
		return l;
	}
}
0