結果

問題 No.20 砂漠のオアシス
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2015-06-24 18:46:07
言語 Java19
(openjdk 21)
結果
AC  
実行時間 444 ms / 5,000 ms
コード長 2,591 bytes
コンパイル時間 3,044 ms
コンパイル使用メモリ 75,020 KB
実行使用メモリ 62,232 KB
最終ジャッジ日時 2023-08-03 07:40:26
合計ジャッジ時間 9,289 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 136 ms
55,792 KB
testcase_01 AC 125 ms
55,792 KB
testcase_02 AC 127 ms
56,028 KB
testcase_03 AC 195 ms
58,940 KB
testcase_04 AC 197 ms
58,508 KB
testcase_05 AC 406 ms
60,312 KB
testcase_06 AC 422 ms
60,620 KB
testcase_07 AC 419 ms
60,512 KB
testcase_08 AC 444 ms
60,572 KB
testcase_09 AC 436 ms
61,036 KB
testcase_10 AC 119 ms
56,188 KB
testcase_11 AC 119 ms
56,156 KB
testcase_12 AC 201 ms
58,392 KB
testcase_13 AC 200 ms
58,720 KB
testcase_14 AC 225 ms
60,088 KB
testcase_15 AC 208 ms
58,700 KB
testcase_16 AC 252 ms
60,076 KB
testcase_17 AC 231 ms
60,232 KB
testcase_18 AC 247 ms
62,232 KB
testcase_19 AC 253 ms
60,220 KB
testcase_20 AC 190 ms
56,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Comparator;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;

public class Main {
	static int N, L, OX, OY;
	static int[][] LL;
	
	static int[][][] cost;
	static boolean[][] check;
	static int[][] mincost;
	
	static int[] dx = {1, 0, -1, 0};
	static int[] dy = {0, 1, 0, -1};
	
	public static void dik(int sx, int sy)
	{
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < N; j++) {
				check[i][j] = false;
				mincost[i][j] = Integer.MAX_VALUE / 2;
			}
		}
		
		PriorityQueue<int[]> queue = new PriorityQueue<int[]>(new Comparator<int[]>() {
			public int compare(int[] a, int[] b)
			{
				int t = mincost[a[1]][a[0]] - mincost[b[1]][b[0]];
				
				if(t > 0)
					return 1;
				else if(t == 0)
					return 0;
				else {
					return -1;
				}
			}
		});
		
		mincost[sy][sx] = 0;
		queue.offer(new int[]{sx, sy});
		check[sy][sx] = true;
		
		while (!queue.isEmpty()) {
			int[] q = queue.poll();
			int x = q[0];
			int y = q[1];
			for (int i = 0; i < 4; i++) {
				int xx = x + dx[i];
				int yy = y + dy[i];
				if( !(0 <= xx && xx <= N-1) ) continue;
				if( !(0 <= yy && yy <= N-1) ) continue;
				if(check[yy][xx] == true) continue;
				
				mincost[yy][xx] = Math.min(mincost[yy][xx], mincost[y][x] + cost[y][x][i]);
				queue.offer(new int[]{xx, yy});
				check[yy][xx] = true;
			}			
		}
	}
	
	public static int solve(){
		cost = new int[N][N][4];
		check = new boolean[N][N];
		mincost = new int[N][N];
		
		for (int y = 0; y < N; y++) {
			for (int x = 0; x < N; x++) {
				for (int i = 0; i < 4; i++) {
					int xx = x + dx[i];
					int yy = y + dy[i];
					if( !(0 <= xx && xx <= N-1) || !(0 <= yy && yy <= N-1) ) 
					{
						cost[y][x][i] = Integer.MAX_VALUE / 2;
						continue;
					}
					cost[y][x][i] = LL[yy][xx];
				}
			}
		}
		
		dik(0, 0);
		int stog = mincost[N-1][N-1];
		if(stog < L)
		{
			System.out.println("YES");
			return 0;
		}
		if(OX == 0 && OY == 0)
		{
			System.out.println("NO");
			return 0;
		}
		int stoo = mincost[OY-1][OX-1];
		dik(OX-1, OY-1);
		int otog = mincost[N-1][N-1];
		if(stoo >= L)
		{
			System.out.println("NO");
			return 0;
		}
		L = (L - stoo) * 2;
		if(otog >= L)
		{
			System.out.println("NO");
			return 0;
		}
		
		System.out.println("YES");
		return 0;
	}
	
	public static void main(String[] args){
		Scanner S = new Scanner(System.in);

		N = S.nextInt();
		L = S.nextInt();
		OX = S.nextInt();
		OY = S.nextInt();
		
		LL = new int[N][N];
		for (int y = 0; y < N; y++) {
			for (int x = 0; x < N; x++) {
				LL[y][x] = S.nextInt();
			}
		}

		solve();
	}
}
0