結果
問題 | No.424 立体迷路 |
ユーザー |
|
提出日時 | 2016-09-22 22:41:26 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 128 ms / 2,000 ms |
コード長 | 1,470 bytes |
コンパイル時間 | 2,952 ms |
コンパイル使用メモリ | 77,640 KB |
実行使用メモリ | 41,524 KB |
最終ジャッジ日時 | 2024-07-05 07:02:16 |
合計ジャッジ時間 | 6,675 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 21 |
ソースコード
import java.io.PrintStream;import java.util.Scanner;public class Y424 {int h, w, sx, sy, tx, ty;String[] s;boolean[][] visited;static int[] dx = { 0, 1, 0, -1 };static int[] dy = { 1, 0, -1, 0 };Y424() throws Exception {Scanner in = new Scanner(System.in);PrintStream out = new PrintStream(System.out);h = in.nextInt();w = in.nextInt();sx = in.nextInt();sy = in.nextInt();tx = in.nextInt();ty = in.nextInt();s = new String[h];for (int i = 0; i < h; i++) {s[i] = in.next();}visited = new boolean[h][w];visited[sx-1][sy-1] = true;dfs(sx-1, sy-1);if (visited[tx-1][ty-1]) {out.println("YES");} else {out.println("NO");}out.flush();}void dfs(int x, int y) {for (int dir = 0; dir < 4; dir++) {int xx = x + dx[dir];int yy = y + dy[dir];if (0 <= xx && xx < h && 0 <= yy && yy < w) {if (Math.abs((int)s[xx].charAt(yy) - (int)s[x].charAt(y)) <= 1 && !visited[xx][yy]) {visited[xx][yy] = true;dfs(xx, yy);}int xxx = x + 2 * dx[dir];int yyy = y + 2 * dy[dir];if (0 <= xxx && xxx < h && 0 <= yyy && yyy < w) {if (s[x].charAt(y) == s[xxx].charAt(yyy) && (int)s[x].charAt(y) > (int)s[xx].charAt(yy)) {if (!visited[xxx][yyy]) {visited[xxx][yyy] = true;dfs(xxx, yyy);}}}}}}public static void main(String argv[]) throws Exception {new Y424();}}