結果

問題 No.124 門松列(3)
ユーザー GrenacheGrenache
提出日時 2016-06-12 01:54:17
言語 Java21
(openjdk 21)
結果
AC  
実行時間 101 ms / 5,000 ms
コード長 3,982 bytes
コンパイル時間 3,552 ms
コンパイル使用メモリ 80,224 KB
実行使用メモリ 54,228 KB
最終ジャッジ日時 2024-04-17 18:55:08
合計ジャッジ時間 5,977 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
52,348 KB
testcase_01 AC 45 ms
50,292 KB
testcase_02 AC 44 ms
50,540 KB
testcase_03 AC 101 ms
54,228 KB
testcase_04 AC 78 ms
51,964 KB
testcase_05 AC 84 ms
51,932 KB
testcase_06 AC 45 ms
50,616 KB
testcase_07 AC 44 ms
50,392 KB
testcase_08 AC 44 ms
50,412 KB
testcase_09 AC 45 ms
50,492 KB
testcase_10 AC 43 ms
50,644 KB
testcase_11 AC 44 ms
50,188 KB
testcase_12 AC 43 ms
50,360 KB
testcase_13 AC 43 ms
50,624 KB
testcase_14 AC 45 ms
50,600 KB
testcase_15 AC 44 ms
50,132 KB
testcase_16 AC 51 ms
50,572 KB
testcase_17 AC 46 ms
50,304 KB
testcase_18 AC 44 ms
50,304 KB
testcase_19 AC 46 ms
50,144 KB
testcase_20 AC 49 ms
50,516 KB
testcase_21 AC 45 ms
50,504 KB
testcase_22 AC 46 ms
50,176 KB
testcase_23 AC 57 ms
50,688 KB
testcase_24 AC 67 ms
53,296 KB
testcase_25 AC 76 ms
51,356 KB
testcase_26 AC 50 ms
50,592 KB
testcase_27 AC 48 ms
50,472 KB
testcase_28 AC 89 ms
51,924 KB
testcase_29 AC 88 ms
52,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.*;


public class Main_yukicoder124 {

    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[][] m = new int[h][w];
        for (int i = 0; i < h; i++) {
        	for (int j = 0; j < w; j++) {
        		m[i][j] = sc.nextInt();
        	}
        }

        int[] dx = {1, -1, 0, 0};
        int[] dy = {0, 0, 1, -1};

        Queue<Integer> qx = new ArrayDeque<>();
        Queue<Integer> qy = new ArrayDeque<>();
        Queue<Integer> prev = new ArrayDeque<>();
        Queue<Integer> step = new ArrayDeque<>();
        List<List<Set<Integer>>> used = new ArrayList<>();

        for (int i = 0; i < h; i++) {
        	List<Set<Integer>> tmp = new ArrayList<>();
        	for (int j = 0; j < w; j++) {
        		tmp.add(new HashSet<>());
        	}
        	used.add(tmp);
        }

        qx.add(0);
        qy.add(0);
        prev.add(enc(-1, -1, -1, -1));
        step.add(0);
        while (!qx.isEmpty()) {
        	int x = qx.remove();
        	int y = qy.remove();
        	int p = prev.remove();
        	int s = step.remove();

    		int x1 = decx1(p);
    		int y1 = decy1(p);
//    		int x2 = decx2(p);
//    		int y2 = decy2(p);

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

        		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 (x1 != 255) {
        			if (m[ny][nx] == m[y][x] || m[ny][nx] == m[y1][x1]) {
        				continue;
        			}
        			if (m[y][x] < m[y1][x1] && m[ny][nx] < m[y][x]) {
        				continue;
        			}
        			if (m[y][x] > m[y1][x1] && m[ny][nx] > m[y][x]) {
        				continue;
        			}
        		}

        		int ptmp = enc(x, y, x1, y1);
        		if (used.get(ny).get(nx).contains(ptmp)) {
        			continue;
        		}

        		qx.add(nx);
        		qy.add(ny);
        		prev.add(ptmp);
        		step.add(s + 1);
        		used.get(ny).get(nx).add(ptmp);
        	}
        }

        pr.println(-1);

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

	private static int decx1(int p) {
		return (p >> 24) & 0xff;
	}

	private static int decy1(int p) {
		return (p >> 16) & 0xff;
	}

//	private static int decx2(int p) {
//		return (p >> 8) & 0xff;
//	}

//	private static int decy2(int p) {
//		return p & 0xff;
//	}

	private static int enc(int x1, int y1, int x2, int y2) {
		int ret = 0;
		ret |= x1 << 24;
		ret |= y1 << 16;
		ret |= x2 << 8;
		ret |= y2;

		return ret;
	}

	@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