結果

問題 No.424 立体迷路
ユーザー roaris
提出日時 2019-08-12 22:46:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 52 ms / 2,000 ms
コード長 1,231 bytes
コンパイル時間 137 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 68,428 KB
最終ジャッジ日時 2024-09-17 15:23:02
合計ジャッジ時間 2,050 ms
ジャッジサーバーID
(参考情報)
judge3 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def bfs(sx, sy):
    queue = deque([])
    queue.append((sx, sy))
    visited = [[-1 for _ in range(w)] for _ in range(h)]
    visited[sx][sy] = 1
    
    while len(queue) > 0:
        cx, cy = queue.popleft()
        
        for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
            nx, ny = cx + dx, cy + dy
            
            if 0 <= nx < h and 0 <= ny < w:
                if abs(S[cx][cy] - S[nx][ny]) <= 1 and visited[nx][ny] == -1:
                    visited[nx][ny] = 1
                    queue.append((nx, ny))
        
        for dx, dy in [(-2, 0), (2, 0), (0, -2), (0, 2)]:
            nx, ny = cx + dx, cy + dy
            
            if 0 <= nx < h and 0 <= ny < w:
                mx, my = (nx + cx) // 2, (ny + cy) // 2
                
                if S[cx][cy] == S[nx][ny] and S[cx][cy] > S[mx][my] and visited[nx][ny] == -1:
                    visited[nx][ny] = 1
                    queue.append((nx, ny))
    
    return visited
    
h, w = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(h)]
visited = bfs(sx-1, sy-1)

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