結果

問題 No.424 立体迷路
ユーザー aaaaasatori
提出日時 2018-02-23 18:32:46
言語 Java
(openjdk 23)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,760 bytes
コンパイル時間 3,499 ms
コンパイル使用メモリ 80,188 KB
実行使用メモリ 41,972 KB
最終ジャッジ日時 2024-07-05 07:15:52
合計ジャッジ時間 7,452 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class No424 {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int h = sc.nextInt();
		int w = sc.nextInt();
		Place start =  new Place(sc.nextInt()-1,sc.nextInt()-1);
		Place goal = new Place(sc.nextInt()-1,sc.nextInt()-1);
		int map[][] = new int[h][w];
		for(int i = 0;i < h;i++) {
			String s = sc.next();
			for(int j = 0;j < w;j++) {
				map[i][j] = Integer.parseInt("" + s.charAt(j));
			}
		}
		int dx[] = {1,0,-1,0};
		int dy[] = {0,1,0,-1};
		int dx2[] = {2,0,-2,0};
		int dy2[] = {0,2,0,-2};
		int mapprime[][] = new int[h][w]; //0なら到達不可能,1なら到達可能
		mapprime[start.x][start.y] = 1;
		Queue<Place> que = new LinkedList<Place>();
		que.offer(start);
		
		while(que.size() > 0) {
			Place cplace = que.poll();
			for(int i = 0;i < 4;i++) {
				int x1 = cplace.x + dx[i];
				int y1 = cplace.y + dy[i];
				int x2 = cplace.x + dx2[i];
				int y2 = cplace.y + dy2[i];
				if(x1 >= 0 && x1 < h && y1 >= 0 && y1 < w && mapprime[x1][y1] == 0) {
					if(Math.abs(map[cplace.x][cplace.y] - map[x1][y1]) == 1 || Math.abs(map[cplace.x][cplace.y] - map[x1][y1]) == 0) {
						mapprime[x1][y1] = 1;
						que.offer(new Place(x1,y1));
					}
				}
				
				if(x2 >= 0 && x2 < h && y2 >= 0 && y2 < w && mapprime[x2][y2] == 0) {
					if(map[cplace.x][cplace.y] == map[x2][y2] && map[x2][y2] > map[x1][y1]) {
						mapprime[x2][y2] = 1;
						que.offer(new Place(x2,y2));
					}
				}
			}
		}
		
		if(mapprime[goal.x][goal.y] == 1) {
			System.out.println("YES");
		}else {
			System.out.println("NO");
		}
	}
}
class Place{
	int x; //縦方向
	int y; //横方向
	Place(int x,int y){
		this.x = x;
		this.y = y;
	}
}
0