結果
問題 | No.340 雪の足跡 |
ユーザー |
|
提出日時 | 2016-01-30 04:27:01 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 2,906 bytes |
コンパイル時間 | 225 ms |
コンパイル使用メモリ | 82,196 KB |
実行使用メモリ | 205,916 KB |
最終ジャッジ日時 | 2024-09-21 19:00:50 |
合計ジャッジ時間 | 13,915 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 5 |
other | AC * 31 TLE * 1 |
ソースコード
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 lineshsHW = [[0] * W for h in range(H)] # horizontal linesfor n in range(N):process_move(hsWH, hsHW, W, H)return W, H, hsWH, hsHWdef 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 moveif w0 > w1:hsHW[h0][w1] += 1hsHW[h0][w0] -= 1else:hsHW[h0][w0] += 1hsHW[h0][w1] -= 1w0 = w1else: # vertical moveif h0 > h1:hsWH[w0][h1] += 1hsWH[w0][h0] -= 1else:hsWH[w0][h0] += 1hsWH[w0][h1] -= 1h0 = h1def 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 Esdef process_vertical_move(W, H, hsWH, Es):for w, hsw in enumerate(hsWH):cum = 0idx = wfor val in hsw:cum += valif cum:Es[idx][3] = 1Es[idx + W][2] = 1idx += Wdef process_horizontal_move(W, H, hsHW, Es):for h, hsh in enumerate(hsHW):cum = 0idx = h * Wfor val in hsh:cum += valif cum:Es[idx][1] = 1Es[idx + 1][0] = 1idx += 1def 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 = 0goal = H * W - 1unvisited = [True] * (H * W)frontiers = [start]cost = 0while frontiers:cost += 1new_frontiers = []for f in frontiers:if f == goal:return cost - 1Esf = Es[f]if Esf[0] and unvisited[f - 1]:unvisited[f - 1] = Falsenew_frontiers.append(f - 1)if Esf[1] and unvisited[f + 1]:unvisited[f + 1] = Falsenew_frontiers.append(f + 1)if Esf[2] and unvisited[f - W]:unvisited[f - W] = Falsenew_frontiers.append(f - W)if Esf[3] and unvisited[f + W]:unvisited[f + W] = Falsenew_frontiers.append(f + W)frontiers = new_frontiersreturn -1ans = solve()if ans == -1:print("Odekakedekinai..")else:print(ans)