結果
| 問題 | No.34 砂漠の行商人 |
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-10-23 08:14:53 |
| 言語 | Java (openjdk 23) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,180 bytes |
| 記録 | |
| コンパイル時間 | 3,033 ms |
| コンパイル使用メモリ | 79,736 KB |
| 実行使用メモリ | 138,420 KB |
| 最終ジャッジ日時 | 2024-07-21 09:47:31 |
| 合計ジャッジ時間 | 18,108 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 9 TLE * 2 -- * 15 |
ソースコード
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 s = sc.nextInt() - 1 + (sc.nextInt() - 1) * n;
int t = sc.nextInt() - 1 + (sc.nextInt() - 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 * n; i++) {
field[i] = sc.nextInt();
}
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;
}
}
}
tenten