結果
問題 |
No.340 雪の足跡
|
ユーザー |
![]() |
提出日時 | 2020-05-14 14:00:29 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,589 bytes |
コンパイル時間 | 2,297 ms |
コンパイル使用メモリ | 80,180 KB |
実行使用メモリ | 75,204 KB |
最終ジャッジ日時 | 2024-09-15 05:34:28 |
合計ジャッジ時間 | 23,268 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 19 TLE * 6 -- * 7 |
ソースコード
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int w = sc.nextInt(); int h = sc.nextInt(); int n = sc.nextInt(); int size = w * h; int[][] hfield = new int[h][w]; int[][] vfield = new int[w][h]; for (int i = 0; i < n; i++) { int count = sc.nextInt(); int prev = sc.nextInt(); for (int j = 0; j < count; j++) { int current = sc.nextInt(); int left = Math.min(prev, current); int right = Math.max(prev, current); if (right - left < w) { int height = left / w; hfield[height][left % w]++; hfield[height][right % w]--; } else { int width = left % w; vfield[width][left / w]++; vfield[width][right / w]--; } prev = current; } } for (int i = 0; i < h; i++) { for (int j = 1; j < w; j++) { hfield[i][j] += hfield[i][j - 1]; } } for (int i = 0; i < w; i++) { for (int j = 1; j < h; j++) { vfield[i][j] += vfield[i][j - 1]; } } int[][] costs = new int[h][w]; for (int i = 0; i < h; i++) { Arrays.fill(costs[i], Integer.MAX_VALUE); } PriorityQueue<Path> queue = new PriorityQueue<>(); queue.add(new Path(0, 0, 0)); while (queue.size() > 0) { Path p = queue.poll(); if (costs[p.height][p.width] <= p.value) { continue; } costs[p.height][p.width] = p.value; if (p.height > 0 && vfield[p.width][p.height - 1] > 0) { queue.add(new Path(p.height - 1, p.width, p.value + 1)); } if (p.height < h - 1 && vfield[p.width][p.height] > 0) { queue.add(new Path(p.height + 1, p.width, p.value + 1)); } if (p.width > 0 && hfield[p.height][p.width - 1] > 0) { queue.add(new Path(p.height, p.width - 1, p.value + 1)); } if (p.width < w - 1 && hfield[p.height][p.width] > 0) { queue.add(new Path(p.height, p.width + 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 height; int width; int value; public Path(int height, int width, int value) { this.height = height; this.width = width; this.value = value; } public int compareTo(Path another) { return value - another.value; } } }