結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
53,020 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 131 ms
54,140 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 129 ms
54,408 KB
testcase_13 AC 132 ms
54,108 KB
testcase_14 AC 117 ms
54,232 KB
testcase_15 AC 116 ms
53,040 KB
testcase_16 AC 128 ms
54,084 KB
testcase_17 AC 130 ms
54,328 KB
testcase_18 AC 129 ms
54,068 KB
testcase_19 AC 125 ms
54,228 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(cnt);
		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