結果

問題 No.424 立体迷路
ユーザー atkrymatkrym
提出日時 2016-10-19 15:44:00
言語 Java21
(openjdk 21)
結果
AC  
実行時間 152 ms / 2,000 ms
コード長 1,989 bytes
コンパイル時間 2,053 ms
コンパイル使用メモリ 74,356 KB
実行使用メモリ 56,552 KB
最終ジャッジ日時 2023-09-18 16:58:59
合計ジャッジ時間 6,623 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
56,344 KB
testcase_01 AC 127 ms
55,820 KB
testcase_02 AC 126 ms
55,920 KB
testcase_03 AC 124 ms
55,788 KB
testcase_04 AC 129 ms
56,552 KB
testcase_05 AC 127 ms
55,956 KB
testcase_06 AC 128 ms
55,708 KB
testcase_07 AC 127 ms
55,664 KB
testcase_08 AC 127 ms
55,540 KB
testcase_09 AC 126 ms
55,388 KB
testcase_10 AC 125 ms
56,000 KB
testcase_11 AC 127 ms
56,076 KB
testcase_12 AC 126 ms
56,004 KB
testcase_13 AC 130 ms
55,580 KB
testcase_14 AC 130 ms
55,740 KB
testcase_15 AC 130 ms
55,992 KB
testcase_16 AC 129 ms
55,844 KB
testcase_17 AC 138 ms
55,388 KB
testcase_18 AC 138 ms
55,484 KB
testcase_19 AC 136 ms
53,600 KB
testcase_20 AC 136 ms
55,672 KB
testcase_21 AC 128 ms
55,832 KB
testcase_22 AC 129 ms
55,844 KB
testcase_23 AC 127 ms
55,652 KB
testcase_24 AC 137 ms
55,576 KB
testcase_25 AC 152 ms
56,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    private static Scanner sc = new Scanner(System.in);
    private static int h;
    private static int w;
    private static int sx;
    private static int sy;
    private static int ex;
    private static int ey;
    private static int[][] ary;
    private static boolean[][] is;
    public static void main(String[] args) throws Exception {
        h = sc.nextInt();
        w = sc.nextInt();
        sx = sc.nextInt()-1;
        sy = sc.nextInt()-1;
        ex = sc.nextInt()-1;
        ey = sc.nextInt()-1;
        
        ary = new int[h][w];
        is = new boolean[h][w];
        
        for (int i = 0;i < h;i++) {
            String s = sc.next();
            for (int j = 0;j < w;j++) {
                ary[i][j] = Integer.parseInt(s.substring(j, j+1));
            }
        }
        
        if (move(sx, sy)) {
            System.out.println("YES");
        } else {
            System.out.println("NO");
        }
    }
    
    private static boolean move(int x, int y) {
        
        if (x == ex && y == ey) return true;
        int[] dx = {-1, 0, 1, 0, -2, 0, 2, 0};
        int[] dy = {0, 1, 0, -1, 0, 2, 0, -2};
        
        int height = ary[x][y];
        is[x][y] = true;
        
        for (int i = 0;i < 8;i++) {
            int nx = x + dx[i];
            int ny = y + dy[i];
            if (0 <= nx && nx < h && 0 <= ny && ny < w && !is[nx][ny]) {
                int nheight = ary[nx][ny];
                boolean m = false;
                if (i < 4) {
                    m = (height-1 <= nheight && nheight <= height+1);
                } else {
                    int iheight = ary[x+dx[i-4]][y+dy[i-4]];
                    m = (height == nheight && iheight < height);
                }
                
                if (m) {
                    if (move(nx, ny)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
0