結果

問題 No.424 立体迷路
ユーザー Daigo HIROOKA
提出日時 2018-05-28 04:52:30
言語 Java
(openjdk 23)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 1,276 bytes
コンパイル時間 3,786 ms
コンパイル使用メモリ 77,424 KB
実行使用メモリ 41,340 KB
最終ジャッジ日時 2024-07-05 07:16:30
合計ジャッジ時間 6,209 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class No424{
	
	static int h, w, sx, sy, gx, gy;
	static String[] B;
	static int[] dx = {0, 1, 0, -1};
	static int[] dy = {1, 0, -1, 0};
	static boolean[][] visited;

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);

		h = sc.nextInt();
		w = sc.nextInt();
		sx = sc.nextInt();
		sy = sc.nextInt();
		gx = sc.nextInt();
		gy = sc.nextInt();
		B = new String[h];
		for(int i = 0; i < h; i++){
			B[i] = sc.next();
		}
		visited = new boolean[h][w];
		visited[sx-1][sy-1] = true;
		dfs(sx-1, sy-1);

		if(visited[gx-1][gy-1]) System.out.println("YES");
		else System.out.println("NO");
	}

	private static void dfs(int x, int y){
		for(int dir = 0; dir < 4; dir++){
			int x1 = x + dx[dir];
			int y1 = y + dy[dir];
			if(0 <= x1 && x1 < h && 0 <= y1 && y1 < w){
				if(Math.abs((int)B[x1].charAt(y1) - (int)B[x].charAt(y)) <= 1
				   && !visited[x1][y1]){
					visited[x1][y1] = true;
					dfs(x1, y1);
				}
			}

			int x2 = x + 2*dx[dir];
			int y2 = y + 2*dy[dir];
			if(0 <= x2 && x2 < h && 0 <= y2 && y2 < w){
				if(B[x2].charAt(y2) == B[x].charAt(y)
				   && (int)B[x1].charAt(y1) < (int)B[x].charAt(y)){
					if(!visited[x2][y2]){
						visited[x2][y2] = true;
						dfs(x2, y2);
					}
				}
			}
		}
	}
}
0