結果

問題 No.340 雪の足跡
ユーザー GrenacheGrenache
提出日時 2016-06-19 15:54:50
言語 Java21
(openjdk 21)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,193 bytes
コンパイル時間 3,810 ms
コンパイル使用メモリ 79,716 KB
実行使用メモリ 70,460 KB
最終ジャッジ日時 2024-10-11 05:22:10
合計ジャッジ時間 20,367 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
37,048 KB
testcase_01 AC 56 ms
37,116 KB
testcase_02 AC 56 ms
37,268 KB
testcase_03 AC 57 ms
36,948 KB
testcase_04 AC 57 ms
36,776 KB
testcase_05 AC 59 ms
36,652 KB
testcase_06 AC 58 ms
37,084 KB
testcase_07 AC 57 ms
37,076 KB
testcase_08 AC 56 ms
37,248 KB
testcase_09 AC 57 ms
37,152 KB
testcase_10 AC 59 ms
37,184 KB
testcase_11 AC 106 ms
39,228 KB
testcase_12 AC 91 ms
38,596 KB
testcase_13 AC 107 ms
39,240 KB
testcase_14 AC 220 ms
45,588 KB
testcase_15 AC 232 ms
46,440 KB
testcase_16 AC 205 ms
45,244 KB
testcase_17 AC 262 ms
46,888 KB
testcase_18 AC 241 ms
46,340 KB
testcase_19 AC 266 ms
46,532 KB
testcase_20 AC 836 ms
57,748 KB
testcase_21 AC 559 ms
48,488 KB
testcase_22 AC 97 ms
50,496 KB
testcase_23 AC 843 ms
59,640 KB
testcase_24 AC 660 ms
56,880 KB
testcase_25 AC 733 ms
57,228 KB
testcase_26 AC 278 ms
45,704 KB
testcase_27 AC 149 ms
42,152 KB
testcase_28 AC 272 ms
46,096 KB
testcase_29 AC 962 ms
63,732 KB
testcase_30 AC 869 ms
53,272 KB
testcase_31 AC 971 ms
66,308 KB
testcase_32 TLE -
testcase_33 AC 916 ms
70,460 KB
testcase_34 TLE -
testcase_35 AC 999 ms
69,412 KB
testcase_36 AC 925 ms
69,584 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