結果

問題 No.340 雪の足跡
ユーザー mkawa2mkawa2
提出日時 2022-01-23 14:03:27
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 893 ms / 1,000 ms
コード長 2,023 bytes
コンパイル時間 1,655 ms
コンパイル使用メモリ 86,680 KB
実行使用メモリ 109,124 KB
最終ジャッジ日時 2023-08-19 18:51:46
合計ジャッジ時間 15,244 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 92 ms
71,604 KB
testcase_01 AC 89 ms
71,532 KB
testcase_02 AC 91 ms
71,532 KB
testcase_03 AC 90 ms
71,452 KB
testcase_04 AC 90 ms
71,516 KB
testcase_05 AC 92 ms
71,600 KB
testcase_06 AC 90 ms
71,420 KB
testcase_07 AC 91 ms
71,500 KB
testcase_08 AC 89 ms
71,424 KB
testcase_09 AC 89 ms
71,196 KB
testcase_10 AC 90 ms
71,372 KB
testcase_11 AC 112 ms
77,184 KB
testcase_12 AC 118 ms
77,488 KB
testcase_13 AC 119 ms
77,688 KB
testcase_14 AC 195 ms
79,868 KB
testcase_15 AC 207 ms
80,124 KB
testcase_16 AC 181 ms
78,976 KB
testcase_17 AC 215 ms
80,184 KB
testcase_18 AC 216 ms
80,388 KB
testcase_19 AC 217 ms
80,252 KB
testcase_20 AC 540 ms
89,144 KB
testcase_21 AC 389 ms
78,932 KB
testcase_22 AC 94 ms
79,460 KB
testcase_23 AC 606 ms
87,728 KB
testcase_24 AC 455 ms
88,108 KB
testcase_25 AC 474 ms
88,708 KB
testcase_26 AC 171 ms
78,312 KB
testcase_27 AC 117 ms
77,496 KB
testcase_28 AC 235 ms
79,316 KB
testcase_29 AC 667 ms
96,264 KB
testcase_30 AC 513 ms
91,028 KB
testcase_31 AC 810 ms
105,108 KB
testcase_32 AC 891 ms
109,124 KB
testcase_33 AC 810 ms
105,056 KB
testcase_34 AC 893 ms
108,720 KB
testcase_35 AC 751 ms
97,412 KB
testcase_36 AC 750 ms
97,128 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 = (1 << 63)-1
# inf = (1<<31)-1
# md = 10**9+7
md = 998244353

def main():
    from collections import deque

    w, h, n = LI()

    row = [0]*h
    col = [0]*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] |= (1 << t-j)-1 << j
            else:
                if i > s: i, s = s, i
                col[j] |= (1 << s-i)-1 << i

    # for a in row: print(bin(a)[2:].zfill(w))
    # for a in col: print(bin(a)[2:].zfill(h))

    def push(ij):
        dist[ij] = d+1
        if ij == h*w-1: return True
        q.append(ij)
        return False

    dist = [-1]*h*w
    q = deque([0])
    dist[0] = 0
    while q:
        ij = q.popleft()
        d = dist[ij]
        i, j = divmod(ij, w)
        if j and row[i] >> j-1 & 1 and dist[ij-1] == -1:
            if push(ij-1): break
        if j+1 < w and row[i] >> j & 1 and dist[ij+1] == -1:
            if push(ij+1): break
        if i and col[j] >> i-1 & 1 and dist[ij-w] == -1:
            if push(ij-w): break
        if i+1 < h and col[j] >> i & 1 and dist[ij+w] == -1:
            if push(ij+w): break
        # pDB(i,j,dist)

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

main()
0