結果

問題 No.424 立体迷路
ユーザー rlangevinrlangevin
提出日時 2023-02-08 12:37:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 138 ms / 2,000 ms
コード長 1,584 bytes
コンパイル時間 283 ms
コンパイル使用メモリ 87,192 KB
実行使用メモリ 78,072 KB
最終ジャッジ日時 2023-09-20 05:12:10
合計ジャッジ時間 4,514 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 97 ms
71,356 KB
testcase_01 AC 96 ms
71,356 KB
testcase_02 AC 98 ms
71,596 KB
testcase_03 AC 97 ms
71,572 KB
testcase_04 AC 100 ms
71,576 KB
testcase_05 AC 98 ms
71,796 KB
testcase_06 AC 99 ms
71,420 KB
testcase_07 AC 99 ms
71,420 KB
testcase_08 AC 98 ms
71,712 KB
testcase_09 AC 97 ms
71,628 KB
testcase_10 AC 96 ms
71,628 KB
testcase_11 AC 100 ms
71,576 KB
testcase_12 AC 100 ms
71,312 KB
testcase_13 AC 98 ms
71,748 KB
testcase_14 AC 97 ms
71,464 KB
testcase_15 AC 97 ms
71,424 KB
testcase_16 AC 96 ms
71,360 KB
testcase_17 AC 138 ms
77,756 KB
testcase_18 AC 136 ms
77,724 KB
testcase_19 AC 134 ms
78,072 KB
testcase_20 AC 130 ms
77,764 KB
testcase_21 AC 97 ms
71,668 KB
testcase_22 AC 99 ms
71,680 KB
testcase_23 AC 96 ms
71,456 KB
testcase_24 AC 119 ms
77,796 KB
testcase_25 AC 121 ms
77,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

def bfs(G, s, N):
    Q = deque([])
    dist = [-1] * N
    par = [-1] * N
    sz = [1] * N
    dist[s] = 0
    rev = [0] * N
    for u in G[s]:
        par[u] = s
        dist[u] = 1
        sz[u] += sz[s]
        Q.append(u)
 
    while Q:
        u = Q.popleft()
        for v in G[u]:
            if dist[v] != -1:
                continue
            if v < u:
                rev[v] = rev[u] + 1
            else:
                rev[v] = rev[u]
            dist[v] = dist[u] + 1
            par[v] = u
            sz[v] += sz[u]
            Q.append(v)
            
    return dist

def f(h,w):
    return h * W + w

H, W = map(int, input().split())
sx, sy, gx, gy = map(int, input().split())
sx, sy, gx, gy = sx - 1, sy - 1, gx - 1, gy - 1
B = []
for i in range(H):
    b = list(input())
    b = list(map(int, b))
    B.append(b)

G = [[] for i in range(H * W)]
dx = [1, 0, -1, 0, 2, 0, -2, 0]
dy = [0, 1, 0, -1, 0, 2, 0, -2]
for i in range(H):
    for j in range(W):
        for k in range(8):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x > H - 1 or y < 0 or y > W - 1:
                continue
            if k <= 3:
                if abs(B[i][j] - B[x][y]) <= 1:
                    G[f(i, j)].append(f(x, y))
            else:
                if abs(B[i][j] - B[x][y]) == 0:
                    mx, my = i + dx[k]//2, j + dy[k]//2
                    if B[mx][my] < B[x][y]:
                        G[f(i, j)].append(f(x, y))

D = bfs(G, f(sx, sy), H * W)
print("YES") if D[f(gx, gy)] != -1 else print("NO")
0