結果

問題 No.340 雪の足跡
ユーザー rpy3cpprpy3cpp
提出日時 2016-01-30 11:01:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 980 ms / 1,000 ms
コード長 2,906 bytes
コンパイル時間 456 ms
コンパイル使用メモリ 81,816 KB
実行使用メモリ 205,556 KB
最終ジャッジ日時 2023-10-21 17:53:20
合計ジャッジ時間 12,591 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,484 KB
testcase_01 AC 37 ms
53,484 KB
testcase_02 AC 37 ms
53,484 KB
testcase_03 AC 37 ms
53,484 KB
testcase_04 AC 36 ms
53,484 KB
testcase_05 AC 37 ms
53,484 KB
testcase_06 AC 38 ms
53,484 KB
testcase_07 AC 37 ms
53,484 KB
testcase_08 AC 38 ms
53,484 KB
testcase_09 AC 37 ms
53,484 KB
testcase_10 AC 39 ms
53,484 KB
testcase_11 AC 56 ms
66,244 KB
testcase_12 AC 58 ms
66,376 KB
testcase_13 AC 60 ms
68,424 KB
testcase_14 AC 101 ms
78,088 KB
testcase_15 AC 120 ms
86,180 KB
testcase_16 AC 87 ms
76,804 KB
testcase_17 AC 133 ms
87,728 KB
testcase_18 AC 126 ms
87,712 KB
testcase_19 AC 137 ms
87,724 KB
testcase_20 AC 533 ms
205,092 KB
testcase_21 AC 178 ms
76,436 KB
testcase_22 AC 227 ms
200,832 KB
testcase_23 AC 475 ms
205,260 KB
testcase_24 AC 412 ms
196,976 KB
testcase_25 AC 539 ms
205,152 KB
testcase_26 AC 97 ms
75,932 KB
testcase_27 AC 59 ms
72,356 KB
testcase_28 AC 102 ms
77,164 KB
testcase_29 AC 667 ms
152,044 KB
testcase_30 AC 513 ms
133,964 KB
testcase_31 AC 862 ms
194,232 KB
testcase_32 AC 980 ms
205,164 KB
testcase_33 AC 901 ms
205,020 KB
testcase_34 AC 962 ms
205,160 KB
testcase_35 AC 809 ms
205,556 KB
testcase_36 AC 791 ms
205,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve():
    W, H, hsWH, hsHW = read_data()
    Es = build_edges(W, H, hsWH, hsHW)
    return wfs(Es, W, H)


def read_data():
    W, H, N = map(int, input().split())
    hsWH = [[0] * H for w in range(W)]  # vertical lines
    hsHW = [[0] * W for h in range(H)]  # horizontal lines
    for n in range(N):
        process_move(hsWH, hsHW, W, H)
    return W, H, hsWH, hsHW


def process_move(hsWH, hsHW, W, H):
    Mnotused = input()
    Bs = list(map(int, input().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[h0][w1] += 1
                hsHW[h0][w0] -= 1
            else:
                hsHW[h0][w0] += 1
                hsHW[h0][w1] -= 1
            w0 = w1
        else:        # vertical move
            if h0 > h1:
                hsWH[w0][h1] += 1
                hsWH[w0][h0] -= 1
            else:
                hsWH[w0][h0] += 1
                hsWH[w0][h1] -= 1
            h0 = h1


def build_edges(W, H, hsWH, hsHW):
    Es = [[0, 0, 0, 0] for idx in range(H * W)]
    process_vertical_move(W, H, hsWH, Es)
    process_horizontal_move(W, H, hsHW, Es)
    return Es


def process_vertical_move(W, H, hsWH, Es):
    for w, hsw in enumerate(hsWH):
        cum = 0
        idx = w
        for val in hsw:
            cum += val
            if cum:
                Es[idx][3] = 1
                Es[idx + W][2] = 1
            idx += W
    

def process_horizontal_move(W, H, hsHW, Es):
    for h, hsh in enumerate(hsHW):
        cum = 0
        idx = h * W
        for val in hsh:
            cum += val
            if cum:
                Es[idx][1] = 1
                Es[idx + 1][0] = 1
            idx += 1


def wfs(Es, W, H):
    '''Es[idx][d]: セルidx番のd方向の辺の有無。(d=0,1,2,3=左、右、下、上=-1,1,-W,W)
    idx=0 から、idx=H*W-1 までの最短距離を幅優先探索で求める。
    '''
    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
            Esf = Es[f]
            if Esf[0] and unvisited[f - 1]:
                unvisited[f - 1] = False
                new_frontiers.append(f - 1)
            if Esf[1] and unvisited[f + 1]:
                unvisited[f + 1] = False
                new_frontiers.append(f + 1)
            if Esf[2] and unvisited[f - W]:
                unvisited[f - W] = False
                new_frontiers.append(f - W)
            if Esf[3] and unvisited[f + W]:
                unvisited[f + W] = False
                new_frontiers.append(f + W)
        frontiers = new_frontiers
    return -1
    

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