結果

問題 No.20 砂漠のオアシス
ユーザー tentententen
提出日時 2021-03-05 15:56:46
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,809 bytes
コンパイル時間 2,461 ms
コンパイル使用メモリ 79,284 KB
実行使用メモリ 49,728 KB
最終ジャッジ日時 2024-04-16 03:49:40
合計ジャッジ時間 9,676 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
41,512 KB
testcase_01 AC 150 ms
41,628 KB
testcase_02 AC 150 ms
41,488 KB
testcase_03 AC 227 ms
44,052 KB
testcase_04 AC 233 ms
44,472 KB
testcase_05 AC 466 ms
49,728 KB
testcase_06 AC 427 ms
48,164 KB
testcase_07 AC 525 ms
48,644 KB
testcase_08 AC 458 ms
48,380 KB
testcase_09 AC 507 ms
48,284 KB
testcase_10 AC 143 ms
41,532 KB
testcase_11 AC 142 ms
41,516 KB
testcase_12 AC 230 ms
43,696 KB
testcase_13 AC 225 ms
44,072 KB
testcase_14 AC 267 ms
46,240 KB
testcase_15 AC 250 ms
44,676 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 323 ms
47,808 KB
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

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();
        int oy = sc.nextInt();
        int[][] field = new int[n + 2][n + 2];
        Arrays.fill(field[0], 2000);
        Arrays.fill(field[n + 1], 2000);
        for (int i = 1; i <= n; i++) {
            field[i][0] = 2000;
            field[i][n + 1] = 2000;
            for (int j = 1; j <= n; j++) {
                field[i][j] = sc.nextInt();
            }
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(1, 1, 0));
        int[][] costs = new int[n + 2][n + 2];
        for (int[] arr : costs) {
            Arrays.fill(arr, Integer.MAX_VALUE);
        }
        while (queue.size() > 0) {
            Path p = queue.poll();
            p.value += field[p.x][p.y];
            if (costs[p.x][p.y] <= p.value || v <= p.value) {
                continue;
            }
            costs[p.x][p.y] = p.value;
            queue.add(new Path(p.x - 1, p.y, p.value));
            queue.add(new Path(p.x, p.y - 1, p.value));
            queue.add(new Path(p.x + 1, p.y, p.value));
            queue.add(new Path(p.x, p.y + 1, p.value));
        }
        if ((costs[n][n] == Integer.MAX_VALUE && ox == 0 && oy == 0) || (ox != 0 && oy != 0 && costs[ox][oy] == Integer.MAX_VALUE)) {
            System.out.println("NO");
            return;
        } else if (costs[n][n] < v) {
            System.out.println("YES");
            return;
        }
        v = (v - costs[ox][oy]) * 2;
        queue.add(new Path(ox, oy, - field[ox][oy]));
        for (int[] arr : costs) {
            Arrays.fill(arr, Integer.MAX_VALUE);
        }
        while (queue.size() > 0) {
            Path p = queue.poll();
            p.value += field[p.x][p.y];
            if (costs[p.x][p.y] <= p.value || v <= p.value) {
                continue;
            }
            costs[p.x][p.y] = p.value;
            queue.add(new Path(p.x - 1, p.y, p.value));
            queue.add(new Path(p.x, p.y - 1, p.value));
            queue.add(new Path(p.x + 1, p.y, p.value));
            queue.add(new Path(p.x, p.y + 1, p.value));
        }
        if (costs[n][n] == Integer.MAX_VALUE) {
            System.out.println("NO");
        } else {
            System.out.println("YES");
        }
    }
    
    static class Path implements Comparable<Path> {
        int x;
        int y;
        int value;
        
        public Path(int x, int y, int value) {
            this.x = x;
            this.y = y;
            this.value = value;
        }
        
        public int compareTo(Path another) {
            return value - another.value;
        }
    }
}
0