結果
| 問題 |
No.340 雪の足跡
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-12-29 02:28:04 |
| 言語 | Java (openjdk 23) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,380 bytes |
| コンパイル時間 | 2,029 ms |
| コンパイル使用メモリ | 79,512 KB |
| 実行使用メモリ | 89,784 KB |
| 最終ジャッジ日時 | 2024-10-04 09:52:31 |
| 合計ジャッジ時間 | 25,468 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 17 WA * 2 TLE * 9 -- * 4 |
ソースコード
import java.util.*;
public class Main {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int h = sc.nextInt();
int w = sc.nextInt();
int[][] ver = new int[h + 1][w + 1];
int[][] hor = new int[h + 1][w + 1];
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int m = sc.nextInt();
int prev = sc.nextInt();
for (int j = 0; j < m; j++) {
int x = sc.nextInt();
int min = Math.min(prev, x);
int max = Math.max(prev, x);
if (max - min >= w) {
ver[min / w][min % w]++;
ver[max / w + 1][max % w]--;
} else {
hor[min / w][min % w]++;
hor[max / w][max % w + 1]--;
}
prev = x;
}
}
for (int i = 0; i < h; i++) {
for (int j = 1; j < w; j++) {
hor[i][j] += hor[i][j - 1];
}
}
for (int i = 0; i < w; i++) {
for (int j = 1; j < h; j++) {
ver[j][i] += ver[j - 1][i];
}
}
PriorityQueue<Path> queue = new PriorityQueue<>();
queue.add(new Path(0, 0, 0));
int[][] costs = new int[h][w];
for (int[] arr : costs) {
Arrays.fill(arr, Integer.MAX_VALUE);
}
while (queue.size() > 0) {
Path p = queue.poll();
if (costs[p.r][p.c] <= p.value) {
continue;
}
costs[p.r][p.c] = p.value;
if (p.r > 0 && ver[p.r][p.c] > 0 && ver[p.r - 1][p.c] > 0) {
queue.add(new Path(p.r - 1, p.c, p.value + 1));
}
if (p.r < h - 1 && ver[p.r][p.c] > 0 && ver[p.r + 1][p.c] > 0) {
queue.add(new Path(p.r + 1, p.c, p.value + 1));
}
if (p.c > 0 && hor[p.r][p.c] > 0 && hor[p.r][p.c - 1] > 0) {
queue.add(new Path(p.r, p.c - 1, p.value + 1));
}
if (p.c < w - 1 && hor[p.r][p.c] > 0 && hor[p.r][p.c + 1] > 0) {
queue.add(new Path(p.r, p.c + 1, p.value + 1));
}
}
if (costs[h - 1][w - 1] == Integer.MAX_VALUE) {
System.out.println("Odekakedekinai..");
} else {
System.out.println(costs[h - 1][w - 1]);
}
}
static class Path implements Comparable<Path> {
int r;
int c;
int value;
public Path(int r, int c, int value) {
this.r = r;
this.c = c;
this.value = value;
}
public int compareTo(Path another) {
return value - another.value;
}
}
}
tenten