結果
| 問題 |
No.34 砂漠の行商人
|
| コンテスト | |
| ユーザー |
htensai
|
| 提出日時 | 2020-06-12 18:32:34 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 481 ms / 5,000 ms |
| コード長 | 1,996 bytes |
| コンパイル時間 | 2,563 ms |
| コンパイル使用メモリ | 78,988 KB |
| 実行使用メモリ | 66,268 KB |
| 最終ジャッジ日時 | 2024-06-24 04:02:58 |
| 合計ジャッジ時間 | 7,002 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 26 |
ソースコード
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 sc = Integer.parseInt(first[2]) - 1;
int sr = Integer.parseInt(first[3]) - 1;
int gc = Integer.parseInt(first[4]) - 1;
int gr = Integer.parseInt(first[5]) - 1;
int[][] field = new int[n][n];
int[][] costs = 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][j] = line[j].charAt(0) - '0';
}
Arrays.fill(costs[i], Integer.MAX_VALUE);
}
PriorityQueue<Path> queue = new PriorityQueue<>();
queue.add(new Path(sr, sc, -field[sr][sc], 0));
while (queue.size() > 0) {
Path p = queue.poll();
if (p.row < 0 || p.row >= n || p.col < 0 || p.col >= n) {
continue;
}
p.value += field[p.row][p.col];
if (p.value >= v || costs[p.row][p.col] <= p.count) {
continue;
}
costs[p.row][p.col] = p.count;
queue.add(new Path(p.row + 1, p.col, p.value, p.count + 1));
queue.add(new Path(p.row - 1, p.col, p.value, p.count + 1));
queue.add(new Path(p.row, p.col + 1, p.value, p.count + 1));
queue.add(new Path(p.row, p.col - 1, p.value, p.count + 1));
}
if (costs[gr][gc] == Integer.MAX_VALUE) {
System.out.println(-1);
} else {
System.out.println(costs[gr][gc]);
}
}
static class Path implements Comparable<Path> {
int row;
int col;
int value;
int count;
public Path(int row, int col, int value, int count) {
this.row = row;
this.col = col;
this.value = value;
this.count = count;
}
public int compareTo(Path another) {
return value - another.value;
}
}
}
htensai