結果

問題 No.20 砂漠のオアシス
ユーザー tenten
提出日時 2021-03-05 15:56:46
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 2,809 bytes
コンパイル時間 2,322 ms
コンパイル使用メモリ 78,736 KB
実行使用メモリ 59,584 KB
最終ジャッジ日時 2024-10-06 14:57:00
合計ジャッジ時間 9,172 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17 WA * 4
権限があれば一括ダウンロードができます

ソースコード

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