結果

問題 No.424 立体迷路
ユーザー tomeruntomerun
提出日時 2016-09-22 22:38:29
言語 Java19
(openjdk 21)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,545 bytes
コンパイル時間 2,206 ms
コンパイル使用メモリ 74,932 KB
実行使用メモリ 56,152 KB
最終ジャッジ日時 2023-09-18 16:43:55
合計ジャッジ時間 6,797 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,940 KB
testcase_01 AC 128 ms
55,796 KB
testcase_02 AC 131 ms
55,780 KB
testcase_03 AC 129 ms
55,828 KB
testcase_04 AC 128 ms
55,988 KB
testcase_05 AC 126 ms
55,728 KB
testcase_06 AC 127 ms
55,652 KB
testcase_07 AC 127 ms
55,712 KB
testcase_08 AC 127 ms
55,440 KB
testcase_09 AC 128 ms
55,940 KB
testcase_10 AC 127 ms
55,964 KB
testcase_11 AC 130 ms
55,576 KB
testcase_12 AC 132 ms
55,760 KB
testcase_13 AC 132 ms
55,864 KB
testcase_14 AC 131 ms
55,720 KB
testcase_15 AC 131 ms
55,748 KB
testcase_16 AC 131 ms
55,788 KB
testcase_17 AC 133 ms
55,752 KB
testcase_18 AC 135 ms
55,708 KB
testcase_19 AC 134 ms
56,092 KB
testcase_20 AC 138 ms
56,016 KB
testcase_21 AC 128 ms
55,464 KB
testcase_22 AC 131 ms
55,936 KB
testcase_23 AC 127 ms
56,004 KB
testcase_24 AC 140 ms
56,152 KB
testcase_25 AC 136 ms
55,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	static Scanner sc = new Scanner(System.in);
	static int[] DX = { 1, 0, -1, 0 };
	static int[] DY = { 0, 1, 0, -1 };

	public static void main(String[] args) {
		int H = sc.nextInt();
		int W = sc.nextInt();
		int SX = sc.nextInt() - 1;
		int SY = sc.nextInt() - 1;
		int GX = sc.nextInt() - 1;
		int GY = sc.nextInt() - 1;
		int[][] B = new int[H][W];
		for (int i = 0; i < H; ++i) {
			char[] row = sc.next().toCharArray();
			for (int j = 0; j < W; ++j) {
				B[i][j] = row[j] - '0';
			}
		}
		boolean[][] visited = new boolean[H][W];
		visited[SX][SY] = true;
		ArrayList<Integer> q = new ArrayList<>();
		q.add((SX << 16) | SY);
		for (int i = 0; i < q.size(); ++i) {
			int cx = q.get(i) >> 16;
			int cy = q.get(i) & 0xFFFF;
			if (GX == cx && GY == cy) {
				System.out.println("YES");
				return;
			}
			for (int j = 0; j < 4; ++j) {
				int nx = cx + DX[j];
				int ny = cy + DY[j];
				if (nx < 0 || H <= nx || ny < 0 || W <= ny) continue;
				if (visited[nx][ny]) continue;
				if (Math.abs(B[cx][cy] - B[nx][ny]) <= 1) {
					visited[nx][ny] = true;
					q.add((nx << 16) | ny);
				}
			}
			for (int j = 0; j < 4; ++j) {
				int nx = cx + DX[j] * 2;
				int ny = cy + DY[j] * 2;
				if (nx < 0 || H <= nx || ny < 0 || W <= ny) continue;
				if (visited[nx][ny]) continue;
				if (B[cx][cy] == B[nx][ny] && B[cx][cy] > B[cx + DX[j]][cy + DY[j]]) {
					visited[nx][ny] = true;
					q.add((nx << 16) | ny);
				}
			}
		}
		System.out.println("NO");
	}
}
0