結果

問題 No.20 砂漠のオアシス
ユーザー yuki2006yuki2006
提出日時 2015-06-24 18:09:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 491 ms / 5,000 ms
コード長 3,462 bytes
コンパイル時間 4,704 ms
コンパイル使用メモリ 78,204 KB
実行使用メモリ 49,120 KB
最終ジャッジ日時 2024-04-21 06:36:19
合計ジャッジ時間 7,951 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 118 ms
41,180 KB
testcase_01 AC 123 ms
41,368 KB
testcase_02 AC 127 ms
41,516 KB
testcase_03 AC 193 ms
44,600 KB
testcase_04 AC 196 ms
44,588 KB
testcase_05 AC 389 ms
46,516 KB
testcase_06 AC 431 ms
49,120 KB
testcase_07 AC 449 ms
49,020 KB
testcase_08 AC 491 ms
49,052 KB
testcase_09 AC 441 ms
49,060 KB
testcase_10 AC 127 ms
41,200 KB
testcase_11 AC 117 ms
41,668 KB
testcase_12 AC 187 ms
43,968 KB
testcase_13 AC 190 ms
44,096 KB
testcase_14 AC 211 ms
46,340 KB
testcase_15 AC 203 ms
44,616 KB
testcase_16 AC 252 ms
47,496 KB
testcase_17 AC 230 ms
46,140 KB
testcase_18 AC 245 ms
46,008 KB
testcase_19 AC 262 ms
46,472 KB
testcase_20 AC 172 ms
42,448 KB
権限があれば一括ダウンロードができます

ソースコード

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[]>(N*N, 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});
        check[sy][sx] = true;

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

                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