結果

問題 No.340 雪の足跡
ユーザー rpy3cpprpy3cpp
提出日時 2016-01-30 23:41:02
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,488 bytes
コンパイル時間 101 ms
コンパイル使用メモリ 11,968 KB
実行使用メモリ 104,696 KB
最終ジャッジ日時 2023-10-21 18:14:44
合計ジャッジ時間 16,977 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,288 KB
testcase_01 AC 33 ms
10,288 KB
testcase_02 AC 32 ms
10,288 KB
testcase_03 AC 33 ms
10,288 KB
testcase_04 AC 30 ms
10,288 KB
testcase_05 AC 30 ms
10,288 KB
testcase_06 AC 31 ms
10,288 KB
testcase_07 AC 30 ms
10,288 KB
testcase_08 AC 31 ms
10,288 KB
testcase_09 AC 31 ms
10,288 KB
testcase_10 AC 30 ms
10,288 KB
testcase_11 AC 35 ms
10,360 KB
testcase_12 AC 34 ms
10,396 KB
testcase_13 AC 35 ms
10,444 KB
testcase_14 AC 99 ms
12,320 KB
testcase_15 AC 111 ms
12,480 KB
testcase_16 AC 79 ms
11,512 KB
testcase_17 AC 132 ms
12,812 KB
testcase_18 AC 119 ms
12,960 KB
testcase_19 AC 133 ms
13,576 KB
testcase_20 AC 741 ms
46,388 KB
testcase_21 AC 462 ms
13,656 KB
testcase_22 AC 101 ms
40,868 KB
testcase_23 TLE -
testcase_24 AC 594 ms
40,088 KB
testcase_25 AC 704 ms
55,624 KB
testcase_26 AC 131 ms
12,008 KB
testcase_27 AC 49 ms
10,564 KB
testcase_28 AC 127 ms
12,420 KB
testcase_29 AC 981 ms
36,100 KB
testcase_30 AC 700 ms
29,992 KB
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import itertools

def solve():
    W, H, hsWH, hsHW = read_data()
    hsWH = list(itertools.accumulate(hsWH))
    hsHW = list(itertools.accumulate(hsHW))
    result = wfs(W, H, hsWH, hsHW)
    return result


def read_data():
    W, H, N = map(int, input().split())
    hsWH = [0] * (W * H)  # vertical lines
    hsHW = [0] * (W * H)  # horizontal lines
    lines = sys.stdin.readlines()
    flag = True
    for line in lines:
        flag = not flag
        if flag:
            Bs = list(map(int, line.split()))
            h0, w0 = divmod(Bs[0], W)
            for b in Bs:
                h1, w1 = divmod(b, W)
                if h0 == h1:  # horizontal move
                    if w0 > w1:
                        hsHW[b] += 1
                        hsHW[b + w0 - w1] -= 1
                    else:
                        hsHW[b + w0 - w1] += 1
                        hsHW[b] -= 1
                    w0 = w1
                else:        # vertical move
                    if h0 > h1:
                        hsWH[w1 * H + h1] += 1
                        hsWH[w1 * H + h0] -= 1
                    else:
                        hsWH[w1 * H + h0] += 1
                        hsWH[w1 * H + h1] -= 1
                    h0 = h1
    return W, H, hsWH, hsHW


def wfs(W, H, hsWH, hsHW):
    '''
    (0, 0) から (W-1, H-1) までの最短距離を求める。
    hsWH[w * H + h]: cell(w, h) と cell(w, h+1) に辺があるか否か
    hsHW[h * W + w]: cell(w, h) と cell(w+1, h) に辺があるか否か
    '''
    start = 0
    goal = H * W - 1
    unvisited = [True] * (H * W)
    frontiers = [start]
    cost = 0
    while frontiers:
        cost += 1
        new_frontiers = []
        for f in frontiers:
            if f == goal:
                return cost - 1
            g = (f % W) * H + (f//W)
            if hsWH[g] and unvisited[f + W]:
                unvisited[f + W] = False
                new_frontiers.append(f + W)
            if hsWH[g - 1] and unvisited[f - W]:
                unvisited[f - W] = False
                new_frontiers.append(f - W)
            if hsHW[f] and unvisited[f + 1]:
                unvisited[f + 1] = False
                new_frontiers.append(f + 1)
            if hsHW[f - 1] and unvisited[f - 1]:
                unvisited[f - 1] = False
                new_frontiers.append(f - 1)
        frontiers = new_frontiers
    return -1

ans = solve()
if ans == -1:
    print("Odekakedekinai..")
else:
    print(ans)
0