結果

問題 No.3 ビットすごろく
ユーザー rpy3cpprpy3cpp
提出日時 2015-03-26 00:29:23
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 731 bytes
コンパイル時間 85 ms
コンパイル使用メモリ 10,904 KB
実行使用メモリ 8,984 KB
最終ジャッジ日時 2023-09-13 23:06:20
合計ジャッジ時間 1,939 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
7,896 KB
testcase_01 AC 15 ms
7,848 KB
testcase_02 AC 16 ms
7,968 KB
testcase_03 AC 16 ms
8,188 KB
testcase_04 AC 16 ms
7,964 KB
testcase_05 AC 18 ms
8,304 KB
testcase_06 AC 17 ms
8,348 KB
testcase_07 AC 16 ms
7,832 KB
testcase_08 AC 17 ms
8,200 KB
testcase_09 AC 19 ms
8,780 KB
testcase_10 AC 20 ms
8,912 KB
testcase_11 AC 19 ms
8,324 KB
testcase_12 AC 18 ms
8,300 KB
testcase_13 AC 16 ms
8,188 KB
testcase_14 AC 19 ms
8,856 KB
testcase_15 AC 20 ms
8,860 KB
testcase_16 AC 21 ms
8,764 KB
testcase_17 AC 20 ms
8,888 KB
testcase_18 AC 16 ms
8,396 KB
testcase_19 AC 21 ms
8,860 KB
testcase_20 AC 16 ms
7,972 KB
testcase_21 AC 15 ms
7,860 KB
testcase_22 AC 19 ms
8,972 KB
testcase_23 AC 21 ms
8,984 KB
testcase_24 AC 20 ms
8,884 KB
testcase_25 AC 20 ms
8,864 KB
testcase_26 AC 15 ms
7,908 KB
testcase_27 AC 16 ms
8,188 KB
testcase_28 AC 21 ms
8,860 KB
testcase_29 AC 19 ms
8,952 KB
testcase_30 AC 15 ms
7,844 KB
testcase_31 AC 15 ms
7,968 KB
testcase_32 AC 18 ms
8,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def bit_sugoroku(N):
    if N == 1:
        return 1
    steps = 1
    pos = 1
    frontier = [pos]
    visited = {pos}
    while frontier:
        new_frontier = []
        steps += 1
        for pos in frontier:
            d = bin(pos).count('1')
            if pos + d == N or pos - d == N:
                return steps
            p1 = pos + d
            p2 = pos - d
            if p1 < N and p1 not in visited:
                visited.add(p1)
                new_frontier.append(p1)
            if p2 > 1 and p2 not in visited:
                visited.add(p2)
                new_frontier.append(p2)
        frontier = new_frontier
    return - 1

if __name__ == '__main__':
    N = int(input())
    print(bit_sugoroku(N))
0