結果

問題 No.424 立体迷路
コンテスト
ユーザー mlihua09
提出日時 2020-09-12 13:07:47
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 1,530 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 568 ms
コンパイル使用メモリ 85,760 KB
実行使用メモリ 66,688 KB
最終ジャッジ日時 2026-06-05 09:29:00
合計ジャッジ時間 2,880 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code


h, w = map(int, input().split())

sx, sy, gx, gy = map(int, input().split())

B = [[int(i) for i in input()] for i in range(h)]

visited = [[0] * w + [1, 1] for i in range(h)] + [[1] * (w + 2)] * 2

from collections import deque
que = deque([sx - 1, sy - 1])
visited[sx - 1][sy - 1] = 1

while que:
    q1, q2 = que.popleft(), que.popleft()
    h = B[q1][q2]
    
    if visited[q1 + 1][q2] == 0 and abs(B[q1 + 1][q2] - h) < 2:
        visited[q1 + 1][q2] = 1
        que.extend([q1 + 1, q2])
    if visited[q1 - 1][q2] == 0 and abs(B[q1 - 1][q2] - h) < 2:
        visited[q1 - 1][q2] = 1
        que.extend([q1 - 1, q2])
    if visited[q1][q2 + 1] == 0 and abs(B[q1][q2 + 1] - h) < 2:
        visited[q1][q2 + 1] = 1
        que.extend([q1, q2 + 1])
    if visited[q1][q2 - 1] == 0 and abs(B[q1][q2 - 1] - h) < 2:
        visited[q1][q2 - 1] = 1
        que.extend([q1, q2 - 1])
    if visited[q1 + 2][q2] == 0 and B[q1 + 2][q2] == h and B[q1 + 1][q2] < h:
        visited[q1 + 2][q2] = 1
        que.extend([q1 + 2, q2])
    if visited[q1 - 2][q2] == 0 and B[q1 - 2][q2] == h and B[q1 - 1][q2] < h:
        visited[q1 - 2][q2] = 1
        que.extend([q1 - 2, q2])
    if visited[q1][q2 + 2] == 0 and B[q1][q2 + 2] == h and B[q1][q2 + 1] < h:
        visited[q1][q2 + 2] = 1
        que.extend([q1, q2 + 2])
    if visited[q1][q2 - 2] == 0 and B[q1][q2 - 2] == h and B[q1][q2 - 1] < h:
        visited[q1][q2 - 2] = 1
        que.extend([q1, q2 - 2])

if visited[gx - 1][gy - 1]:
    print('YES')
else:
    print('NO')
        
0