結果

問題 No.340 雪の足跡
ユーザー tentententen
提出日時 2020-12-29 08:00:23
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,538 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 80,096 KB
実行使用メモリ 73,616 KB
最終ジャッジ日時 2024-04-15 07:13:49
合計ジャッジ時間 20,537 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
50,392 KB
testcase_01 AC 53 ms
50,092 KB
testcase_02 AC 54 ms
50,548 KB
testcase_03 AC 53 ms
50,520 KB
testcase_04 AC 53 ms
50,404 KB
testcase_05 AC 53 ms
50,304 KB
testcase_06 AC 54 ms
50,564 KB
testcase_07 AC 54 ms
50,100 KB
testcase_08 AC 55 ms
50,400 KB
testcase_09 AC 54 ms
50,376 KB
testcase_10 AC 54 ms
50,072 KB
testcase_11 AC 93 ms
52,044 KB
testcase_12 AC 96 ms
51,948 KB
testcase_13 AC 95 ms
51,896 KB
testcase_14 AC 267 ms
56,272 KB
testcase_15 AC 281 ms
56,648 KB
testcase_16 AC 226 ms
56,336 KB
testcase_17 AC 315 ms
56,612 KB
testcase_18 AC 279 ms
56,716 KB
testcase_19 AC 320 ms
56,620 KB
testcase_20 AC 754 ms
71,416 KB
testcase_21 AC 496 ms
58,756 KB
testcase_22 AC 106 ms
60,920 KB
testcase_23 AC 794 ms
71,100 KB
testcase_24 AC 662 ms
65,068 KB
testcase_25 AC 780 ms
70,492 KB
testcase_26 AC 264 ms
57,228 KB
testcase_27 AC 137 ms
54,072 KB
testcase_28 AC 284 ms
57,204 KB
testcase_29 TLE -
testcase_30 AC 877 ms
62,772 KB
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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;
	    }
	}
}
0