結果
問題 |
No.424 立体迷路
|
ユーザー |
![]() |
提出日時 | 2017-02-04 03:02:53 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,600 bytes |
コンパイル時間 | 2,023 ms |
コンパイル使用メモリ | 77,880 KB |
実行使用メモリ | 41,516 KB |
最終ジャッジ日時 | 2024-12-24 06:07:50 |
合計ジャッジ時間 | 6,220 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 19 WA * 2 |
ソースコード
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"); 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&&Math.abs(Hight[nx][ny]-Hight[x][y])==0) ){ dist[nx][ny]=dist[x][y]+1; q.add(nx); q.add(ny); } } } } System.out.println("NO"); } }