結果

問題 No.340 雪の足跡
ユーザー tentententen
提出日時 2020-10-03 19:16:08
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 2,699 bytes
コンパイル時間 2,165 ms
コンパイル使用メモリ 76,768 KB
実行使用メモリ 406,704 KB
最終ジャッジ日時 2023-09-25 05:38:20
合計ジャッジ時間 13,435 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
56,640 KB
testcase_01 AC 41 ms
49,832 KB
testcase_02 AC 41 ms
49,616 KB
testcase_03 AC 41 ms
49,428 KB
testcase_04 AC 41 ms
49,824 KB
testcase_05 AC 40 ms
49,568 KB
testcase_06 AC 41 ms
49,952 KB
testcase_07 AC 42 ms
49,452 KB
testcase_08 AC 44 ms
49,712 KB
testcase_09 AC 41 ms
49,420 KB
testcase_10 AC 42 ms
49,664 KB
testcase_11 AC 91 ms
52,416 KB
testcase_12 AC 100 ms
50,460 KB
testcase_13 AC 121 ms
54,800 KB
testcase_14 AC 521 ms
89,160 KB
testcase_15 AC 548 ms
90,748 KB
testcase_16 AC 344 ms
70,992 KB
testcase_17 AC 613 ms
105,736 KB
testcase_18 AC 583 ms
96,864 KB
testcase_19 AC 610 ms
105,308 KB
testcase_20 TLE -
testcase_21 AC 533 ms
59,408 KB
testcase_22 AC 311 ms
136,024 KB
testcase_23 TLE -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

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 n = Integer.parseInt(first[2]);
		int size = w * h;
		int[][] hImos = new int[w][h];
		int[][] wImos = new int[h][w];
		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 next = Integer.parseInt(line[j + 1]);
		        if (prev / w == next / w) {
		            int max = Math.max(prev, next);
		            int min = Math.min(prev, next);
		            wImos[min / w][min % w]++;
		            wImos[max / w][max % w]--;
		        } else {
		            int max = Math.max(prev, next);
		            int min = Math.min(prev, next);
		            hImos[min % w][min / w]++;
		            hImos[max % w][max / w]--;
		        }
		        prev = next;
		    }
		}
		ArrayList<HashSet<Integer>> graph = new ArrayList<>();
		for (int i = 0; i < size; i++) {
		    graph.add(new HashSet<>());
		}
		for (int i = 0; i < w; i++) {
		    for (int j = 1; j < h; j++) {
		        hImos[i][j] += hImos[i][j - 1];
		        if (hImos[i][j - 1] > 0) {
		            graph.get(i + (j - 1) * w).add(i + j * w);
		            graph.get(i + j * w).add(i + (j - 1) * w);
		        }
		    }
		}
		for (int i = 0; i < h; i++) {
		    for (int j = 1; j < w; j++) {
		        wImos[i][j] += wImos[i][j - 1];
		        if (wImos[i][j - 1] > 0) {
		            graph.get(i * w + j - 1).add(i * w + j);
		            graph.get(i * w + j).add(i * w + j - 1);
		        }
		    }
		}
		PriorityQueue<Path> queue = new PriorityQueue<>();
		queue.add(new Path(0, 0));
		int[] costs = new int[size];
		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;
		    for (int x : graph.get(p.idx)) {
		        queue.add(new Path(x, p.value + 1));
		    }
		}
		if (costs[size - 1] == Integer.MAX_VALUE) {
		    System.out.println("Odekakedekinai..");
		} else {
		    System.out.println(costs[size - 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