結果

問題 No.424 立体迷路
ユーザー GrenacheGrenache
提出日時 2016-09-22 22:42:31
言語 Java19
(openjdk 21)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 1,972 bytes
コンパイル時間 5,114 ms
コンパイル使用メモリ 75,844 KB
実行使用メモリ 57,948 KB
最終ジャッジ日時 2023-09-18 16:46:06
合計ジャッジ時間 8,463 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
53,908 KB
testcase_01 AC 130 ms
55,688 KB
testcase_02 AC 133 ms
55,664 KB
testcase_03 AC 132 ms
56,088 KB
testcase_04 AC 135 ms
55,708 KB
testcase_05 AC 130 ms
55,840 KB
testcase_06 AC 133 ms
55,676 KB
testcase_07 AC 131 ms
55,536 KB
testcase_08 AC 131 ms
55,688 KB
testcase_09 AC 131 ms
55,664 KB
testcase_10 AC 132 ms
55,692 KB
testcase_11 AC 130 ms
55,808 KB
testcase_12 AC 131 ms
55,800 KB
testcase_13 AC 133 ms
56,328 KB
testcase_14 AC 134 ms
53,636 KB
testcase_15 AC 133 ms
57,948 KB
testcase_16 AC 132 ms
57,700 KB
testcase_17 AC 138 ms
55,472 KB
testcase_18 AC 137 ms
55,712 KB
testcase_19 AC 141 ms
56,036 KB
testcase_20 AC 139 ms
55,752 KB
testcase_21 AC 129 ms
56,048 KB
testcase_22 AC 133 ms
55,744 KB
testcase_23 AC 134 ms
55,804 KB
testcase_24 AC 144 ms
55,376 KB
testcase_25 AC 142 ms
55,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder424 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int[] dx = {1, -1, 0, 0};
		int[] dy = {0, 0, 1, -1};
		int[] dx2 = {2, -2, 0, 0};
		int[] dy2 = {0, 0, 2, -2};

		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[][] b = new char[h][];
		for (int i = 0; i < h; i++) {
			b[i] = sc.next().toCharArray();
		}

		Queue<Integer> qx = new ArrayDeque<>();
		Queue<Integer> qy = new ArrayDeque<>();
		qx.add(sx);
		qy.add(sy);
		boolean[][] used = new boolean[h][w];
		used[sy][sx] = true;

		while (!qx.isEmpty()) {
			int x = qx.remove();
			int y = qy.remove();

			if (x == gx && y == gy) {
				pr.println("YES");

				return;
			}

			for (int i = 0, size = dx.length; i < size; i++) {
				int nx = x + dx[i];
				int ny = y + dy[i];

				if (nx < 0 || nx >= w || ny < 0 || ny >= h) {
					continue;
				}

				if (used[ny][nx] == true) {
					continue;
				}

				if (Math.abs(b[ny][nx] - b[y][x]) <= 1) {
					qx.add(nx);
					qy.add(ny);
					used[ny][nx] = true;
				}
			}

			for (int i = 0, size = dx.length; i < size; i++) {
				int nx = x + dx[i];
				int ny = y + dy[i];
				int nx2 = x + dx2[i];
				int ny2 = y + dy2[i];

				if (nx2 < 0 || nx2 >= w || ny2 < 0 || ny2 >= h) {
					continue;
				}

				if (used[ny2][nx2] == true) {
					continue;
				}

				if (b[ny2][nx2] == b[y][x] && b[ny][nx] < b[y][x]) {
					qx.add(nx2);
					qy.add(ny2);
					used[ny2][nx2] = true;
				}
			}
		}

		pr.println("NO");
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);

		solve();

		pr.close();
		sc.close();
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0