結果
| 問題 | 
                            No.340 雪の足跡
                             | 
                    
| コンテスト | |
| ユーザー | 
                             maspy
                         | 
                    
| 提出日時 | 2020-03-21 13:53:57 | 
| 言語 | PyPy3  (7.3.15)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 721 ms / 1,000 ms | 
| コード長 | 1,410 bytes | 
| コンパイル時間 | 221 ms | 
| コンパイル使用メモリ | 82,176 KB | 
| 実行使用メモリ | 223,920 KB | 
| 最終ジャッジ日時 | 2024-12-21 16:08:24 | 
| 合計ジャッジ時間 | 9,646 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 5 | 
| other | AC * 32 | 
ソースコード
#!/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 = [0]
goal = N - 1
d = 0
while q:
    qq = []
    d += 1
    for v in q:
        for w in graph[v]:
            if visited[w]:
                continue
            visited[w] = True
            qq.append(w)
    if visited[goal]:
        print(d)
        exit()
    q = qq
print('Odekakedekinai..')
            
            
            
        
            
maspy