結果

問題 No.340 雪の足跡
ユーザー gew1fw
提出日時 2025-06-12 14:37:15
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,078 bytes
コンパイル時間 573 ms
コンパイル使用メモリ 82,188 KB
実行使用メモリ 168,008 KB
最終ジャッジ日時 2025-06-12 14:37:26
合計ジャッジ時間 9,623 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 14 TLE * 2 -- * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    ptr = 0
    W = int(input[ptr])
    ptr += 1
    H = int(input[ptr])
    ptr += 1
    N = int(input[ptr])
    ptr += 1

    edges = set()

    for _ in range(N):
        M_i = int(input[ptr])
        ptr += 1
        B = list(map(int, input[ptr:ptr + M_i + 1]))
        ptr += M_i + 1
        for j in range(M_i):
            current = B[j]
            next_cell = B[j + 1]
            w1 = current % W
            h1 = current // W
            w2 = next_cell % W
            h2 = next_cell // W

            if h1 == h2:
                # East-West movement
                step = 1 if w2 > w1 else -1
                start, end = (w1, w2) if step == 1 else (w1, w2)
                for w in range(w1, w2, step):
                    a = h1 * W + w
                    b = h1 * W + (w + step)
                    if a < b:
                        edges.add((a, b))
                    else:
                        edges.add((b, a))
            else:
                # North-South movement
                step = 1 if h2 > h1 else -1
                start, end = (h1, h2) if step == 1 else (h1, h2)
                for h in range(h1, h2, step):
                    a = h * W + w1
                    b = (h + step) * W + w1
                    if a < b:
                        edges.add((a, b))
                    else:
                        edges.add((b, a))

    size = W * H
    adj = [[] for _ in range(size)]
    for a, b in edges:
        adj[a].append(b)
        adj[b].append(a)

    start = 0
    end = W * H - 1
    if start == end:
        print(0)
        return

    visited = [-1] * size
    q = deque([start])
    visited[start] = 0

    while q:
        u = q.popleft()
        for v in adj[u]:
            if visited[v] == -1:
                visited[v] = visited[u] + 1
                if v == end:
                    print(visited[v])
                    return
                q.append(v)

    print("Odekakedekinai..")

if __name__ == '__main__':
    main()
0