結果

問題 No.340 雪の足跡
ユーザー maspymaspy
提出日時 2020-03-21 13:41:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 998 ms / 1,000 ms
コード長 1,493 bytes
コンパイル時間 536 ms
コンパイル使用メモリ 87,012 KB
実行使用メモリ 118,500 KB
最終ジャッジ日時 2023-08-23 12:43:34
合計ジャッジ時間 14,816 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,616 KB
testcase_01 AC 98 ms
71,548 KB
testcase_02 AC 93 ms
71,464 KB
testcase_03 AC 92 ms
71,124 KB
testcase_04 AC 95 ms
71,552 KB
testcase_05 AC 94 ms
71,556 KB
testcase_06 AC 94 ms
71,356 KB
testcase_07 AC 94 ms
71,552 KB
testcase_08 AC 95 ms
71,544 KB
testcase_09 AC 94 ms
71,584 KB
testcase_10 AC 96 ms
71,408 KB
testcase_11 AC 132 ms
77,512 KB
testcase_12 AC 126 ms
77,900 KB
testcase_13 AC 129 ms
77,880 KB
testcase_14 AC 193 ms
80,492 KB
testcase_15 AC 195 ms
80,440 KB
testcase_16 AC 162 ms
79,612 KB
testcase_17 AC 209 ms
81,420 KB
testcase_18 AC 213 ms
80,876 KB
testcase_19 AC 200 ms
81,212 KB
testcase_20 AC 330 ms
109,136 KB
testcase_21 AC 168 ms
80,836 KB
testcase_22 AC 115 ms
100,256 KB
testcase_23 AC 568 ms
109,004 KB
testcase_24 AC 229 ms
107,420 KB
testcase_25 AC 292 ms
109,124 KB
testcase_26 AC 167 ms
79,004 KB
testcase_27 AC 115 ms
77,512 KB
testcase_28 AC 178 ms
79,748 KB
testcase_29 AC 675 ms
103,324 KB
testcase_30 AC 537 ms
96,780 KB
testcase_31 AC 919 ms
114,512 KB
testcase_32 AC 996 ms
118,500 KB
testcase_33 AC 996 ms
116,708 KB
testcase_34 AC 998 ms
117,892 KB
testcase_35 AC 910 ms
117,284 KB
testcase_36 AC 894 ms
117,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from collections import deque

W, H, Q = map(int, readline().split())
query = readlines()

if H == W == 1:
    print(0)
    exit()

N = H * W
horizonal_max = list(range(N))
vertical_max = list(range(N))

for line in query[1::2]:
    B = map(int, line.split())
    x = next(B)
    for y in B:
        a, b = x, y
        if a > b:
            a, b = b, a
        if b - a < W:
            if horizonal_max[a] < b:
                horizonal_max[a] = b
        else:
            if vertical_max[a] < b:
                vertical_max[a] = b
        x = y

for i in range(N):
    h = horizonal_max[i]
    if h > i:
        if horizonal_max[i + 1] < h:
            horizonal_max[i + 1] = h
    v = vertical_max[i]
    if v > i:
        if vertical_max[i + W] < v:
            vertical_max[i + W] = v

INF = N + 10
dist = [INF] * N
dist[0] = 0
q = deque([0])
goal = N - 1


def gen_w(v):
    if horizonal_max[v] > v:
        yield v + 1
    if v and horizonal_max[v - 1] >= v:
        yield v - 1
    if vertical_max[v] > v:
        yield v + W
    if v >= W and vertical_max[v - W] >= v:
        yield v - W


while q:
    v = q.popleft()
    dw = dist[v] + 1

    for w in gen_w(v):
        if dist[w] <= dw:
            continue
        if w == goal:
            print(dw)
            exit()
        dist[w] = dw
        q.append(w)

print('Odekakedekinai..')
0