結果

問題 No.3 ビットすごろく
ユーザー nbisco
提出日時 2016-04-09 23:12:44
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 662 bytes
コンパイル時間 86 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-07-01 07:48:59
合計ジャッジ時間 2,293 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
#fileencoding: utf-8

from collections import deque

def popcnt(i):
    count = 0
    while i > 0:
        count += (i&0x1)
        i >>= 1
    return count

def bfs(N):
    visited = [0] * N
    queue = deque()
    queue.append((1,1))  # (index, score)
    while queue:
        p = queue.popleft()
        if visited[p[0]-1] == 1:
            continue
        visited[p[0]-1] = 1
        if p[0] == N:
            return p[1]
        bits = popcnt(p[0])
        if p[0]+bits <= N:
            queue.append((p[0]+bits, p[1]+1))
        if p[0]-bits > 0:
            queue.append((p[0]-bits,p[1]+1))
    return -1

print(bfs(int(input())))
0