結果

問題 No.340 雪の足跡
ユーザー mkawa2mkawa2
提出日時 2022-01-23 13:29:35
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 371 ms / 1,000 ms
コード長 1,956 bytes
コンパイル時間 305 ms
コンパイル使用メモリ 82,428 KB
実行使用メモリ 107,044 KB
最終ジャッジ日時 2024-05-07 01:09:18
合計ジャッジ時間 8,660 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,932 KB
testcase_01 AC 39 ms
55,676 KB
testcase_02 AC 40 ms
55,888 KB
testcase_03 AC 40 ms
55,432 KB
testcase_04 AC 39 ms
54,580 KB
testcase_05 AC 42 ms
54,440 KB
testcase_06 AC 40 ms
55,052 KB
testcase_07 AC 40 ms
54,884 KB
testcase_08 AC 39 ms
55,380 KB
testcase_09 AC 40 ms
55,468 KB
testcase_10 AC 40 ms
55,628 KB
testcase_11 AC 57 ms
69,200 KB
testcase_12 AC 63 ms
71,672 KB
testcase_13 AC 65 ms
73,512 KB
testcase_14 AC 97 ms
78,844 KB
testcase_15 AC 105 ms
79,076 KB
testcase_16 AC 85 ms
77,580 KB
testcase_17 AC 111 ms
79,884 KB
testcase_18 AC 107 ms
79,472 KB
testcase_19 AC 109 ms
79,568 KB
testcase_20 AC 260 ms
101,800 KB
testcase_21 AC 125 ms
77,172 KB
testcase_22 AC 82 ms
91,012 KB
testcase_23 AC 273 ms
101,660 KB
testcase_24 AC 214 ms
101,808 KB
testcase_25 AC 241 ms
102,612 KB
testcase_26 AC 94 ms
77,260 KB
testcase_27 AC 58 ms
73,100 KB
testcase_28 AC 97 ms
78,220 KB
testcase_29 AC 296 ms
92,088 KB
testcase_30 AC 264 ms
88,188 KB
testcase_31 AC 345 ms
105,624 KB
testcase_32 AC 371 ms
106,008 KB
testcase_33 AC 332 ms
106,648 KB
testcase_34 AC 370 ms
106,340 KB
testcase_35 AC 290 ms
107,044 KB
testcase_36 AC 290 ms
106,864 KB
権限があれば一括ダウンロードができます

ソースコード

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