結果

問題 No.340 雪の足跡
ユーザー GrenacheGrenache
提出日時 2016-06-19 15:54:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 873 ms / 1,000 ms
コード長 4,193 bytes
コンパイル時間 3,400 ms
コンパイル使用メモリ 79,856 KB
実行使用メモリ 67,492 KB
最終ジャッジ日時 2024-04-19 13:00:44
合計ジャッジ時間 17,473 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
37,244 KB
testcase_01 AC 48 ms
37,332 KB
testcase_02 AC 49 ms
37,384 KB
testcase_03 AC 47 ms
37,308 KB
testcase_04 AC 48 ms
36,956 KB
testcase_05 AC 48 ms
36,940 KB
testcase_06 AC 47 ms
36,956 KB
testcase_07 AC 51 ms
37,208 KB
testcase_08 AC 47 ms
37,240 KB
testcase_09 AC 47 ms
37,280 KB
testcase_10 AC 48 ms
37,364 KB
testcase_11 AC 88 ms
39,228 KB
testcase_12 AC 84 ms
38,900 KB
testcase_13 AC 85 ms
39,108 KB
testcase_14 AC 196 ms
45,992 KB
testcase_15 AC 208 ms
46,436 KB
testcase_16 AC 175 ms
45,488 KB
testcase_17 AC 223 ms
46,584 KB
testcase_18 AC 213 ms
46,620 KB
testcase_19 AC 224 ms
46,344 KB
testcase_20 AC 717 ms
67,492 KB
testcase_21 AC 450 ms
48,052 KB
testcase_22 AC 94 ms
50,808 KB
testcase_23 AC 711 ms
59,540 KB
testcase_24 AC 550 ms
56,440 KB
testcase_25 AC 670 ms
57,480 KB
testcase_26 AC 242 ms
45,748 KB
testcase_27 AC 136 ms
42,404 KB
testcase_28 AC 240 ms
46,304 KB
testcase_29 AC 830 ms
53,940 KB
testcase_30 AC 686 ms
52,996 KB
testcase_31 AC 840 ms
58,496 KB
testcase_32 AC 855 ms
59,984 KB
testcase_33 AC 873 ms
60,140 KB
testcase_34 AC 867 ms
59,784 KB
testcase_35 AC 782 ms
60,384 KB
testcase_36 AC 793 ms
60,032 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder340_1 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Printer pr = new Printer(System.out);

        int w = sc.nextInt();
        int h = sc.nextInt();
        int n = sc.nextInt();

		// ====================================
//		long stime = System.currentTimeMillis();
		// ====================================

		int[][] iw = new int[h][w];
		int[][] ih = new int[h][w];

		for (int i = 0; i < n; i++) {
			int m = sc.nextInt();
			int prev = 0;
			for (int j = 0; j < m + 1; j++) {
				int b = sc.nextInt();
				if (j > 0) {
					int ph = prev / w;
					int pw = prev % w;
					int nh = b / w;
					int nw = b % w;

					if (ph == nh) {
						iw[ph][Math.min(pw, nw)]++;
						iw[ph][Math.max(pw, nw)]--;
					} else if (pw == nw) {
						ih[Math.min(ph, nh)][pw]++;
						ih[Math.max(ph, nh)][pw]--;
					} else {
					}
				}
				prev = b;
			}
		}

		// ====================================
//		long etime = System.currentTimeMillis();
//		System.out.printf("time : %d\n", etime - stime);
		// ====================================

		for (int i = 0; i < h; i++) {
			for (int j = 0; j < w - 1; j++) {
				if (j > 0) {
					iw[i][j] += iw[i][j - 1];
				}
			}
		}

		for (int j = 0; j < w; j++) {
			for (int i = 0; i < h - 1; i++) {
				if (i > 0) {
					ih[i][j] += ih[i - 1][j];
				}
			}
		}

		// ====================================
//		etime = System.currentTimeMillis();
//		System.out.printf("time : %d\n", etime - stime);
		// ====================================

		int[] dx = {1, -1, 0, 0};
		int[] dy = {0, 0, 1, -1};
		Queue<Integer> qx = new ArrayDeque<>();
		Queue<Integer> qy = new ArrayDeque<>();
		Queue<Integer> qd = new ArrayDeque<>();
		boolean[][] used = new boolean[h][w];
		qx.add(0);
		qy.add(0);
		qd.add(0);
		used[0][0] = true;

		while (!qx.isEmpty()) {
			int x = qx.remove();
			int y = qy.remove();
			int d = qd.remove();

			if (x == w - 1 && y == h - 1) {
				pr.println(d);

				// ====================================
//				etime = System.currentTimeMillis();
//				System.out.printf("time : %d\n", etime - stime);
				// ====================================

				pr.close();
				sc.close();
				return;
			}

			for (int i = 0; i < dx.length; i++) {
				int nx = x + dx[i];
				int ny = y + dy[i];

				if (nx < 0 || nx >= w || ny < 0 || ny >= h) {
					continue;
				}
				if (used[ny][nx]) {
					continue;
				}

				if (!isOK(x, y, nx, ny, ih, iw)) {
					continue;
				}

				qx.add(nx);
				qy.add(ny);
				qd.add(d + 1);
				used[ny][nx] = true;
			}
		}

		pr.println("Odekakedekinai..");

		// ====================================
//		etime = System.currentTimeMillis();
//		System.out.printf("time : %d\n", etime - stime);
		// ====================================

		pr.close();
        sc.close();
    }

	private static boolean isOK(int x, int y, int nx, int ny, int[][] ih, int[][] iw) {
		if (x == nx) {
			if (ih[Math.min(y, ny)][x] > 0) {
				return true;
			}
		} else if (y == ny) {
			if (iw[y][Math.min(x, nx)] > 0) {
				return true;
			}
		} else {

		}

		return false;
	}

	@SuppressWarnings("unused")
	private static class Scanner {
		BufferedReader br;
		Iterator<String> it;

		Scanner (InputStream in) {
			br = new BufferedReader(new InputStreamReader(in));
		}

		String next() throws RuntimeException  {
			try {
				if (it == null || !it.hasNext()) {
					it = Arrays.asList(br.readLine().split(" ")).iterator();
				}
				return it.next();
			} catch (IOException e) {
				throw new IllegalStateException();
			}
		}

		int nextInt() throws RuntimeException {
			return Integer.parseInt(next());
		}

		long nextLong() throws RuntimeException {
			return Long.parseLong(next());
		}

		float nextFloat() throws RuntimeException {
			return Float.parseFloat(next());
		}

		double nextDouble() throws RuntimeException {
			return Double.parseDouble(next());
		}

		void close() {
			try {
				br.close();
			} catch (IOException e) {
//				throw new IllegalStateException();
			}
		}
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0