結果

問題 No.34 砂漠の行商人
ユーザー tentententen
提出日時 2020-10-23 08:19:27
言語 Java19
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,524 bytes
コンパイル時間 2,914 ms
コンパイル使用メモリ 75,328 KB
実行使用メモリ 136,460 KB
最終ジャッジ日時 2023-09-28 15:06:00
合計ジャッジ時間 16,514 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
50,052 KB
testcase_01 AC 48 ms
49,548 KB
testcase_02 AC 85 ms
51,464 KB
testcase_03 AC 55 ms
49,592 KB
testcase_04 AC 364 ms
60,516 KB
testcase_05 AC 371 ms
60,520 KB
testcase_06 AC 109 ms
52,096 KB
testcase_07 AC 483 ms
60,780 KB
testcase_08 AC 581 ms
64,644 KB
testcase_09 AC 4,953 ms
136,460 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
            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