結果

問題 No.424 立体迷路
ユーザー uafr_csuafr_cs
提出日時 2016-09-22 22:37:38
言語 Java19
(openjdk 21)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,881 bytes
コンパイル時間 2,408 ms
コンパイル使用メモリ 75,792 KB
実行使用メモリ 56,144 KB
最終ジャッジ日時 2023-09-18 16:43:50
合計ジャッジ時間 7,202 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
55,532 KB
testcase_01 AC 132 ms
55,736 KB
testcase_02 AC 135 ms
53,848 KB
testcase_03 AC 130 ms
55,732 KB
testcase_04 AC 133 ms
56,080 KB
testcase_05 AC 131 ms
55,728 KB
testcase_06 AC 131 ms
55,896 KB
testcase_07 AC 130 ms
55,896 KB
testcase_08 AC 133 ms
55,500 KB
testcase_09 AC 128 ms
55,728 KB
testcase_10 AC 130 ms
55,948 KB
testcase_11 AC 132 ms
55,924 KB
testcase_12 AC 128 ms
55,896 KB
testcase_13 AC 133 ms
55,812 KB
testcase_14 AC 137 ms
55,764 KB
testcase_15 AC 134 ms
55,840 KB
testcase_16 AC 135 ms
55,732 KB
testcase_17 AC 138 ms
55,928 KB
testcase_18 AC 138 ms
55,852 KB
testcase_19 AC 139 ms
56,072 KB
testcase_20 AC 138 ms
56,044 KB
testcase_21 AC 129 ms
55,768 KB
testcase_22 AC 133 ms
55,692 KB
testcase_23 AC 132 ms
56,144 KB
testcase_24 AC 144 ms
55,584 KB
testcase_25 AC 145 ms
55,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static int[][] move_dirs = {
		{0,  1},
		{0, -1},
		{ 1, 0},
		{-1, 0}
	};
	
	public static boolean in_range(int x, int y, int w, int h){
		return 0 <= x && x < w && 0 <= y && y < h;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		final int h = sc.nextInt();
		final int w = sc.nextInt();
		
		final int sy = sc.nextInt() - 1;
		final int sx = sc.nextInt() - 1;
		final int gy = sc.nextInt() - 1;
		final int gx = sc.nextInt() - 1;
		
		int[][] heights = new int[h][w];
		
		for(int i = 0; i < h; i++){
			final char[] in = sc.next().toCharArray();
			
			for(int j = 0; j < w; j++){
				heights[i][j] = Character.getNumericValue(in[j]);
			}
		}
		
		boolean[][] visited = new boolean[h][w];
		
		LinkedList<Integer> x_queue = new LinkedList<Integer>();
		LinkedList<Integer> y_queue = new LinkedList<Integer>();
		x_queue.add(sx); y_queue.add(sy);
		visited[sy][sx] = true;
		
		while(!x_queue.isEmpty()){
			final int x = x_queue.poll();
			final int y = y_queue.poll();
			
			for(int[] move : move_dirs){
				final int nx = x + move[0];
				final int ny = y + move[1];
				final int nnx = x + move[0] * 2;
				final int nny = y + move[1] * 2;
				
				if(!in_range(nx, ny, w, h)){
					continue;
				}
				
				if(!visited[ny][nx] && Math.abs(heights[y][x] - heights[ny][nx]) <= 1){
					visited[ny][nx] = true;
					x_queue.add(nx); y_queue.add(ny);
				}
				
				if(!in_range(nnx, nny, w, h)){
					continue;
				}
				
				if(!visited[nny][nnx] && heights[nny][nnx] == heights[y][x] && heights[ny][nx] < heights[y][x]){
					visited[nny][nnx] = true;
					x_queue.add(nnx); y_queue.add(nny);
				}
			}
		}
		
		System.out.println(visited[gy][gx] ? "YES" : "NO");
	}

}
0