結果

問題 No.340 雪の足跡
ユーザー tktk_snsn
提出日時 2021-01-14 18:17:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,402 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 107,652 KB
最終ジャッジ日時 2024-11-24 10:17:26
合計ジャッジ時間 7,215 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 23 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
input = sys.stdin.readline

h, w, n = map(int, input().split())
hori = [[0] * (w + 1) for _ in range(h)]
vert = [[0] * w for _ in range(h + 1)]

for _ in range(n):
    m = int(input())
    B = tuple(map(int, input().split()))
    for s, t in zip(B, B[1:]):
        hs, ws = divmod(s, w)
        ht, wt = divmod(t, w)
        if hs == ht:
            hori[hs][min(ws, wt)] += 1
            hori[hs][max(ws, wt)+1] -= 1
        elif ws == wt:
            vert[min(hs, ht)][ws] += 1
            vert[max(hs, ht)+1][ws] -= 1

for i in range(h):
    for j in range(w):
        hori[i][j + 1] += hori[i][j]
for j in range(w):
    for i in range(h):
        vert[i + 1][j] += vert[i][j]


def toID(x, y):
    return x * w + y

def push(x, y, d):
    ID = toID(x, y)
    if dist[ID] == -1:
        dist[ID] = d
        que.append(ID)

   
dist = [-1] * (h * w)
dist[0] = 0
que = deque([0])
while que:
    s = que.popleft()
    d = dist[s] + 1
    x, y = divmod(s, w)
    if x and vert[x][y] and vert[x - 1][y]:
        push(x - 1, y, d)
    if x + 1 < h and vert[x][y] and vert[x + 1][y]:
        push(x + 1, y, d)
    if y and hori[x][y] and hori[x][y - 1]:
        push(x, y - 1, d)
    if y + 1 < w and hori[x][y] and hori[x][y + 1]:
        push(x, y + 1, d)

goal = h * w - 1
if dist[goal] == -1:
    print("Odekakedekinai..")
else:
    print(dist[goal])


       
0