結果

問題 No.424 立体迷路
ユーザー tentententen
提出日時 2022-08-10 15:53:20
言語 Java21
(openjdk 21)
結果
AC  
実行時間 83 ms / 2,000 ms
コード長 2,730 bytes
コンパイル時間 3,326 ms
コンパイル使用メモリ 78,456 KB
実行使用メモリ 54,140 KB
最終ジャッジ日時 2023-10-21 01:47:01
合計ジャッジ時間 6,055 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 62 ms
52,476 KB
testcase_01 AC 60 ms
53,576 KB
testcase_02 AC 61 ms
53,584 KB
testcase_03 AC 62 ms
53,588 KB
testcase_04 AC 61 ms
53,768 KB
testcase_05 AC 60 ms
53,588 KB
testcase_06 AC 61 ms
53,588 KB
testcase_07 AC 60 ms
53,576 KB
testcase_08 AC 61 ms
53,592 KB
testcase_09 AC 59 ms
53,584 KB
testcase_10 AC 61 ms
53,588 KB
testcase_11 AC 60 ms
53,592 KB
testcase_12 AC 61 ms
53,592 KB
testcase_13 AC 62 ms
53,584 KB
testcase_14 AC 62 ms
53,592 KB
testcase_15 AC 61 ms
53,608 KB
testcase_16 AC 60 ms
53,596 KB
testcase_17 AC 70 ms
53,608 KB
testcase_18 AC 71 ms
53,604 KB
testcase_19 AC 70 ms
53,596 KB
testcase_20 AC 70 ms
54,140 KB
testcase_21 AC 60 ms
51,712 KB
testcase_22 AC 62 ms
53,600 KB
testcase_23 AC 61 ms
53,596 KB
testcase_24 AC 82 ms
54,136 KB
testcase_25 AC 83 ms
54,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner();
        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;
        ArrayList<ArrayList<Integer>> graph = new ArrayList<>();
        char[][] field = new char[h][];
        for (int i = 0; i < h; i++) {
            field[i] = sc.next().toCharArray();
            for (int j = 0; j < w; j++) {
                graph.add(new ArrayList<>());
                if (i > 0 && Math.abs(field[i][j] - field[i - 1][j]) <= 1) {
                    graph.get(i * w + j).add((i - 1) * w + j);
                    graph.get((i - 1) * w + j).add(i * w + j);
                }
                if (i > 1 && field[i][j] == field[i - 2][j] && field[i][j] > field[i - 1][j]) {
                    graph.get(i * w + j).add((i - 2) * w + j);
                    graph.get((i - 2) * w + j).add(i * w + j);
                }
                if (j > 0 && Math.abs(field[i][j] - field[i][j - 1]) <= 1) {
                    graph.get(i * w + j).add(i * w + j - 1);
                    graph.get(i * w + j - 1).add(i * w + j);
                }
                if (j > 1 && field[i][j] == field[i][j - 2] && field[i][j] > field[i][j - 1]) {
                    graph.get(i * w + j).add(i * w + j - 2);
                    graph.get(i * w + j - 2).add(i * w + j);
                }
            }
        }
        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");
        }
    }
}

class Scanner {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer("");
    
    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 String next() throws Exception {
        while (!st.hasMoreTokens()) {
            st = new StringTokenizer(br.readLine());
        }
        return st.nextToken();
    }
}
0