結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー YamaKasaYamaKasa
提出日時 2018-09-04 02:45:25
言語 Java21
(openjdk 21)
結果
AC  
実行時間 146 ms / 5,000 ms
コード長 1,631 bytes
コンパイル時間 2,598 ms
コンパイル使用メモリ 77,884 KB
実行使用メモリ 54,608 KB
最終ジャッジ日時 2024-04-19 04:35:11
合計ジャッジ時間 6,274 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
54,352 KB
testcase_01 AC 135 ms
54,348 KB
testcase_02 AC 134 ms
54,096 KB
testcase_03 AC 142 ms
54,408 KB
testcase_04 AC 134 ms
54,140 KB
testcase_05 AC 136 ms
54,012 KB
testcase_06 AC 138 ms
54,052 KB
testcase_07 AC 137 ms
54,204 KB
testcase_08 AC 134 ms
54,284 KB
testcase_09 AC 134 ms
54,344 KB
testcase_10 AC 135 ms
53,868 KB
testcase_11 AC 134 ms
54,204 KB
testcase_12 AC 139 ms
54,192 KB
testcase_13 AC 141 ms
54,264 KB
testcase_14 AC 143 ms
54,244 KB
testcase_15 AC 143 ms
54,444 KB
testcase_16 AC 145 ms
54,608 KB
testcase_17 AC 142 ms
54,200 KB
testcase_18 AC 139 ms
54,280 KB
testcase_19 AC 146 ms
54,344 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[4][4];
//		for(int i = 0; i < 4; i++) {
//			for(int j = 0; j < 4; j++) {
//				a[i][j] = i * 4 + j + 1;
//			}
//		}
//		a[3][3] = 0;

		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);
				//System.out.println(k + " " + n);
				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