結果

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

ソースコード

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