結果

問題 No.424 立体迷路
ユーザー atkrym
提出日時 2016-10-19 15:44:00
言語 Java
(openjdk 23)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 1,989 bytes
コンパイル時間 2,169 ms
コンパイル使用メモリ 78,024 KB
実行使用メモリ 41,768 KB
最終ジャッジ日時 2024-07-05 07:12:21
合計ジャッジ時間 6,581 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

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