結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー GrenacheGrenache
提出日時 2015-06-19 22:47:31
言語 Java21
(openjdk 21)
結果
AC  
実行時間 118 ms / 5,000 ms
コード長 1,374 bytes
コンパイル時間 2,952 ms
コンパイル使用メモリ 74,180 KB
実行使用メモリ 57,932 KB
最終ジャッジ日時 2023-09-21 10:01:44
合計ジャッジ時間 6,155 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
56,288 KB
testcase_01 AC 115 ms
56,288 KB
testcase_02 AC 116 ms
55,972 KB
testcase_03 AC 115 ms
56,152 KB
testcase_04 AC 114 ms
56,276 KB
testcase_05 AC 116 ms
57,932 KB
testcase_06 AC 117 ms
55,836 KB
testcase_07 AC 117 ms
56,032 KB
testcase_08 AC 117 ms
55,968 KB
testcase_09 AC 116 ms
56,032 KB
testcase_10 AC 117 ms
56,012 KB
testcase_11 AC 115 ms
56,432 KB
testcase_12 AC 117 ms
56,180 KB
testcase_13 AC 117 ms
55,804 KB
testcase_14 AC 118 ms
56,144 KB
testcase_15 AC 115 ms
55,856 KB
testcase_16 AC 114 ms
56,028 KB
testcase_17 AC 115 ms
55,784 KB
testcase_18 AC 112 ms
56,048 KB
testcase_19 AC 114 ms
55,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;


public class Main_yukicoder228 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        a = new int[4][4];
        int sx = 0;
        int sy = 0;
        g = new boolean[4][4];
        for (int i = 0; i < 4; i++) {
        	for (int j = 0; j < 4; j++) {
        		a[i][j] = sc.nextInt();
        		if (a[i][j] == 0) {
        			sx = j;
        			sy = i;
        		}
        		if (a[i][j] == i * 4 + j + 1) {
        			g[i][j] = true;
        		}
        	}
        }
        
		g[sy][sx] = true;
        dfs(sx, sy);

        boolean ret = true;
        for (int i = 0; i < 4; i++) {
        	for (int j = 0; j < 4; j++) {
        		if (!g[i][j]) {
        			ret = false;
        		}
        	}
        }
        
        if (ret) {
        	System.out.println("Yes");
        } else {
        	System.out.println("No");
        }
        
        sc.close();
    }

    static int[][] a;
    static boolean[][] g;
	static int[] dx = {1, -1, 0, 0};
	static int[] dy = {0, 0, 1, -1};

    private static void dfs(int sx, int sy) {
		for (int i = 0; i < 4; i++) {
			int tmpx = sx + dx[i];
			int tmpy = sy + dy[i];
			if (tmpx >= 0 && tmpx < 4 && tmpy >= 0 && tmpy < 4) {
				if (a[tmpy][tmpx] == sy * 4 + sx + 1) {
					g[tmpy][tmpx] = true;
					dfs(tmpx, tmpy);
					break;
				}
			}
		}
	}
}
0