結果

問題 No.424 立体迷路
ユーザー thaim24thaim24
提出日時 2016-09-22 23:32:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 137 ms / 2,000 ms
コード長 3,299 bytes
コンパイル時間 3,479 ms
コンパイル使用メモリ 80,084 KB
実行使用メモリ 56,344 KB
最終ジャッジ日時 2023-09-18 16:51:40
合計ジャッジ時間 7,910 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 129 ms
55,868 KB
testcase_01 AC 125 ms
55,872 KB
testcase_02 AC 125 ms
56,120 KB
testcase_03 AC 124 ms
56,344 KB
testcase_04 AC 125 ms
56,172 KB
testcase_05 AC 121 ms
55,612 KB
testcase_06 AC 122 ms
56,140 KB
testcase_07 AC 125 ms
55,556 KB
testcase_08 AC 126 ms
56,036 KB
testcase_09 AC 127 ms
55,764 KB
testcase_10 AC 125 ms
55,848 KB
testcase_11 AC 125 ms
56,036 KB
testcase_12 AC 125 ms
55,548 KB
testcase_13 AC 126 ms
56,344 KB
testcase_14 AC 125 ms
56,212 KB
testcase_15 AC 126 ms
55,932 KB
testcase_16 AC 128 ms
55,760 KB
testcase_17 AC 133 ms
55,596 KB
testcase_18 AC 132 ms
56,012 KB
testcase_19 AC 132 ms
55,960 KB
testcase_20 AC 130 ms
55,796 KB
testcase_21 AC 124 ms
55,560 KB
testcase_22 AC 127 ms
55,688 KB
testcase_23 AC 126 ms
55,836 KB
testcase_24 AC 137 ms
55,564 KB
testcase_25 AC 134 ms
55,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class No424 {
    static final int[][] move = {
            {0, 1},
            {-1, 0},
            {0, -1},
            {1, 0}
    };
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

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

        int[] pos = new int[4];
        pos[0] = sc.nextInt();
        pos[1] = sc.nextInt();
        pos[2] = sc.nextInt();
        pos[3] = sc.nextInt();

        String[] b = new String[h];
        for (int i=0; i<h; i++) {
            b[i] = sc.next();
        }
        int[][] board = new int[h+2][w+2];
        for (int i=1; i<=h; i++) {
            for (int j=1; j<=w; j++) {
                board[i][j] = b[i-1].charAt(j-1) - '0';
            }
        }

        String result = solve(h, w, pos, board);

        System.out.println(result);
    }

    public static String solve(int h, int w, int[] pos, int[][] board) {
        boolean[][] reachable = new boolean[h+2][w+2];

        reachable[pos[0]][pos[1]] = true;
        List<Pos> list = new ArrayList<Pos>();
        list.add(new Pos(pos[0], pos[1]));

        while (!reachable[pos[2]][pos[3]] && list.size() > 0) {
            Pos current = list.remove(0);
//            System.err.println("current:" + current.toString());
            for (int i=0; i<4; i++) {
                Pos next = new Pos(current.x + move[i][0], current.y + move[i][1]);
                Pos next2 = new Pos(next.x + move[i][0], next.y + move[i][1]);
//                System.err.println("\tmove:" + move[i][0] + ", " + move[i][1] + ", next:" + next + ", next2:" + next2);

                if (next.x> h || next.x == 0
                        || next.y > w
                        || next.y == 0) {
                    continue;
                }

//                System.err.println("\tcheck start");
                if (!reachable[next.x][next.y] &&
                        Math.abs(board[current.x][current.y]-board[next.x][next.y]) <= 1) {
                    reachable[next.x][next.y] = true;
//                    System.err.println("\tboard: current=" + board[current.x][current.y] + ", next=" + board[next.x][next.y]);
                    list.add(next);
//                    System.err.println(next);
                }

//                System.err.println("\tcheck2 start");
                if (board[current.x][current.y] > board[next.x][next.y]
                        && next2.x <= h && next2.x > 0
                        && next2.y <= w && next2.y > 0
                        && !reachable[next2.x][next2.y]
                        && board[current.x][current.y] == board[next2.x][next2.y]) {
                    reachable[next2.x][next2.y] = true;
                    list.add(new Pos(next2.x, next2.y));
//                    System.err.println(next2);
                }
            }
        }

        if (reachable[pos[2]][pos[3]]) {
            return "YES";
        } else {
            return "NO";
        }
    }

    public static class Pos {
        int x;
        int y;
        public Pos(int a, int b) {
            x = a;
            y = b;
        }

        public String toString() {
            return "(" + x + ", " + y + ")";
        }
    }
}
0