結果

問題 No.424 立体迷路
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-09-24 12:05:02
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 1,417 bytes
コンパイル時間 75 ms
コンパイル使用メモリ 11,084 KB
実行使用メモリ 9,036 KB
最終ジャッジ日時 2023-09-18 16:54:45
合計ジャッジ時間 1,713 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,576 KB
testcase_01 AC 19 ms
8,592 KB
testcase_02 AC 20 ms
8,576 KB
testcase_03 AC 21 ms
8,772 KB
testcase_04 AC 21 ms
8,752 KB
testcase_05 AC 20 ms
8,572 KB
testcase_06 AC 20 ms
8,576 KB
testcase_07 AC 19 ms
8,688 KB
testcase_08 AC 19 ms
8,592 KB
testcase_09 AC 19 ms
8,752 KB
testcase_10 AC 19 ms
8,584 KB
testcase_11 AC 19 ms
8,644 KB
testcase_12 AC 19 ms
8,740 KB
testcase_13 AC 19 ms
8,592 KB
testcase_14 AC 19 ms
8,580 KB
testcase_15 AC 19 ms
8,588 KB
testcase_16 AC 18 ms
8,760 KB
testcase_17 AC 19 ms
8,604 KB
testcase_18 AC 20 ms
8,724 KB
testcase_19 AC 20 ms
8,768 KB
testcase_20 AC 20 ms
8,768 KB
testcase_21 AC 20 ms
8,584 KB
testcase_22 AC 19 ms
8,768 KB
testcase_23 AC 19 ms
8,792 KB
testcase_24 AC 28 ms
9,036 KB
testcase_25 AC 28 ms
8,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import collections


DELTA_1 = [(1, 0), (-1, 0), (0, 1), (0, -1)]


def can_escape(height, width, start, goal, stage):
    def out_of_stage(r, c):
        return r < 0 or r >= height or c < 0 or c >= width
    visited = collections.defaultdict(bool)
    visited[start] = True
    q = collections.deque()
    q.append(start)
    while q:
        r0, c0 = q.popleft()
        if (r0, c0) == goal:
            return True
        for dr, dc in DELTA_1:
            r, c = r0 + dr, c0 + dc
            if out_of_stage(r, c):
                continue
            elif abs(stage[r][c] - stage[r0][c0]) <= 1 and not visited[(r, c)]:
                visited[(r, c)] = True
                q.append((r, c))
            r2, c2 = r0 + 2 * dr, c0 + 2 * dc
            if out_of_stage(r2, c2):
                continue
            elif stage[r2][c2] == stage[r0][c0] > stage[r][c]:
                if not visited[(r2, c2)]:
                    visited[(r2, c2)] = True
                    q.append((r2, c2))
    else:
        return False


def main():
    height, width = map(int, input().split())
    sx, sy, gx, gy = map(lambda x: int(x) - 1, input().split())
    start = sx, sy
    goal = gx, gy
    stage = [list(map(int, input())) for _ in range(height)]
    if can_escape(height, width, start, goal, stage):
        print("YES")
    else:
        print("NO")


if __name__ == '__main__':
    main()
0