結果

問題 No.340 雪の足跡
ユーザー rpy3cpprpy3cpp
提出日時 2016-01-30 10:52:17
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,439 bytes
コンパイル時間 319 ms
コンパイル使用メモリ 12,176 KB
実行使用メモリ 96,824 KB
最終ジャッジ日時 2023-10-21 17:52:50
合計ジャッジ時間 16,805 ms
ジャッジサーバーID
(参考情報)
judge15 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,368 KB
testcase_01 AC 31 ms
10,368 KB
testcase_02 AC 31 ms
10,368 KB
testcase_03 AC 30 ms
10,368 KB
testcase_04 AC 30 ms
10,368 KB
testcase_05 AC 30 ms
10,368 KB
testcase_06 AC 30 ms
10,368 KB
testcase_07 AC 29 ms
10,368 KB
testcase_08 AC 30 ms
10,368 KB
testcase_09 AC 30 ms
10,368 KB
testcase_10 AC 30 ms
10,368 KB
testcase_11 AC 33 ms
10,428 KB
testcase_12 AC 33 ms
10,420 KB
testcase_13 AC 35 ms
10,460 KB
testcase_14 AC 99 ms
12,072 KB
testcase_15 AC 110 ms
12,132 KB
testcase_16 AC 75 ms
11,016 KB
testcase_17 AC 130 ms
12,336 KB
testcase_18 AC 120 ms
12,336 KB
testcase_19 AC 130 ms
12,336 KB
testcase_20 AC 751 ms
40,316 KB
testcase_21 AC 436 ms
10,556 KB
testcase_22 AC 173 ms
33,784 KB
testcase_23 TLE -
testcase_24 AC 618 ms
34,244 KB
testcase_25 AC 706 ms
49,584 KB
testcase_26 AC 116 ms
11,164 KB
testcase_27 AC 49 ms
10,476 KB
testcase_28 AC 120 ms
11,544 KB
testcase_29 AC 983 ms
28,636 KB
testcase_30 AC 709 ms
22,500 KB
testcase_31 TLE -
testcase_32 TLE -
testcase_33 TLE -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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


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(hsWH, hsHW):
    cum_sum(hsWH)
    cum_sum(hsHW)

def cum_sum(mat):
    for row in mat:
        cum = 0
        for i, val in enumerate(row):
            cum += val
            row[i] = cum

def wfs(W, H, hsWH, hsHW):
    '''
    (0, 0) から (W-1, H-1) までの最短距離を求める。
    hsWH[w][h]: cell(w, h) と cell(w, h+1) に辺があるか否か
    hsHW[h][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
            h, w = divmod(f, W)
            if hsWH[w][h] and unvisited[f + W]:
                unvisited[f + W] = False
                new_frontiers.append(f + W)
            if hsWH[w][h-1] and unvisited[f - W]:
                unvisited[f - W] = False
                new_frontiers.append(f - W)
            if hsHW[h][w] and unvisited[f + 1]:
                unvisited[f + 1] = False
                new_frontiers.append(f + 1)
            if hsHW[h][w-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