結果

問題 No.228 ゆきこちゃんの 15 パズル
ユーザー tentententen
提出日時 2023-07-27 14:21:37
言語 Java21
(openjdk 21)
結果
AC  
実行時間 75 ms / 5,000 ms
コード長 3,868 bytes
コンパイル時間 3,007 ms
コンパイル使用メモリ 84,060 KB
実行使用メモリ 51,936 KB
最終ジャッジ日時 2024-04-15 02:06:05
合計ジャッジ時間 4,391 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,100 KB
testcase_01 AC 71 ms
51,384 KB
testcase_02 AC 72 ms
51,836 KB
testcase_03 AC 54 ms
50,432 KB
testcase_04 AC 73 ms
51,936 KB
testcase_05 AC 71 ms
51,684 KB
testcase_06 AC 74 ms
51,628 KB
testcase_07 AC 71 ms
51,932 KB
testcase_08 AC 75 ms
51,468 KB
testcase_09 AC 72 ms
51,704 KB
testcase_10 AC 74 ms
51,752 KB
testcase_11 AC 71 ms
38,772 KB
testcase_12 AC 53 ms
37,016 KB
testcase_13 AC 54 ms
36,912 KB
testcase_14 AC 54 ms
37,220 KB
testcase_15 AC 54 ms
37,360 KB
testcase_16 AC 55 ms
37,140 KB
testcase_17 AC 55 ms
37,516 KB
testcase_18 AC 56 ms
37,272 KB
testcase_19 AC 55 ms
36,972 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Main {
    static int[][] goal = new int[4][4];
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        int[][] org = new int[4][4];
        int idx = 1;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                goal[i][j] = sc.nextInt();
                org[i][j] = idx % 16;
                idx++;
            }
        }
        check(org, new boolean[16]);
        System.out.println("No");
    }
    
    static boolean same(int[][] org) {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (goal[i][j] != org[i][j]) {
                    return false;
                }
            }
        }
        return true;
    }
    
    static void check(int[][] org, boolean[] used) {
        if (same(org)) {
            System.out.println("Yes");
            System.exit(0);
        }
        int r = 0;
        int c = 0;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (org[i][j] == 0) {
                    r = i;
                    c = j;
                    break;
                }
            }
        }
        if (r > 0) {
            int v = org[r - 1][c];
            if (!used[v]) {
                org[r - 1][c] = 0;
                org[r][c] = v;
                used[v] = true;
                check(org, used);
                used[v] = false;
                org[r - 1][c] = v;
                org[r][c] = 0;
            }
        }
        if (r < 3) {
            int v = org[r + 1][c];
            if (!used[v]) {
                org[r + 1][c] = 0;
                org[r][c] = v;
                used[v] = true;
                check(org, used);
                used[v] = false;
                org[r + 1][c] = v;
                org[r][c] = 0;
            }
        }
        if (c > 0) {
            int v = org[r][c - 1];
            if (!used[v]) {
                org[r][c - 1] = 0;
                org[r][c] = v;
                used[v] = true;
                check(org, used);
                used[v] = false;
                org[r][c - 1] = v;
                org[r][c] = 0;
            }
        }
        if (c < 3) {
            int v = org[r][c + 1];
            if (!used[v]) {
                org[r][c + 1] = 0;
                org[r][c] = v;
                used[v] = true;
                check(org, used);
                used[v] = false;
                org[r][c + 1] = v;
                org[r][c] = 0;
            }
        }
    }
} 
class Utilities {
    static String arrayToLineString(Object[] arr) {
        return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
    }
    
    static String arrayToLineString(int[] arr) {
        return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
    }
}
class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    StringBuilder sb = new StringBuilder();
    
    public Scanner() throws Exception {
        
    }
    
    public int nextInt() throws Exception {
        return Integer.parseInt(next());
    }
    
    public long nextLong() throws Exception {
        return Long.parseLong(next());
    }
    
    public double nextDouble() throws Exception {
        return Double.parseDouble(next());
    }
    
    public int[] nextIntArray() throws Exception {
        return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
    }
    
    public String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
    
}
0