結果

問題 No.340 雪の足跡
ユーザー tentententen
提出日時 2020-12-29 02:28:04
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,380 bytes
コンパイル時間 2,325 ms
コンパイル使用メモリ 79,708 KB
実行使用メモリ 87,172 KB
最終ジャッジ日時 2024-04-15 01:59:37
合計ジャッジ時間 17,716 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
61,360 KB
testcase_01 AC 129 ms
54,108 KB
testcase_02 AC 129 ms
54,532 KB
testcase_03 AC 136 ms
54,136 KB
testcase_04 AC 132 ms
54,004 KB
testcase_05 AC 130 ms
54,132 KB
testcase_06 AC 131 ms
54,088 KB
testcase_07 AC 133 ms
54,316 KB
testcase_08 AC 131 ms
54,216 KB
testcase_09 AC 133 ms
54,104 KB
testcase_10 AC 133 ms
54,008 KB
testcase_11 WA -
testcase_12 AC 206 ms
57,244 KB
testcase_13 AC 233 ms
57,576 KB
testcase_14 AC 621 ms
59,412 KB
testcase_15 AC 578 ms
59,844 KB
testcase_16 WA -
testcase_17 AC 713 ms
60,008 KB
testcase_18 AC 701 ms
60,016 KB
testcase_19 AC 742 ms
60,596 KB
testcase_20 TLE -
testcase_21 TLE -
testcase_22 AC 188 ms
66,208 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 AC 759 ms
64,548 KB
testcase_27 AC 428 ms
59,564 KB
testcase_28 AC 774 ms
68,040 KB
testcase_29 TLE -
testcase_30 TLE -
testcase_31 TLE -
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 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;
	    }
	}
}
0