結果

問題 No.424 立体迷路
ユーザー tenten
提出日時 2020-12-04 16:28:29
言語 Java
(openjdk 23)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 2,667 bytes
コンパイル時間 2,627 ms
コンパイル使用メモリ 83,392 KB
実行使用メモリ 42,784 KB
最終ジャッジ日時 2024-09-15 02:55:29
合計ジャッジ時間 7,305 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

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