結果

問題 No.3 ビットすごろく
ユーザー pawn0818pawn0818
提出日時 2018-09-30 01:10:59
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 53 ms / 5,000 ms
コード長 1,479 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 10,900 KB
実行使用メモリ 9,048 KB
最終ジャッジ日時 2023-09-14 01:07:38
合計ジャッジ時間 2,524 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 21 ms
8,480 KB
testcase_01 AC 20 ms
8,660 KB
testcase_02 AC 18 ms
8,412 KB
testcase_03 AC 25 ms
8,452 KB
testcase_04 AC 20 ms
8,640 KB
testcase_05 AC 35 ms
8,720 KB
testcase_06 AC 25 ms
8,584 KB
testcase_07 AC 22 ms
8,696 KB
testcase_08 AC 32 ms
8,744 KB
testcase_09 AC 40 ms
8,832 KB
testcase_10 AC 44 ms
8,760 KB
testcase_11 AC 37 ms
8,700 KB
testcase_12 AC 34 ms
8,796 KB
testcase_13 AC 24 ms
8,656 KB
testcase_14 AC 44 ms
8,808 KB
testcase_15 AC 50 ms
8,816 KB
testcase_16 AC 48 ms
8,840 KB
testcase_17 AC 49 ms
8,764 KB
testcase_18 AC 22 ms
8,504 KB
testcase_19 AC 50 ms
9,048 KB
testcase_20 AC 18 ms
8,628 KB
testcase_21 AC 17 ms
8,532 KB
testcase_22 AC 42 ms
8,912 KB
testcase_23 AC 52 ms
8,892 KB
testcase_24 AC 53 ms
8,888 KB
testcase_25 AC 52 ms
8,976 KB
testcase_26 AC 18 ms
8,696 KB
testcase_27 AC 25 ms
8,520 KB
testcase_28 AC 48 ms
8,796 KB
testcase_29 AC 38 ms
8,840 KB
testcase_30 AC 18 ms
8,688 KB
testcase_31 AC 18 ms
8,656 KB
testcase_32 AC 38 ms
8,612 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
# !/usr/bin/env python
# vim: set fileencoding=utf-8 :

"""
#
# Author:   Noname
# URL:      https://github.com/pettan0818
# License:  MIT License
# Created: 金  9/28 23:04:27 2018

# Usage
#
"""
from collections import deque
def get():
    """
    """
    return int(input())

def get_each_cell_num(length):
    """
    >>> get_each_cell_num(5)
    [1, 1, 2, 1, 2]
    """
    def maker(num):
        return sum([int(i) for i in list(bin(num)[2:])])
    return [maker(i) for i in range(1, length+1)]

def bfs(res, goal, route):
    QUE = deque([])
    goal = goal - 1 # 配列と問題文のIndexは1差がある。
    res[0] = 1
    QUE.append(0)

    while QUE: # Listが空だとFalse
        pos = QUE.popleft()

        if pos == goal: # ゴールに着いたら探索不要
            return res

        for i in [route[pos], -route[pos]]:
            next_pos = pos + i
            if next_pos >= len(res):
                continue

            if next_pos >= 0 and res[next_pos] == -1:
                QUE.append(next_pos)
                # print("next_pos: ", next_pos)
                # print("res: ", res)
                # print("QUE: ", QUE)
                res[next_pos] = res[pos] + 1
    return res

def solve(length):
    """
    >>> solve(5)
    """
    res = [-1] * length
    route = get_each_cell_num(length)

    shortest_path = bfs(res, length, route)
    print(shortest_path[-1])


if __name__ == '__main__':
    solve(get())
0