結果

問題 No.424 立体迷路
ユーザー kohaku_kohakukohaku_kohaku
提出日時 2017-02-04 03:12:59
言語 Java19
(openjdk 21)
結果
AC  
実行時間 146 ms / 2,000 ms
コード長 1,947 bytes
コンパイル時間 2,316 ms
コンパイル使用メモリ 81,012 KB
実行使用メモリ 56,280 KB
最終ジャッジ日時 2023-09-18 17:00:47
合計ジャッジ時間 6,931 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,468 KB
testcase_01 AC 127 ms
55,852 KB
testcase_02 AC 130 ms
56,068 KB
testcase_03 AC 129 ms
55,832 KB
testcase_04 AC 129 ms
55,928 KB
testcase_05 AC 127 ms
55,592 KB
testcase_06 AC 127 ms
55,428 KB
testcase_07 AC 128 ms
55,732 KB
testcase_08 AC 134 ms
55,784 KB
testcase_09 AC 131 ms
55,828 KB
testcase_10 AC 129 ms
55,836 KB
testcase_11 AC 131 ms
55,656 KB
testcase_12 AC 132 ms
55,900 KB
testcase_13 AC 131 ms
56,280 KB
testcase_14 AC 131 ms
55,668 KB
testcase_15 AC 132 ms
55,704 KB
testcase_16 AC 131 ms
55,804 KB
testcase_17 AC 142 ms
55,808 KB
testcase_18 AC 141 ms
55,892 KB
testcase_19 AC 139 ms
55,996 KB
testcase_20 AC 139 ms
55,472 KB
testcase_21 AC 130 ms
55,772 KB
testcase_22 AC 131 ms
55,824 KB
testcase_23 AC 130 ms
55,692 KB
testcase_24 AC 146 ms
55,472 KB
testcase_25 AC 145 ms
55,740 KB
権限があれば一括ダウンロードができます

ソースコード

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 sx = sc.nextInt()-1;
        int sy = sc.nextInt()-1;
        int gx = sc.nextInt()-1;
        int gy = sc.nextInt()-1;
        int [][] Hight = new int [H][W];
        int [][] dist = new int [H][W];
        for(int i=0; i<H; i++){
            String tmp = sc.next();
            for(int j=0; j<W; j++){
                Hight[i][j] = tmp.charAt(j)-'0';
                dist[i][j] = -1;
            }
        }
        Queue<Integer> q = new LinkedList<>();
        int [] dx = {1,0,-1,0};
        int [] dy = {0,1,0,-1};
        q.add(sx);
        q.add(sy);
        dist[sx][sy]=0;
        while(!q.isEmpty()){
            int x = q.poll();
            int y = q.poll();
            if(x==gx&&y==gy){
                System.out.println("YES");
                //print(H,W,dist);
                return;
            }
            for(int k=1; k<=2; k++){
                for(int i=0; i<4; i++){
                    int nx = x+k*dx[i];
                    int ny = y+k*dy[i];
                    if(nx<0||ny<0||nx>=H||ny>=W)continue;
                    if(dist[nx][ny]!=-1)continue;
                    if( (k==1&&Math.abs(Hight[nx][ny]-Hight[x][y])<=1)||
                        (k==2&&(Hight[nx][ny]-Hight[x][y])==0&&Hight[nx][ny]>Hight[(nx+x)/2][(ny+y)/2]) ){
                        dist[nx][ny]=dist[x][y]+1;
                        q.add(nx);
                        q.add(ny);
                    }
                }
            }
        }
        //print(H,W,dist);
        System.out.println("NO");
    }
    static void print(int H, int W, int[][]A){
        for(int i=0; i<H; i++){
            for(int j=0; j<W; j++){
                System.out.print(A[i][j]+"  ");
            }
            System.out.println();
        }
    }
}
0