結果

問題 No.340 雪の足跡
ユーザー mkawa2
提出日時 2022-01-23 13:29:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 401 ms / 1,000 ms
コード長 1,956 bytes
コンパイル時間 212 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 106,880 KB
最終ジャッジ日時 2024-11-29 12:52:38
合計ジャッジ時間 9,203 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()
dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = 18446744073709551615
# inf = 4294967295
# md = 10**9+7
md = 998244353

from collections import deque

w, h, n = LI()

row = [[0]*w for _ in range(h)]
col = [[0]*h for _ in range(w)]

for _ in range(n):
    m = II()
    bb = LI()
    for k in range(m):
        i, j = divmod(bb[k], w)
        s, t = divmod(bb[k+1], w)
        if i == s:
            if j > t: j, t = t, j
            row[i][j] += 1
            row[s][t] -= 1
        else:
            if i > s: i, s = s, i
            col[j][i] += 1
            col[t][s] -= 1

for i in range(h):
    for j in range(w-1):
        row[i][j+1] += row[i][j]

for j in range(w):
    for i in range(h-1):
        col[j][i+1] += col[j][i]
# p2D(row)
# p2D(col)

def push(i, j, d):
    dist[i][j] = d
    q.append((i, j))

dist = [[-1]*w for _ in range(h)]
q = deque([(0, 0)])
dist[0][0] = 0
while q:
    i, j = q.popleft()
    d = dist[i][j]
    if j and row[i][j-1] and dist[i][j-1] == -1:
        push(i, j-1, d+1)
    if j+1 < w and row[i][j] and dist[i][j+1] == -1:
        push(i, j+1, d+1)
    if i and col[j][i-1] and dist[i-1][j] == -1:
        push(i-1, j, d+1)
    if i+1 < h and col[j][i] and dist[i+1][j] == -1:
        push(i+1, j, d+1)
    # pDB(i,j)
    # p2D(dist)

ans = dist[-1][-1]
if ans == -1: ans = "Odekakedekinai.."
print(ans)
0