結果

問題 No.20 砂漠のオアシス
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2015-06-24 17:49:21
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,564 bytes
コンパイル時間 2,588 ms
コンパイル使用メモリ 74,804 KB
実行使用メモリ 127,192 KB
最終ジャッジ日時 2023-08-03 07:38:50
合計ジャッジ時間 11,325 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
60,504 KB
testcase_01 AC 140 ms
55,896 KB
testcase_02 AC 146 ms
55,740 KB
testcase_03 AC 225 ms
58,768 KB
testcase_04 AC 244 ms
60,400 KB
testcase_05 AC 478 ms
60,964 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます

ソースコード

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});
		
		while (!queue.isEmpty()) {
			int[] q = queue.poll();
			int x = q[0];
			int y = q[1];
			check[y][x] = true;
			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});
			}			
		}
	}
	
	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