結果
問題 |
No.34 砂漠の行商人
|
ユーザー |
![]() |
提出日時 | 2020-05-11 16:04:49 |
言語 | Java (openjdk 23) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,089 bytes |
コンパイル時間 | 2,538 ms |
コンパイル使用メモリ | 80,236 KB |
実行使用メモリ | 285,960 KB |
最終ジャッジ日時 | 2024-07-18 12:40:26 |
合計ジャッジ時間 | 13,547 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 8 WA * 1 TLE * 1 -- * 16 |
ソースコード
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]; ArrayList<ArrayList<HashMap<Integer, Integer>>> values = new ArrayList<>(); for (int i = 0; i < n; i++) { values.add(new ArrayList<>()); for (int j = 0; j < n; j++) { costs[i][j] = sc.nextInt(); values.get(i).add(new HashMap<>()); } } 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 (values.get(p.y).get(p.x).containsKey(p.value)) { continue; } values.get(p.y).get(p.x).put(p.value, 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)); } } int min = Integer.MAX_VALUE; for (int x : values.get(gy).get(gx).values()) { 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 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; } } } }