結果
| 問題 |
No.424 立体迷路
|
| コンテスト | |
| ユーザー |
ぴろず
|
| 提出日時 | 2016-09-22 22:34:42 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 144 ms / 2,000 ms |
| コード長 | 1,371 bytes |
| コンパイル時間 | 2,148 ms |
| コンパイル使用メモリ | 79,596 KB |
| 実行使用メモリ | 54,252 KB |
| 最終ジャッジ日時 | 2024-07-05 07:01:03 |
| 合計ジャッジ時間 | 6,477 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 21 |
ソースコード
package no424;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;
public class Main {
static int[] D = {1,0,-1,0};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int h = sc.nextInt();
int w = sc.nextInt();
int[][] m = new int[h][w];
int si = sc.nextInt() - 1;
int sj = sc.nextInt() - 1;
int gi = sc.nextInt() - 1;
int gj = sc.nextInt() - 1;
for(int i=0;i<h;i++) {
String s = sc.next();
for(int j=0;j<w;j++) {
m[i][j] = s.charAt(j) - '0';
}
}
boolean[][] used = new boolean[h][w];
Queue<Integer> q = new ArrayDeque<>();
q.offer(si * 100 + sj);
used[si][sj] = true;
while(!q.isEmpty()) {
int c = q.poll();
int ci = c / 100;
int cj = c % 100;
for(int k=0;k<4;k++) {
int ni = ci + D[k];
int nj = cj + D[k^1];
if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
continue;
}
if (Math.abs(m[ci][cj] - m[ni][nj]) <= 1 && !used[ni][nj]) {
used[ni][nj] = true;
q.offer(ni * 100 + nj);
}
int ii = ni;
int jj = nj;
ni += D[k];
nj += D[k^1];
if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
continue;
}
if (m[ci][cj] == m[ni][nj] && m[ci][cj] > m[ii][jj] && !used[ni][nj]) {
used[ni][nj] = true;
q.offer(ni * 100 + nj);
}
}
}
System.out.println(used[gi][gj] ? "YES" : "NO");
}
}
ぴろず