結果

問題 No.340 雪の足跡
ユーザー maspy
提出日時 2020-03-21 13:51:51
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,437 bytes
コンパイル時間 281 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 223,616 KB
最終ジャッジ日時 2024-12-21 16:06:33
合計ジャッジ時間 8,982 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample RE * 5
other AC * 1 RE * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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

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

INF = N + 10
dist = [INF] * N
visited = [False] * N
q = deque([0])
goal = N - 1
d = 0
while q:
    qq = []
    d += 1
    for v in q:
        for w in graph[v]:
            if visited[w]:
                continue
            if w == goal:
                print(d)
                exit()
            visited[w] = True
            qq.append(w)
    q = qq

print('Odekakedekinai..')
0