結果

問題 No.340 雪の足跡
ユーザー rpy3cpprpy3cpp
提出日時 2016-01-30 23:42:21
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 494 ms / 1,000 ms
コード長 2,488 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 81,832 KB
実行使用メモリ 211,384 KB
最終ジャッジ日時 2023-10-21 18:13:44
合計ジャッジ時間 8,492 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,648 KB
testcase_01 AC 37 ms
53,648 KB
testcase_02 AC 36 ms
53,648 KB
testcase_03 AC 35 ms
53,648 KB
testcase_04 AC 38 ms
53,648 KB
testcase_05 AC 37 ms
53,648 KB
testcase_06 AC 36 ms
53,648 KB
testcase_07 AC 37 ms
53,648 KB
testcase_08 AC 37 ms
53,648 KB
testcase_09 AC 41 ms
53,648 KB
testcase_10 AC 36 ms
53,648 KB
testcase_11 AC 52 ms
66,404 KB
testcase_12 AC 51 ms
64,496 KB
testcase_13 AC 55 ms
66,564 KB
testcase_14 AC 91 ms
86,372 KB
testcase_15 AC 91 ms
87,308 KB
testcase_16 AC 74 ms
79,536 KB
testcase_17 AC 98 ms
89,208 KB
testcase_18 AC 97 ms
89,076 KB
testcase_19 AC 98 ms
89,208 KB
testcase_20 AC 334 ms
202,612 KB
testcase_21 AC 151 ms
79,164 KB
testcase_22 AC 149 ms
188,452 KB
testcase_23 AC 386 ms
211,384 KB
testcase_24 AC 293 ms
206,912 KB
testcase_25 AC 321 ms
188,976 KB
testcase_26 AC 86 ms
77,004 KB
testcase_27 AC 55 ms
72,520 KB
testcase_28 AC 88 ms
80,596 KB
testcase_29 AC 372 ms
141,904 KB
testcase_30 AC 291 ms
127,080 KB
testcase_31 AC 439 ms
180,256 KB
testcase_32 AC 494 ms
207,924 KB
testcase_33 AC 436 ms
204,664 KB
testcase_34 AC 487 ms
207,964 KB
testcase_35 AC 404 ms
188,824 KB
testcase_36 AC 406 ms
188,820 KB
権限があれば一括ダウンロードができます

ソースコード

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