結果

問題 No.340 雪の足跡
ユーザー tentententen
提出日時 2020-12-29 02:35:14
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,380 bytes
コンパイル時間 2,280 ms
コンパイル使用メモリ 80,068 KB
実行使用メモリ 86,724 KB
最終ジャッジ日時 2024-04-15 02:04:07
合計ジャッジ時間 16,465 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
61,256 KB
testcase_01 AC 127 ms
53,844 KB
testcase_02 AC 127 ms
54,200 KB
testcase_03 AC 125 ms
53,876 KB
testcase_04 AC 128 ms
53,980 KB
testcase_05 AC 126 ms
53,924 KB
testcase_06 AC 129 ms
53,924 KB
testcase_07 AC 127 ms
53,984 KB
testcase_08 AC 126 ms
53,996 KB
testcase_09 AC 126 ms
53,888 KB
testcase_10 AC 133 ms
53,980 KB
testcase_11 AC 246 ms
57,392 KB
testcase_12 AC 221 ms
57,104 KB
testcase_13 AC 238 ms
57,652 KB
testcase_14 AC 616 ms
59,640 KB
testcase_15 AC 668 ms
60,120 KB
testcase_16 AC 577 ms
59,568 KB
testcase_17 AC 701 ms
60,528 KB
testcase_18 AC 703 ms
59,900 KB
testcase_19 AC 660 ms
60,932 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 AC 170 ms
65,796 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 729 ms
65,044 KB
testcase_27 AC 442 ms
59,348 KB
testcase_28 AC 844 ms
69,356 KB
testcase_29 TLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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