結果

問題 No.340 雪の足跡
ユーザー tentententen
提出日時 2020-12-29 02:42:43
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,822 bytes
コンパイル時間 2,616 ms
コンパイル使用メモリ 79,040 KB
実行使用メモリ 77,752 KB
最終ジャッジ日時 2024-04-15 02:12:01
合計ジャッジ時間 19,585 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 49 ms
50,536 KB
testcase_01 AC 50 ms
50,464 KB
testcase_02 AC 51 ms
50,208 KB
testcase_03 AC 50 ms
50,488 KB
testcase_04 AC 48 ms
50,296 KB
testcase_05 AC 51 ms
50,568 KB
testcase_06 AC 50 ms
50,492 KB
testcase_07 AC 50 ms
50,576 KB
testcase_08 AC 49 ms
50,516 KB
testcase_09 AC 49 ms
50,224 KB
testcase_10 AC 50 ms
50,476 KB
testcase_11 AC 85 ms
51,904 KB
testcase_12 AC 83 ms
51,716 KB
testcase_13 AC 89 ms
52,020 KB
testcase_14 AC 253 ms
56,792 KB
testcase_15 AC 262 ms
57,104 KB
testcase_16 AC 217 ms
56,620 KB
testcase_17 AC 283 ms
56,980 KB
testcase_18 AC 268 ms
57,340 KB
testcase_19 AC 291 ms
57,444 KB
testcase_20 AC 789 ms
70,296 KB
testcase_21 AC 578 ms
59,096 KB
testcase_22 AC 119 ms
64,212 KB
testcase_23 WA -
testcase_24 AC 691 ms
65,804 KB
testcase_25 AC 857 ms
73,936 KB
testcase_26 AC 252 ms
56,996 KB
testcase_27 AC 118 ms
54,540 KB
testcase_28 AC 284 ms
57,420 KB
testcase_29 TLE -
testcase_30 AC 925 ms
63,520 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 + 1][w + 1];
		int[][] hor = new int[h + 1][w + 1];
		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 / 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 && costs[p.r - 1][p.c] == Integer.MAX_VALUE) {
		        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 && costs[p.r + 1][p.c] == Integer.MAX_VALUE) {
		        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 && costs[p.r][p.c - 1] == Integer.MAX_VALUE) {
		        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 && costs[p.r][p.c + 1] == Integer.MAX_VALUE) {
		        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