結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー YamaKasaYamaKasa
提出日時 2018-09-04 00:28:58
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 1,532 bytes
コンパイル時間 3,794 ms
コンパイル使用メモリ 77,404 KB
実行使用メモリ 54,424 KB
最終ジャッジ日時 2024-04-18 10:30:49
合計ジャッジ時間 7,462 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
41,788 KB
testcase_01 AC 131 ms
41,748 KB
testcase_02 WA -
testcase_03 AC 140 ms
41,344 KB
testcase_04 AC 133 ms
41,536 KB
testcase_05 AC 134 ms
41,316 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 131 ms
41,908 KB
testcase_13 AC 143 ms
41,852 KB
testcase_14 AC 142 ms
41,628 KB
testcase_15 AC 141 ms
41,804 KB
testcase_16 AC 131 ms
40,484 KB
testcase_17 AC 136 ms
41,976 KB
testcase_18 AC 140 ms
41,864 KB
testcase_19 AC 137 ms
41,848 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;
		int[]a = new int[16];
		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) {
					int t = p[r0][c0];
					p[r0][c0] = k;
					p[nr][nc] = t;
					r0 = nr;
					c0 = nc;
					flag = false;
					if(a[t] != 0) {
						System.out.println("No");
						System.exit(0);
					}else {
						a[t] = 1;
					}
					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