結果

問題 No.3 ビットすごろく
ユーザー ynishimuraynishimura
提出日時 2020-10-09 08:57:52
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 28 ms / 5,000 ms
コード長 2,291 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 10,788 KB
実行使用メモリ 8,916 KB
最終ジャッジ日時 2023-09-14 01:52:07
合計ジャッジ時間 2,175 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,552 KB
testcase_01 AC 20 ms
8,572 KB
testcase_02 AC 19 ms
8,492 KB
testcase_03 AC 21 ms
8,524 KB
testcase_04 AC 20 ms
8,588 KB
testcase_05 AC 23 ms
8,652 KB
testcase_06 AC 21 ms
8,480 KB
testcase_07 AC 20 ms
8,480 KB
testcase_08 AC 23 ms
8,548 KB
testcase_09 AC 25 ms
8,712 KB
testcase_10 AC 26 ms
8,756 KB
testcase_11 AC 24 ms
8,680 KB
testcase_12 AC 23 ms
8,776 KB
testcase_13 AC 20 ms
8,572 KB
testcase_14 AC 26 ms
8,756 KB
testcase_15 AC 28 ms
8,816 KB
testcase_16 AC 27 ms
8,748 KB
testcase_17 AC 28 ms
8,884 KB
testcase_18 AC 20 ms
8,496 KB
testcase_19 AC 28 ms
8,832 KB
testcase_20 AC 20 ms
8,496 KB
testcase_21 AC 19 ms
8,576 KB
testcase_22 AC 26 ms
8,788 KB
testcase_23 AC 28 ms
8,840 KB
testcase_24 AC 28 ms
8,916 KB
testcase_25 AC 27 ms
8,780 KB
testcase_26 AC 19 ms
8,584 KB
testcase_27 AC 20 ms
8,588 KB
testcase_28 AC 27 ms
8,876 KB
testcase_29 AC 24 ms
8,684 KB
testcase_30 AC 19 ms
8,596 KB
testcase_31 AC 19 ms
8,496 KB
testcase_32 AC 24 ms
8,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
N = int(input())


def movable_distance(num):
    "2進数で表現した時の1のビット数 => 前後に移動できる距離"
    # print(f'num:{num}, b:{bin(num)}')
    # 2進数表示した場合の1の数を数える
    return bin(num).count("1")


def bit():
    que = deque()
    que.append(1)  #探索するマス
    # マスの数を表す整数N(1<N<10000)が与えられます。
    trout = deque([0] * (N + 1))

    # 1から開始のマス
    trout[1] = 1

    while (len(que) > 0):
        # print(f'#### {que}')
        now = que.popleft()  #現在のマス目

        # debug
        # for i in range(N):
        #     if (i + 1 == now):
        #         print(f'*{trout[i + 1]}|', end='')  #現在のマス目
        #     elif (now == N):
        #         print(f'gaol => {trout[now]}', end='')  #終了
        #         break
        #     elif (trout[i + 1] != 0):
        #         print(f'{trout[i + 1]}|', end='')  #探索済マス
        #     else:
        #         print('-|', end='')  #未探索マス
        # print()

        if now == N:
            return trout[N]  #目的の最小移動数

        movecnt = movable_distance(now)  #現在のマス目を2進数に変換した時の1の数
        # print(f'now {now}, movecnt {movecnt}')

        back = now - movecnt  #後ろのマスに移動する
        forward = now + movecnt  #前のマスに移動する

        if (back > 0 and trout[back] == 0):
            que.append(back)  #探索を行うためキューに入れる
            trout[back] = trout[now] + 1  #移動した先に移動数の最小値を記録
            # print(
            #     f'マス{back}への移動数の最小値を記録: {trout[back]}, 探索を行うための位置をappend: {back}'
            # )

        if (forward <= N and trout[forward] == 0):
            que.append(forward)  #探索を行うためキューに入れる
            trout[forward] = trout[now] + 1  #移動した先に移動数の最小値を記録
            # print(
            #     f'マス{forward}への移動数の最小値を記録: {trout[forward]}, 探索を行うための位置をappend: {forward}'
            # )

    return -1


if __name__ == "__main__":
    print(bit())
0