結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー YamaKasaYamaKasa
提出日時 2018-09-04 02:47:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 136 ms / 5,000 ms
コード長 1,431 bytes
コンパイル時間 1,974 ms
コンパイル使用メモリ 76,916 KB
実行使用メモリ 41,684 KB
最終ジャッジ日時 2024-04-19 04:37:19
合計ジャッジ時間 5,358 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
41,684 KB
testcase_01 AC 129 ms
41,120 KB
testcase_02 AC 130 ms
41,248 KB
testcase_03 AC 125 ms
41,444 KB
testcase_04 AC 117 ms
40,884 KB
testcase_05 AC 129 ms
41,608 KB
testcase_06 AC 126 ms
41,096 KB
testcase_07 AC 130 ms
41,484 KB
testcase_08 AC 120 ms
39,964 KB
testcase_09 AC 129 ms
41,224 KB
testcase_10 AC 118 ms
39,992 KB
testcase_11 AC 121 ms
41,032 KB
testcase_12 AC 110 ms
40,152 KB
testcase_13 AC 107 ms
40,088 KB
testcase_14 AC 120 ms
41,500 KB
testcase_15 AC 126 ms
41,436 KB
testcase_16 AC 122 ms
41,264 KB
testcase_17 AC 105 ms
39,908 KB
testcase_18 AC 112 ms
40,640 KB
testcase_19 AC 111 ms
40,372 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 n = num(r0, c0);
				if(n == k) {
					p[r0][c0] = k;
					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;
	}
	static int num(int r, int c) {
		int n = 0;
		n = r * 4 + c + 1;
		return n;
	}
}
0