結果

問題 No.20 砂漠のオアシス
ユーザー こるこる
提出日時 2017-02-17 11:49:30
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 3,044 bytes
コンパイル時間 2,215 ms
コンパイル使用メモリ 79,152 KB
実行使用メモリ 60,088 KB
最終ジャッジ日時 2024-04-21 07:04:38
合計ジャッジ時間 6,048 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
49,948 KB
testcase_01 AC 55 ms
50,524 KB
testcase_02 AC 73 ms
51,332 KB
testcase_03 AC 92 ms
52,016 KB
testcase_04 AC 110 ms
51,668 KB
testcase_05 AC 556 ms
60,088 KB
testcase_06 AC 250 ms
56,608 KB
testcase_07 AC 273 ms
55,908 KB
testcase_08 AC 232 ms
56,276 KB
testcase_09 AC 249 ms
55,872 KB
testcase_10 WA -
testcase_11 AC 54 ms
49,872 KB
testcase_12 WA -
testcase_13 AC 98 ms
51,240 KB
testcase_14 AC 109 ms
51,964 KB
testcase_15 AC 102 ms
51,836 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 127 ms
52,044 KB
testcase_20 AC 75 ms
50,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;
import java.io.*;

public class Main {
    
    static NODE[][] nodes;
    
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        int N=Integer.parseInt(st.nextToken());
        int V=Integer.parseInt(st.nextToken());
        int Ox=Integer.parseInt(st.nextToken());
        int Oy=Integer.parseInt(st.nextToken());
        PriorityQueue<NODE> pq=new PriorityQueue<>(new MyComparator());
        nodes=new NODE[N][N];
        for(int i=0;i<N;i++){
            st=new StringTokenizer(br.readLine());
            for(int j=0;j<N;j++){
                nodes[i][j]=new NODE();
                nodes[i][j].x=i; nodes[i][j].y=j;
                nodes[i][j].level=Integer.parseInt(st.nextToken());
                nodes[i][j].dist=-1;
            }
        }nodes[0][0].dist=0;
        pq.offer(nodes[0][0]);
        NODE node;
        while(!pq.isEmpty()){
            node=pq.poll();
            //System.out.println(node.x+" "+node.y);
            if(node.x>0){ update(pq,node,node.x-1,node.y); }
            if(node.x<N-1){ update(pq,node,node.x+1,node.y); }
            if(node.y>0){ update(pq,node,node.x,node.y-1); }
            if(node.y<N-1){ update(pq,node,node.x,node.y+1); }
        }int start_to_end=V-nodes[N-1][N-1].dist;
        if(Ox==0&&Oy==0){ System.out.println((start_to_end>0) ? "YES":"NO"); }
        else{
            int start_to_O=V-nodes[Ox-1][Oy-1].dist;
            if(start_to_O<=0){
                System.out.println("NO");
                System.exit(0);
            }else{ start_to_O*=2; }
            for(int i=0;i<N;i++){
                for(int j=0;j<N;j++){
                    nodes[i][j].dist=-1;
                }
            }
            nodes[Ox-1][Oy-1].dist=0;
            pq.offer(nodes[Ox-1][Oy-1]);
            while(!pq.isEmpty()){
                node=pq.poll();
                //System.out.println(node.x+" "+node.y);
                if(node.x>0){ update(pq,node,node.x-1,node.y); }
                if(node.x<N-1){ update(pq,node,node.x+1,node.y); }
                if(node.y>0){ update(pq,node,node.x,node.y-1); }
                if(node.y<N-1){ update(pq,node,node.x,node.y+1); }
            }int O_to_end=start_to_O-nodes[N-1][N-1].dist;
            System.out.println((O_to_end>0) ? "YES":"NO");
        }
    }
    
    static void update(PriorityQueue<NODE> pq,NODE node,int x,int y){
        if(nodes[x][y].dist==-1 || nodes[x][y].dist>node.dist+nodes[x][y].level){
            nodes[x][y].dist=nodes[x][y].level+node.dist;
            if(!pq.contains(nodes[x][y])){ pq.offer(nodes[x][y]); }
        }
    }

}

class MyComparator implements Comparator{
    public int compare(Object arg0,Object arg1){
        NODE x=(NODE) arg0;
        NODE y=(NODE) arg1;
        if(x.dist<y.dist) return -1;
        else if(x.dist>y.dist) return 1;
        else return 0;
    }
}

class NODE{
    int level,dist,x,y;
}
0