結果

問題 No.3 ビットすごろく
ユーザー bonyuta0204bonyuta0204
提出日時 2017-04-14 13:37:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,687 bytes
コンパイル時間 239 ms
コンパイル使用メモリ 11,100 KB
実行使用メモリ 9,948 KB
最終ジャッジ日時 2023-09-25 19:17:29
合計ジャッジ時間 2,966 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 AC 19 ms
8,672 KB
testcase_03 AC 25 ms
8,736 KB
testcase_04 WA -
testcase_05 AC 31 ms
9,168 KB
testcase_06 AC 25 ms
8,892 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 31 ms
9,048 KB
testcase_13 AC 23 ms
8,700 KB
testcase_14 AC 39 ms
9,816 KB
testcase_15 AC 43 ms
9,664 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 19 ms
8,560 KB
testcase_22 AC 39 ms
9,856 KB
testcase_23 AC 43 ms
9,800 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

N = int(input())
num_dict = {}
def _d_to_one(digit):
    """
    return number of 1 in binary of digit
    """
    binary = "{0: b}".format(digit)
    num_one = 0
    for i in range(len(binary)):
        if binary[i] == "1":
            num_one += 1
    return num_one

def d_to_one(digit):
    try:
        num = num_dict[digit]
    except KeyError:
        num = _d_to_one(digit)
        num_dict[digit] = num
    return num
def next_num(digits):
    step = d_to_one(digits)
    next = []
    if digits - step > 1:
        next.append(digits - step)
        
    if digits + step <= N:
        next.append(digits + step)
    return next


def bfs(start):
    if start == N:
        return [start]
    queue = deque([(start, [start])])
    

    while queue:

        vertex, path = queue.popleft()
        for next in set(next_num(vertex)) - set(path):
            if next == N:
                path.append(next)
                return path
            else:
                queue.append((next, path + [next]))
    return None

def bfs_no_path(start):
    if start == N:
        return [start]

    count = 1
    visited = set([start])

    queue = deque([(start, count)])
    

    while queue:
        vertex, count= queue.popleft()
        for next in set(next_num(vertex)) - visited :
            if next == N:
                count  += 1
                return count 

            else:
                visited.add(next)
                queue.append((next,  count))
    return None



def main():
    result = bfs_no_path(1)
    if result is not None:
        print(result)
    else:
        
        print(-1)
if __name__ == "__main__":
    main()
0