結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー mobius_bkstmobius_bkst
提出日時 2015-06-21 10:25:10
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,819 bytes
コンパイル時間 4,607 ms
コンパイル使用メモリ 74,908 KB
実行使用メモリ 49,760 KB
最終ジャッジ日時 2023-09-21 22:26:18
合計ジャッジ時間 5,451 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,196 KB
testcase_01 AC 42 ms
49,304 KB
testcase_02 AC 43 ms
49,300 KB
testcase_03 AC 43 ms
49,388 KB
testcase_04 WA -
testcase_05 AC 43 ms
49,712 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 42 ms
49,384 KB
testcase_09 AC 42 ms
49,480 KB
testcase_10 AC 42 ms
49,304 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 42 ms
49,480 KB
testcase_14 AC 42 ms
49,260 KB
testcase_15 AC 42 ms
49,256 KB
testcase_16 AC 42 ms
49,392 KB
testcase_17 WA -
testcase_18 AC 42 ms
49,424 KB
testcase_19 AC 41 ms
49,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class No278 {
    public static void main(String[] args) {
        try {
            int goal[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
                    { 13, 14, 15, 0 } };
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    System.in));
            int start[][] = new int[4][4];
            for (int i = 0; i < 4; i++) {
                start[i] = strToIntArray(br.readLine());
            }
            String str = "Yes";
            for (int i = 0; i < 4; i++) {
                for (int j = 0; j < 4; j++) {
                    if (goal[i][j] == 0) {
                        continue;
                    }
                    for (int j2 = 0; j2 < 4; j2++) {
                        for (int k = 0; k < 4; k++) {
                            // 距離が2以上離れてたらむりだろ
                            if (goal[i][j] == start[j2][k]) {
                                if (1 < Math.abs(i - j2) + Math.abs(j - k)) {
                                    str = "No";
                                }
                            }
                        }
                    }
                }
            }
            int start2[] = new int[16];
            int count = 0;
            for (int[] is : start) {
                for (int i : is) {
                    if (i != 0) {
                        start2[count] = i;
                    } else {
                        start2[count] = 100;
                    }
                    count++;
                }
            }

            if (!checkright15Puzzle(start2)) {
                str = "No";
            }

            System.out.println(str);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Error:" + e.getMessage());
        }
    }

    static boolean checkright15Puzzle(int[] x) {
        if (x == null) {
            return false;
        }
        int N = x.length;
        int count = 0;
        // ループの条件に注意
        for (int i = 0; i < N; i++) {
            if (x[i] != 1) {
                for (int j = i; j < N; j++) {
                    if (x[j] == i) {
                        int swap = x[i];
                        x[i] = i;
                        x[j] = swap;
                        count++;
                        break;
                    }
                }
            }
        }

        return count % 2 == 0;
    }

    static int[] strToIntArray(String S) {
        String[] strArray = S.split(" ");
        int[] intArray = new int[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            intArray[i] = Integer.parseInt(strArray[i]);
        }
        return intArray;
    }
}
0