結果

問題 No.424 立体迷路
ユーザー 37zigen37zigen
提出日時 2016-09-22 22:51:53
言語 Java21
(openjdk 21)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 1,813 bytes
コンパイル時間 3,716 ms
コンパイル使用メモリ 75,240 KB
実行使用メモリ 56,340 KB
最終ジャッジ日時 2023-09-18 16:48:27
合計ジャッジ時間 8,150 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 125 ms
56,116 KB
testcase_01 AC 125 ms
55,960 KB
testcase_02 AC 125 ms
56,060 KB
testcase_03 AC 123 ms
56,040 KB
testcase_04 AC 128 ms
56,072 KB
testcase_05 AC 125 ms
55,848 KB
testcase_06 AC 125 ms
56,160 KB
testcase_07 AC 126 ms
55,792 KB
testcase_08 AC 123 ms
56,004 KB
testcase_09 AC 127 ms
55,724 KB
testcase_10 AC 125 ms
56,340 KB
testcase_11 AC 125 ms
55,908 KB
testcase_12 AC 128 ms
55,728 KB
testcase_13 AC 126 ms
55,984 KB
testcase_14 AC 128 ms
55,748 KB
testcase_15 AC 128 ms
55,860 KB
testcase_16 AC 126 ms
55,812 KB
testcase_17 AC 132 ms
55,952 KB
testcase_18 AC 132 ms
55,960 KB
testcase_19 AC 133 ms
55,888 KB
testcase_20 AC 131 ms
55,872 KB
testcase_21 AC 126 ms
55,900 KB
testcase_22 AC 125 ms
56,000 KB
testcase_23 AC 125 ms
55,920 KB
testcase_24 AC 135 ms
55,912 KB
testcase_25 AC 135 ms
56,008 KB
権限があれば一括ダウンロードができます

ソースコード

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