結果

問題 No.424 立体迷路
ユーザー tentententen
提出日時 2020-12-04 16:28:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 161 ms / 2,000 ms
コード長 2,667 bytes
コンパイル時間 2,803 ms
コンパイル使用メモリ 77,060 KB
実行使用メモリ 57,164 KB
最終ジャッジ日時 2023-10-13 05:36:24
合計ジャッジ時間 8,307 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,736 KB
testcase_01 AC 126 ms
56,060 KB
testcase_02 AC 129 ms
55,716 KB
testcase_03 AC 125 ms
55,860 KB
testcase_04 AC 130 ms
55,536 KB
testcase_05 AC 126 ms
55,668 KB
testcase_06 AC 123 ms
56,460 KB
testcase_07 AC 126 ms
56,052 KB
testcase_08 AC 127 ms
55,824 KB
testcase_09 AC 125 ms
55,996 KB
testcase_10 AC 128 ms
56,004 KB
testcase_11 AC 125 ms
55,880 KB
testcase_12 AC 121 ms
55,652 KB
testcase_13 AC 125 ms
55,716 KB
testcase_14 AC 125 ms
55,824 KB
testcase_15 AC 123 ms
56,080 KB
testcase_16 AC 123 ms
55,776 KB
testcase_17 AC 144 ms
55,760 KB
testcase_18 AC 144 ms
55,720 KB
testcase_19 AC 144 ms
55,916 KB
testcase_20 AC 139 ms
55,684 KB
testcase_21 AC 121 ms
56,152 KB
testcase_22 AC 126 ms
55,748 KB
testcase_23 AC 123 ms
54,068 KB
testcase_24 AC 161 ms
57,164 KB
testcase_25 AC 146 ms
56,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int h = sc.nextInt();
        int w = sc.nextInt();
        int start = (sc.nextInt() - 1) * w + sc.nextInt() - 1;
        int goal = (sc.nextInt() - 1) * w + sc.nextInt() - 1;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < h; i++) {
            sb.append(sc.next());
        }
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        for (int i = 0; i < h * w; i++) {
            graph.add(new ArrayList<>());
        }
        char[] field = sb.toString().toCharArray();
        for (int i = 0; i < h; i++) {
            int[] idxes = new int[10];
            Arrays.fill(idxes, -1);
            int prev = -10;
            for (int j = 0; j < w; j++) {
                int x = i * w + j;
                int tmp = field[x] - '0';
                if (Math.abs(prev - tmp) == 1) {
                    graph.get(x - 1).add(x);
                    graph.get(x).add(x - 1);
                }
                if (idxes[tmp] != -1) {
                    graph.get(x).add(idxes[tmp]);
                    graph.get(idxes[tmp]).add(x);
                }
                idxes[tmp] = x;
                for (int k = tmp - 1; k >= 0; k--) {
                    idxes[k] = -1;
                }
                prev = tmp;
            }
        }
        for (int i = 0; i < w; i++) {
            int[] idxes = new int[10];
            Arrays.fill(idxes, -1);
            int prev = -10;
            for (int j = 0; j < h; j++) {
                int x = i + j * w;
                int tmp = field[x] - '0';
                if (Math.abs(prev - tmp) == 1) {
                    graph.get(x - w).add(x);
                    graph.get(x).add(x - w);
                }
                if (idxes[tmp] != -1) {
                    graph.get(x).add(idxes[tmp]);
                    graph.get(idxes[tmp]).add(x);
                }
                idxes[tmp] = x;
                for (int k = tmp - 1; k >= 0; k--) {
                    idxes[k] = -1;
                }
                prev = tmp;
            }
        }
        ArrayDeque<Integer> deq = new ArrayDeque<>();
        deq.add(start);
        boolean[] visited = new boolean[h * w];
        while (deq.size() > 0) {
            int x = deq.poll();
            if (visited[x]) {
                continue;
            }
            visited[x] = true;
            deq.addAll(graph.get(x));
        }
        if (visited[goal]) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
}
0