結果

問題 No.340 雪の足跡
ユーザー mkawa2mkawa2
提出日時 2022-01-23 14:02:33
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 483 ms / 1,000 ms
コード長 2,171 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 86,820 KB
実行使用メモリ 111,452 KB
最終ジャッジ日時 2023-08-19 18:50:50
合計ジャッジ時間 10,127 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,488 KB
testcase_01 AC 79 ms
71,760 KB
testcase_02 AC 80 ms
71,572 KB
testcase_03 AC 86 ms
71,508 KB
testcase_04 AC 80 ms
71,764 KB
testcase_05 AC 77 ms
71,732 KB
testcase_06 AC 82 ms
71,716 KB
testcase_07 AC 80 ms
71,480 KB
testcase_08 AC 84 ms
71,712 KB
testcase_09 AC 78 ms
71,760 KB
testcase_10 AC 84 ms
71,776 KB
testcase_11 AC 204 ms
77,240 KB
testcase_12 AC 105 ms
77,648 KB
testcase_13 AC 104 ms
77,916 KB
testcase_14 AC 145 ms
79,772 KB
testcase_15 AC 165 ms
80,032 KB
testcase_16 AC 124 ms
78,828 KB
testcase_17 AC 153 ms
80,312 KB
testcase_18 AC 145 ms
80,388 KB
testcase_19 AC 147 ms
80,436 KB
testcase_20 AC 344 ms
103,048 KB
testcase_21 AC 204 ms
78,172 KB
testcase_22 AC 113 ms
96,368 KB
testcase_23 AC 342 ms
103,232 KB
testcase_24 AC 301 ms
103,288 KB
testcase_25 AC 320 ms
103,324 KB
testcase_26 AC 139 ms
78,112 KB
testcase_27 AC 96 ms
77,232 KB
testcase_28 AC 140 ms
79,344 KB
testcase_29 AC 386 ms
93,148 KB
testcase_30 AC 294 ms
89,136 KB
testcase_31 AC 442 ms
105,996 KB
testcase_32 AC 483 ms
108,400 KB
testcase_33 AC 428 ms
111,452 KB
testcase_34 AC 475 ms
108,800 KB
testcase_35 AC 362 ms
108,584 KB
testcase_36 AC 359 ms
108,604 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

def solve():
    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)

solve()
0