結果

問題 No.3 ビットすごろく
ユーザー cherrypi59cherrypi59
提出日時 2018-06-23 17:42:44
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 40 ms / 5,000 ms
コード長 845 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 10,828 KB
実行使用メモリ 10,616 KB
最終ジャッジ日時 2023-09-14 01:03:05
合計ジャッジ時間 2,283 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,572 KB
testcase_01 AC 20 ms
8,600 KB
testcase_02 AC 19 ms
8,736 KB
testcase_03 AC 24 ms
8,968 KB
testcase_04 AC 20 ms
8,636 KB
testcase_05 AC 30 ms
9,572 KB
testcase_06 AC 25 ms
9,096 KB
testcase_07 AC 22 ms
8,932 KB
testcase_08 AC 28 ms
9,360 KB
testcase_09 AC 32 ms
9,864 KB
testcase_10 AC 36 ms
10,188 KB
testcase_11 AC 31 ms
9,848 KB
testcase_12 AC 29 ms
9,616 KB
testcase_13 AC 22 ms
8,960 KB
testcase_14 AC 35 ms
10,072 KB
testcase_15 AC 38 ms
10,540 KB
testcase_16 AC 38 ms
10,376 KB
testcase_17 AC 38 ms
10,348 KB
testcase_18 AC 22 ms
8,824 KB
testcase_19 AC 40 ms
10,560 KB
testcase_20 AC 19 ms
8,588 KB
testcase_21 AC 19 ms
8,708 KB
testcase_22 AC 34 ms
10,076 KB
testcase_23 AC 39 ms
10,616 KB
testcase_24 AC 39 ms
10,616 KB
testcase_25 AC 40 ms
10,500 KB
testcase_26 AC 19 ms
8,668 KB
testcase_27 AC 23 ms
9,120 KB
testcase_28 AC 37 ms
10,276 KB
testcase_29 AC 32 ms
9,868 KB
testcase_30 AC 18 ms
8,684 KB
testcase_31 AC 18 ms
8,688 KB
testcase_32 AC 29 ms
9,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n = int(input())

G = [[] for i in range(n)]
check, dist = [0 for i in range(n)], [-1 for i in range(n)]


def bsf(s):
    q = deque()
    check[s], dist[s] = 1, 0
    q.append(s)

    while len(q) != 0:
        u = q.popleft()
        for v in range(len(G[u])):
            if check[G[u][v]] == 0:
                check[G[u][v]], dist[G[u][v]] = 1, dist[u] + 1
                q.append(G[u][v])

        check[u] = 2


def main():
    for i in range(n):
        e, cnt = i + 1, 0
        while e != 0:
            if e & 1:
                cnt += 1
            e >>= 1

        if i + cnt < n:
            G[i].append(i+cnt)
        if i - cnt >= 0:
            G[i].append(i-cnt)

    bsf(0)

    if dist[n-1] == -1:
        print(-1)
    else:
        print(dist[n-1]+1)


if __name__ == '__main__':
    main()
0