結果

問題 No.340 雪の足跡
ユーザー gew1fw
提出日時 2025-06-12 20:58:22
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,562 bytes
コンパイル時間 206 ms
コンパイル使用メモリ 82,088 KB
実行使用メモリ 91,520 KB
最終ジャッジ日時 2025-06-12 21:02:03
合計ジャッジ時間 9,077 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 15 TLE * 1 -- * 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

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

    adjacency = [[] for _ in range(W * H)]

    for _ in range(N):
        M_i = int(input[ptr]); ptr += 1
        path = list(map(int, input[ptr:ptr + M_i + 1]))
        ptr += M_i + 1

        for j in range(M_i):
            B = path[j]
            next_B = path[j + 1]

            if B == next_B:
                continue

            w1 = B % W
            h1 = B // W
            w2 = next_B % W
            h2 = next_B // W

            if h1 == h2:
                if w2 > w1:
                    direction = 'east'
                    w_start = w1
                    w_end = w2
                    cells = []
                    for w in range(w_start, w_end + 1):
                        cells.append(w + h1 * W)
                else:
                    direction = 'west'
                    w_start = w1
                    w_end = w2
                    cells = []
                    for w in range(w_start, w_end - 1, -1):
                        cells.append(w + h1 * W)
            else:
                if h2 > h1:
                    direction = 'north'
                    h_start = h1
                    h_end = h2
                    cells = []
                    for h in range(h_start, h_end + 1):
                        cells.append(w1 + h * W)
                else:
                    direction = 'south'
                    h_start = h1
                    h_end = h2
                    cells = []
                    for h in range(h_start, h_end - 1, -1):
                        cells.append(w1 + h * W)

            for i in range(len(cells) - 1):
                u = cells[i]
                v = cells[i + 1]
                if v not in adjacency[u]:
                    adjacency[u].append(v)
                if u not in adjacency[v]:
                    adjacency[v].append(u)

    visited = [-1] * (W * H)
    queue = deque()
    start = 0
    end = W * H - 1

    visited[start] = 0
    queue.append(start)

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

    print("Odekakedekinai..")

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