結果

問題 No.424 立体迷路
ユーザー crazybbb3crazybbb3
提出日時 2016-11-27 15:08:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 88 ms / 2,000 ms
コード長 2,833 bytes
コンパイル時間 2,903 ms
コンパイル使用メモリ 77,016 KB
実行使用メモリ 54,412 KB
最終ジャッジ日時 2023-09-18 17:00:11
合計ジャッジ時間 5,537 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
51,264 KB
testcase_01 AC 61 ms
50,868 KB
testcase_02 AC 62 ms
51,204 KB
testcase_03 AC 62 ms
50,704 KB
testcase_04 AC 61 ms
50,764 KB
testcase_05 AC 61 ms
50,976 KB
testcase_06 AC 62 ms
51,132 KB
testcase_07 AC 62 ms
51,172 KB
testcase_08 AC 62 ms
50,736 KB
testcase_09 AC 61 ms
50,868 KB
testcase_10 AC 62 ms
51,344 KB
testcase_11 AC 63 ms
50,712 KB
testcase_12 AC 62 ms
51,300 KB
testcase_13 AC 62 ms
51,128 KB
testcase_14 AC 62 ms
50,728 KB
testcase_15 AC 63 ms
51,316 KB
testcase_16 AC 61 ms
51,092 KB
testcase_17 AC 66 ms
52,984 KB
testcase_18 AC 63 ms
50,988 KB
testcase_19 AC 63 ms
51,088 KB
testcase_20 AC 63 ms
50,768 KB
testcase_21 AC 60 ms
50,824 KB
testcase_22 AC 61 ms
50,832 KB
testcase_23 AC 60 ms
51,328 KB
testcase_24 AC 63 ms
50,828 KB
testcase_25 AC 88 ms
54,412 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

    static BufferedReader in;
    static PrintWriter out;
    static StringTokenizer tok;

    void solve() throws IOException {
        h = ni();
        w = ni();
        sx = ni() - 1;
        sy = ni() - 1;
        gx = ni() - 1;
        gy = ni() - 1;

        b = new int[h][w];
        for (int i = 0; i < h; i++) {
            b[i] = ns().chars().map(c -> c - '0').toArray();
        }

        used = new boolean[h][w];

        out.println(rec(sx, sy) ? "YES" : "NO");
    }

    int h, w, sx, sy, gx, gy;
    int[][] b;
    boolean[][] used;

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

    boolean rec(int i, int j) {
        if (used[i][j]) return false;
        used[i][j] = true;

        if (i == gx && j == gy) {
            return true;
        }

        for (int k = 0; k < 4; k++) {
            if (isin(i + dx[k], j + dy[k]) && Math.abs(b[i][j] - b[i + dx[k]][j + dy[k]]) <= 1 && rec(i + dx[k], j + dy[k])) {
                return true;
            }
            if (isin(i + dx2[k], j + dy2[k]) && b[i][j] == b[i + dx2[k]][j + dy2[k]] && b[i][j] > b[i + dx[k]][j + dy[k]] && rec(i + dx2[k], j + dy2[k])) {
                return true;
            }
        }

        return false;
    }

    boolean isin(int i, int j) {
        return 0 <= i && i <= h - 1 && 0 <= j && j <= w - 1;
    }

    String ns() throws IOException {
        while (!tok.hasMoreTokens()) {
            tok = new StringTokenizer(in.readLine(), " ");
        }
        return tok.nextToken();
    }

    int ni() throws IOException {
        return Integer.parseInt(ns());
    }

    long nl() throws IOException {
        return Long.parseLong(ns());
    }

    double nd() throws IOException {
        return Double.parseDouble(ns());
    }

    String[] nsa(int n) throws IOException {
        String[] res = new String[n];
        for (int i = 0; i < n; i++) {
            res[i] = ns();
        }
        return res;
    }

    int[] nia(int n) throws IOException {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = ni();
        }
        return res;
    }

    long[] nla(int n) throws IOException {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = nl();
        }
        return res;
    }

    public static void main(String[] args) throws IOException {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
        tok = new StringTokenizer("");
        Main main = new Main();
        main.solve();
        out.close();
    }
}
0