結果

問題 No.424 立体迷路
ユーザー ぴろずぴろず
提出日時 2016-09-22 22:34:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 1,371 bytes
コンパイル時間 2,118 ms
コンパイル使用メモリ 75,820 KB
実行使用メモリ 56,556 KB
最終ジャッジ日時 2023-09-18 16:44:11
合計ジャッジ時間 6,681 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
56,100 KB
testcase_01 AC 129 ms
55,712 KB
testcase_02 AC 128 ms
56,008 KB
testcase_03 AC 129 ms
55,748 KB
testcase_04 AC 129 ms
55,944 KB
testcase_05 AC 127 ms
56,116 KB
testcase_06 AC 127 ms
55,928 KB
testcase_07 AC 126 ms
55,940 KB
testcase_08 AC 126 ms
55,756 KB
testcase_09 AC 126 ms
56,176 KB
testcase_10 AC 125 ms
55,972 KB
testcase_11 AC 126 ms
55,832 KB
testcase_12 AC 126 ms
55,944 KB
testcase_13 AC 127 ms
56,076 KB
testcase_14 AC 126 ms
55,648 KB
testcase_15 AC 127 ms
55,860 KB
testcase_16 AC 129 ms
54,432 KB
testcase_17 AC 135 ms
55,904 KB
testcase_18 AC 136 ms
56,044 KB
testcase_19 AC 135 ms
55,984 KB
testcase_20 AC 134 ms
55,488 KB
testcase_21 AC 132 ms
56,556 KB
testcase_22 AC 131 ms
55,784 KB
testcase_23 AC 129 ms
55,600 KB
testcase_24 AC 141 ms
55,972 KB
testcase_25 AC 141 ms
55,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no424;

import java.util.ArrayDeque;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	static int[] D = {1,0,-1,0};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		int[][] m = new int[h][w];
		int si = sc.nextInt() - 1;
		int sj = sc.nextInt() - 1;
		int gi = sc.nextInt() - 1;
		int gj = sc.nextInt() - 1;
		for(int i=0;i<h;i++) {
			String s = sc.next();
			for(int j=0;j<w;j++) {
				m[i][j] = s.charAt(j) - '0';
			}
		}
		boolean[][] used = new boolean[h][w];
		Queue<Integer> q = new ArrayDeque<>();
		q.offer(si * 100 + sj);
		used[si][sj] = true;
		while(!q.isEmpty()) {
			int c = q.poll();
			int ci = c / 100;
			int cj = c % 100;
			for(int k=0;k<4;k++) {
				int ni = ci + D[k];
				int nj = cj + D[k^1];
				if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
					continue;
				}
				if (Math.abs(m[ci][cj] - m[ni][nj]) <= 1 && !used[ni][nj]) {
					used[ni][nj] = true;
					q.offer(ni * 100 + nj);
				}
				int ii = ni;
				int jj = nj;
				ni += D[k];
				nj += D[k^1];
				if (ni < 0 || ni >= h || nj < 0 || nj >= w) {
					continue;
				}
				if (m[ci][cj] == m[ni][nj]  && m[ci][cj] > m[ii][jj] && !used[ni][nj]) {
					used[ni][nj] = true;
					q.offer(ni * 100 + nj);
				}
			}
		}
		System.out.println(used[gi][gj] ? "YES" : "NO");
	}

}
0