結果
| 問題 | No.34 砂漠の行商人 |
| コンテスト | |
| ユーザー |
htensai
|
| 提出日時 | 2020-05-11 16:54:35 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,958 bytes |
| 記録 | |
| コンパイル時間 | 2,063 ms |
| コンパイル使用メモリ | 79,604 KB |
| 実行使用メモリ | 49,612 KB |
| 最終ジャッジ日時 | 2024-07-18 13:44:12 |
| 合計ジャッジ時間 | 9,700 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 25 WA * 1 |
ソースコード
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 sx = sc.nextInt() - 1;
int sy = sc.nextInt() - 1;
int gx = sc.nextInt() - 1;
int gy = sc.nextInt() - 1;
int[][] costs = new int[n][n];
int[][] counts = new int[n][n];
ArrayList<ArrayList<TreeMap<Integer, Integer>>> values = new ArrayList<>();
for (int i = 0; i < n; i++) {
Arrays.fill(counts[i], Integer.MAX_VALUE);
for (int j = 0; j < n; j++) {
costs[i][j] = sc.nextInt();
}
}
PriorityQueue<Path> queue = new PriorityQueue<>();
queue.add(new Path(sx, sy, 0, v));
while (queue.size() > 0) {
Path p = queue.poll();
p.value -= costs[p.y][p.x];
if (p.value <= 0) {
continue;
}
if (counts[p.y][p.x] <= p.count) {
continue;
}
counts[p.y][p.x] = p.count;
if (p.x > 0) {
queue.add(new Path(p.x - 1, p.y, p.count + 1, p.value));
}
if (p.x < n - 1) {
queue.add(new Path(p.x + 1, p.y, p.count + 1, p.value));
}
if (p.y > 0) {
queue.add(new Path(p.x, p.y - 1, p.count + 1, p.value));
}
if (p.y < n - 1) {
queue.add(new Path(p.x, p.y + 1, p.count + 1, p.value));
}
}
if (counts[gy][gx] == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(counts[gy][gx]);
}
}
static class Path implements Comparable<Path> {
int x;
int y;
int count;
int value;
public Path(int x, int y, int count, int value) {
this.x = x;
this.y = y;
this.count = count;
this.value = value;
}
public int compareTo(Path another) {
if (value == another.value) {
return count - another.count;
} else {
return another.value - value;
}
}
}
}
htensai