結果

問題 No.424 立体迷路
ユーザー neko_the_shadowneko_the_shadow
提出日時 2019-05-17 13:42:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 139 ms / 2,000 ms
コード長 2,427 bytes
コンパイル時間 2,158 ms
コンパイル使用メモリ 79,688 KB
実行使用メモリ 57,700 KB
最終ジャッジ日時 2023-09-18 17:07:20
合計ジャッジ時間 6,653 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
55,720 KB
testcase_01 AC 126 ms
55,688 KB
testcase_02 AC 128 ms
55,676 KB
testcase_03 AC 128 ms
55,860 KB
testcase_04 AC 130 ms
55,716 KB
testcase_05 AC 128 ms
55,376 KB
testcase_06 AC 128 ms
55,380 KB
testcase_07 AC 128 ms
55,716 KB
testcase_08 AC 127 ms
55,384 KB
testcase_09 AC 130 ms
55,408 KB
testcase_10 AC 127 ms
55,904 KB
testcase_11 AC 127 ms
55,964 KB
testcase_12 AC 127 ms
55,928 KB
testcase_13 AC 128 ms
55,764 KB
testcase_14 AC 127 ms
55,904 KB
testcase_15 AC 127 ms
55,688 KB
testcase_16 AC 125 ms
55,636 KB
testcase_17 AC 135 ms
55,688 KB
testcase_18 AC 138 ms
56,356 KB
testcase_19 AC 134 ms
55,696 KB
testcase_20 AC 134 ms
57,700 KB
testcase_21 AC 126 ms
55,944 KB
testcase_22 AC 129 ms
55,852 KB
testcase_23 AC 125 ms
55,632 KB
testcase_24 AC 138 ms
55,852 KB
testcase_25 AC 139 ms
55,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package _0424;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner stdin = new Scanner(System.in);
        int h = stdin.nextInt();
        int w = stdin.nextInt();
        int sx = stdin.nextInt() - 1;
        int sy = stdin.nextInt() - 1;
        int gx = stdin.nextInt() - 1;
        int gy = stdin.nextInt() - 1;
        
        int[][] b = new int[h][w];
        for (int x = 0; x < h; x++) {
            String s = stdin.next();
            for (int y = 0; y < w; y++) {
                b[x][y] = (int)(s.charAt(y) - '0');
            }
        }
        
        boolean[][] visited = new boolean[h][w];
        visited[sx][sy] = true;
        
        Tuple[] diffs = new Tuple[] {new Tuple(0, 2), new Tuple(0, -2), new Tuple(2, 0), new Tuple(-2, 0)};
        
        ArrayDeque<Tuple> stack = new ArrayDeque<>();
        stack.add(new Tuple(sx, sy));
        while (!stack.isEmpty()) {
            Tuple tuple = stack.pop();
            int x1 = tuple.x;
            int y1 = tuple.y;
            
            for (Tuple diff : diffs) {                
                int x2 = x1 + diff.x / 2;
                int y2 = y1 + diff.y / 2;
                int x3 = x1 + diff.x;
                int y3 = y1 + diff.y;
                
                if (0 <= x2 && x2 < h 
                        && 0 <= y2 && y2 < w
                        && 0 <= x3 && x3 < h 
                        && 0 <= y3 && y3 < w
                        && b[x1][y1] == b[x3][y3]
                        && b[x2][y2] < b[x1][y1]
                        && !visited[x3][y3]) {
                    stack.push(new Tuple(x3, y3));
                    visited[x3][y3] = true;
                }
                
                if (0 <= x2 && x2 < h 
                        && 0 <= y2 && y2 < w
                        && Math.abs(b[x1][y1] - b[x2][y2]) <= 1
                        && !visited[x2][y2]) {
                    stack.push(new Tuple(x2, y2));
                    visited[x2][y2] = true;
                }
            }
        }
        
        System.out.println(visited[gx][gy] ? "YES" : "NO");
    }
    
    private static class Tuple {
        private int x;
        private int y;
        
        public Tuple(int x, int y) {
            super();
            this.x = x;
            this.y = y;
        }
    }
}
0