結果

問題 No.340 雪の足跡
ユーザー tktk_snsn
提出日時 2021-01-15 12:27:18
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,301 bytes
コンパイル時間 363 ms
コンパイル使用メモリ 81,964 KB
実行使用メモリ 108,488 KB
最終ジャッジ日時 2024-11-25 10:13:43
合計ジャッジ時間 9,529 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 for _ in range(h)]
vert = [[0] * w for _ in range(h)]


for _ in range(n):
    m = int(input())
    bs, *B = tuple(map(int, input().split()))
    sx, sy = divmod(bs, w)
    for bt in B:
        tx, ty = divmod(bt, w)
        if sx == tx:
            hori[sx][min(sy, ty)] += 1
            hori[sx][max(sy, ty)] -= 1
        elif sy == ty:
            vert[min(sx, tx)][sy] += 1
            vert[max(sx, tx)][sy] -= 1
        sx, sy = tx, ty
   
for i in range(h):
    for j in range(w - 1):
        hori[i][j + 1] += hori[i][j]
for j in range(w):
    for i in range(h - 1):
        vert[i + 1][j] += vert[i][j]

dist = [-1] * (h * w)
dist[0] = 0
que = deque([0])



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

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

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