結果

問題 No.20 砂漠のオアシス
ユーザー htensaihtensai
提出日時 2020-05-14 17:41:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 456 ms / 5,000 ms
コード長 2,778 bytes
コンパイル時間 2,321 ms
コンパイル使用メモリ 75,020 KB
実行使用メモリ 62,864 KB
最終ジャッジ日時 2023-10-13 16:17:27
合計ジャッジ時間 9,274 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
55,932 KB
testcase_01 AC 120 ms
56,084 KB
testcase_02 AC 115 ms
55,680 KB
testcase_03 AC 185 ms
58,228 KB
testcase_04 AC 184 ms
59,104 KB
testcase_05 AC 394 ms
62,864 KB
testcase_06 AC 426 ms
60,380 KB
testcase_07 AC 435 ms
60,896 KB
testcase_08 AC 413 ms
60,488 KB
testcase_09 AC 456 ms
60,748 KB
testcase_10 AC 114 ms
56,084 KB
testcase_11 AC 110 ms
55,920 KB
testcase_12 AC 183 ms
58,484 KB
testcase_13 AC 192 ms
58,432 KB
testcase_14 AC 211 ms
60,120 KB
testcase_15 AC 201 ms
59,484 KB
testcase_16 AC 267 ms
60,816 KB
testcase_17 AC 239 ms
60,280 KB
testcase_18 AC 252 ms
60,316 KB
testcase_19 AC 277 ms
60,356 KB
testcase_20 AC 175 ms
56,336 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
	public static void main (String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int v = sc.nextInt();
		int ox = sc.nextInt() - 1;
		int oy = sc.nextInt() - 1;
		int[][] field = new int[n][n];
		for (int i = 0; i < n; i++) {
		    for (int j = 0; j < n; j++) {
		        field[i][j] = sc.nextInt();
		    }
		}
		int[][] costs = new int[n][n];
		for (int i = 0; i < n; i++) {
		    Arrays.fill(costs[i], Integer.MAX_VALUE);
		}
		PriorityQueue<Path> queue = new PriorityQueue<>();
		queue.add(new Path(0, 0, -field[0][0]));
		while (queue.size() > 0) {
		    Path p = queue.poll();
		    if (costs[p.row][p.col] <= p.value + field[p.row][p.col]) {
		        continue;
		    }
		    costs[p.row][p.col] = p.value + field[p.row][p.col];
		    if (p.row > 0) {
		        queue.add(new Path(p.row - 1, p.col, p.value + field[p.row][p.col]));
		    }
		    if (p.row < n - 1) {
		        queue.add(new Path(p.row + 1, p.col, p.value + field[p.row][p.col]));
		    }
		    if (p.col > 0) {
		        queue.add(new Path(p.row, p.col - 1, p.value + field[p.row][p.col]));
		    }
		    if (p.col < n - 1) {
		        queue.add(new Path(p.row, p.col + 1, p.value + field[p.row][p.col]));
		    }
		}
		if (costs[n - 1][n - 1] < v) {
		    System.out.println("YES");
		    return;
		}
		if (ox < 0 && oy < 0) {
		    System.out.println("NO");
		    return;
		}
		if (costs[oy][ox] >= v) {
		    System.out.println("NO");
		    return;
		}
		v -= costs[oy][ox];
		v *= 2;
		for (int i = 0; i < n; i++) {
		    Arrays.fill(costs[i], Integer.MAX_VALUE);
		}
		queue.add(new Path(oy, ox, -field[oy][ox]));
		while (queue.size() > 0) {
		    Path p = queue.poll();
		    if (costs[p.row][p.col] <= p.value + field[p.row][p.col]) {
		        continue;
		    }
		    costs[p.row][p.col] = p.value + field[p.row][p.col];
		    if (p.row > 0) {
		        queue.add(new Path(p.row - 1, p.col, p.value + field[p.row][p.col]));
		    }
		    if (p.row < n - 1) {
		        queue.add(new Path(p.row + 1, p.col, p.value + field[p.row][p.col]));
		    }
		    if (p.col > 0) {
		        queue.add(new Path(p.row, p.col - 1, p.value + field[p.row][p.col]));
		    }
		    if (p.col < n - 1) {
		        queue.add(new Path(p.row, p.col + 1, p.value + field[p.row][p.col]));
		    }
		}
		if (costs[n - 1][n - 1] < v) {
		    System.out.println("YES");
		} else {
		    System.out.println("NO");
		}
	}
	
	static class Path implements Comparable<Path> {
	    int row;
	    int col;
	    int value;
	    
	    public Path(int row, int col, int value) {
	        this.row = row;
	        this.col = col;
	        this.value = value;
	    }
	    
	    public int compareTo(Path another) {
	        return value - another.value;
	    }
	}
}
0