結果
| 問題 |
No.340 雪の足跡
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2020-12-29 08:00:23 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 951 ms / 1,000 ms |
| コード長 | 2,538 bytes |
| コンパイル時間 | 2,128 ms |
| コンパイル使用メモリ | 79,256 KB |
| 実行使用メモリ | 73,248 KB |
| 最終ジャッジ日時 | 2024-10-04 21:51:43 |
| 合計ジャッジ時間 | 17,890 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 32 |
ソースコード
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(" ", 3);
int w = Integer.parseInt(first[0]);
int h = Integer.parseInt(first[1]);
int[] ver = new int[h * w];
int[] hor = new int[h * w];
int n = Integer.parseInt(first[2]);
for (int i = 0; i < n; i++) {
int m = Integer.parseInt(br.readLine());
String[] line = br.readLine().split(" ", m + 1);
int prev = Integer.parseInt(line[0]);
for (int j = 0; j < m; j++) {
int x = Integer.parseInt(line[j + 1]);
int min = Math.min(prev, x);
int max = Math.max(prev, x);
if (max - min >= w) {
ver[min]++;
ver[max]--;
} else {
hor[min]++;
hor[max]--;
}
prev = x;
}
}
for (int i = 0; i < h; i++) {
for (int j = 1; j < w; j++) {
hor[i * w + j] += hor[i * w + j - 1];
}
}
for (int i = 0; i < w; i++) {
for (int j = 1; j < h; j++) {
ver[j * w + i] += ver[(j - 1) * w + i];
}
}
PriorityQueue<Path> queue = new PriorityQueue<>();
queue.add(new Path(0, 0));
int[] costs = new int[h * w];
Arrays.fill(costs, Integer.MAX_VALUE);
while (queue.size() > 0) {
Path p = queue.poll();
if (costs[p.idx] <= p.value) {
continue;
}
costs[p.idx] = p.value;
if (p.idx / w > 0 && ver[p.idx - w] > 0 && costs[p.idx - w] == Integer.MAX_VALUE) {
queue.add(new Path(p.idx - w, p.value + 1));
}
if (ver[p.idx] > 0 && costs[p.idx + w] == Integer.MAX_VALUE) {
queue.add(new Path(p.idx + w, p.value + 1));
}
if (p.idx % w > 0 && hor[p.idx - 1] > 0 && costs[p.idx - 1] == Integer.MAX_VALUE) {
queue.add(new Path(p.idx - 1, p.value + 1));
}
if (hor[p.idx] > 0 && costs[p.idx + 1] == Integer.MAX_VALUE) {
queue.add(new Path(p.idx + 1, p.value + 1));
}
}
if (costs[h * w - 1] == Integer.MAX_VALUE) {
System.out.println("Odekakedekinai..");
} else {
System.out.println(costs[h * w - 1]);
}
}
static class Path implements Comparable<Path> {
int idx;
int value;
public Path(int idx, int value) {
this.idx = idx;
this.value = value;
}
public int compareTo(Path another) {
return value - another.value;
}
}
}
tenten