結果

問題 No.424 立体迷路
ユーザー 37zigen
提出日時 2016-09-22 22:51:53
言語 Java
(openjdk 23)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 1,813 bytes
コンパイル時間 3,088 ms
コンパイル使用メモリ 79,392 KB
実行使用メモリ 41,744 KB
最終ジャッジ日時 2024-07-05 07:04:53
合計ジャッジ時間 6,670 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

package No400番台;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Scanner;

public class B {
	public static void main(String[] args) {
		new B().run();
	}

	void run() {
		solver();
	}

	void solver() {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		int sy = sc.nextInt() - 1;
		int sx = sc.nextInt() - 1;
		int gy = sc.nextInt() - 1;
		int gx = sc.nextInt() - 1;
		char[][] tmp = new char[h][w];
		for (int i = 0; i < h; i++) {
			tmp[i] = sc.next().toCharArray();
		}
		int[][] table = new int[h][w];
		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w; j++) {
				table[i][j] = tmp[i][j] - '0';
			}
		}
		ArrayDeque<P> que = new ArrayDeque<>();
		boolean[][] arrived = new boolean[h][w];
		arrived[sy][sx] = true;
		que.add(new P(sx, sy));
		while (!que.isEmpty()) {
			P p = que.poll();
			arrived[p.y][p.x] = true;
			for (int i = 0; i < 4; i++) {
				int nx = p.x + dx[i];
				int ny = p.y + dy[i];
				if (0 <= nx && nx < w && 0 <= ny && ny < h && !arrived[ny][nx]) {
					if (Math.abs(table[p.y][p.x] - table[ny][nx]) <= 1) {
						que.add(new P(nx, ny));
						arrived[ny][nx] = true;
					}
				}
				int nnx = p.x + 2 * dx[i];
				int nny = p.y + 2 * dy[i];
				if (0 <= nnx && nnx < w && 0 <= nny && nny < h && !arrived[nny][nnx]) {
					if (table[p.y][p.x] == table[nny][nnx] && table[p.y][p.x] > table[ny][nx]) {
						que.add(new P(nnx, nny));
						arrived[nny][nnx] = true;
					}
				}
			}
		}
		if (arrived[gy][gx]) {
			System.out.println("YES");
		} else {
			System.out.println("NO");
		}
	}

	int[] dx = { 1, -1, 0, 0 };
	int[] dy = { 0, 0, 1, -1 };

	class P {
		int x;
		int y;

		P(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	void tr(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
}
0