結果

問題 No.34 砂漠の行商人
ユーザー tentententen
提出日時 2020-10-23 08:23:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 660 ms / 5,000 ms
コード長 2,703 bytes
コンパイル時間 3,749 ms
コンパイル使用メモリ 75,204 KB
実行使用メモリ 140,056 KB
最終ジャッジ日時 2023-09-28 15:06:38
合計ジャッジ時間 9,393 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
49,572 KB
testcase_01 AC 45 ms
49,540 KB
testcase_02 AC 64 ms
51,184 KB
testcase_03 AC 49 ms
49,496 KB
testcase_04 AC 133 ms
55,768 KB
testcase_05 AC 139 ms
56,684 KB
testcase_06 AC 100 ms
50,052 KB
testcase_07 AC 179 ms
59,480 KB
testcase_08 AC 208 ms
62,312 KB
testcase_09 AC 306 ms
86,340 KB
testcase_10 AC 484 ms
109,216 KB
testcase_11 AC 276 ms
127,920 KB
testcase_12 AC 125 ms
55,272 KB
testcase_13 AC 428 ms
140,056 KB
testcase_14 AC 620 ms
128,108 KB
testcase_15 AC 120 ms
55,696 KB
testcase_16 AC 152 ms
60,452 KB
testcase_17 AC 102 ms
53,476 KB
testcase_18 AC 87 ms
52,736 KB
testcase_19 AC 456 ms
82,084 KB
testcase_20 AC 660 ms
109,156 KB
testcase_21 AC 101 ms
53,560 KB
testcase_22 AC 247 ms
64,040 KB
testcase_23 AC 127 ms
55,320 KB
testcase_24 AC 524 ms
115,532 KB
testcase_25 AC 189 ms
61,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] first = br.readLine().split(" ", 6);
        int n = Integer.parseInt(first[0]);
        int v = Integer.parseInt(first[1]);
        int s = Integer.parseInt(first[2]) - 1 + (Integer.parseInt(first[3]) - 1) * n;
        int t = Integer.parseInt(first[4]) - 1 + (Integer.parseInt(first[5]) - 1) * n;
        v = Math.min(v, (Math.abs(s % n - t % n) + Math.abs(s / n - t / n)) * 9);
        int[] field = new int[n * n];
        for (int i = 0; i < n; i++) {
            String[] line = br.readLine().split(" ", n);
            for (int j = 0; j < n; j++) {
                field[i * n + j] = Integer.parseInt(line[j]);
            }
        }
        int[][] costs = new int[n * n][v + 1];
        for (int[] arr : costs) {
            Arrays.fill(arr, Integer.MAX_VALUE);
        }
        PriorityQueue<Path> queue = new PriorityQueue<>();
        queue.add(new Path(s, 0, v + field[s]));
        while (queue.size() > 0) {
            Path p = queue.poll();
            p.helth -= field[p.idx];
            if (p.helth <= 0 || costs[p.idx][p.helth] <= p.value) {
                continue;
            }
            costs[p.idx][p.helth] = p.value;
            int tmp = p.helth - 1;
            while (tmp >= 0 && costs[p.idx][tmp] > p.value) {
                costs[p.idx][tmp] = p.value;
                tmp--;
            }
            if (p.idx % n > 0) {
                queue.add(new Path(p.idx - 1, p.value + 1, p.helth));
            }
            if (p.idx % n < n - 1) {
                queue.add(new Path(p.idx + 1, p.value + 1, p.helth));
            }
            if (p.idx / n > 0) {
                queue.add(new Path(p.idx - n, p.value + 1, p.helth));
            }
            if (p.idx / n < n - 1) {
                queue.add(new Path(p.idx + n, p.value + 1, p.helth));
            }
        }
        int min = Integer.MAX_VALUE;
        for (int x : costs[t]) {
            min = Math.min(min, x);
        }
        if (min == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(min);
        }
     }
     
     static class Path implements Comparable<Path> {
         int idx;
         int value;
         int helth;
         
         public Path(int idx, int value, int helth) {
             this.idx = idx;
             this.value = value;
             this.helth = helth;
         }
         
         public int compareTo(Path another) {
             return value - another.value;
         }
     }
}
0