結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,456 KB
testcase_01 AC 19 ms
8,512 KB
testcase_02 AC 20 ms
8,652 KB
testcase_03 AC 22 ms
8,708 KB
testcase_04 AC 20 ms
8,628 KB
testcase_05 AC 25 ms
8,688 KB
testcase_06 AC 22 ms
8,684 KB
testcase_07 AC 20 ms
8,472 KB
testcase_08 AC 23 ms
8,616 KB
testcase_09 AC 26 ms
8,744 KB
testcase_10 AC 28 ms
8,808 KB
testcase_11 AC 26 ms
8,776 KB
testcase_12 AC 25 ms
8,668 KB
testcase_13 AC 22 ms
8,552 KB
testcase_14 AC 28 ms
8,732 KB
testcase_15 AC 30 ms
8,928 KB
testcase_16 AC 29 ms
8,912 KB
testcase_17 AC 29 ms
8,872 KB
testcase_18 AC 20 ms
8,652 KB
testcase_19 AC 29 ms
8,976 KB
testcase_20 AC 19 ms
8,644 KB
testcase_21 AC 19 ms
8,524 KB
testcase_22 AC 28 ms
8,908 KB
testcase_23 AC 29 ms
8,852 KB
testcase_24 AC 29 ms
8,864 KB
testcase_25 AC 28 ms
9,008 KB
testcase_26 AC 18 ms
8,524 KB
testcase_27 AC 21 ms
8,660 KB
testcase_28 AC 29 ms
8,844 KB
testcase_29 AC 26 ms
8,724 KB
testcase_30 AC 18 ms
8,508 KB
testcase_31 AC 18 ms
8,520 KB
testcase_32 AC 24 ms
8,688 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
import sys
import math
import copy

def main():
    text = sys.stdin.readline()
    goal = int(text)    #ゴール
    ini_pos = 1           #初期値
    ini_cost = 1
    pos_cos = [ini_pos,ini_cost]   #[position,cost]
    costlist = [-1]*goal
    exactlist = [False]*goal
    costlist[0] = ini_cost
    exactlist[0] = True
    q = Que()
    q.addQ(pos_cos) 
    
    #経路探索処理→ダイクストラ法、ゴールに到達した時点で終了
    while not q.isEmpty():
        pos_cos = q.popQ()

        #行先判定処理
        next_position_list = nextGoto(pos_cos[0])
        for p in next_position_list:
            if p == goal:
                buf = [p, pos_cos[1] + 1]
                exactlist[p-1] = True
                costlist[p-1] = buf[1]
                #print(costlist)
                print(costlist[goal-1])
                return
            elif p < 1 or p > goal:
                continue
            elif exactlist[p-1]:
                continue
            else:
                buf = [p, pos_cos[1] + 1]
                exactlist[p-1] = True
                costlist[p-1] = buf[1]
                q.addQ(buf)
    print(costlist[goal-1])
    return

class Que():
    q_list = []
    def lenQ(self):
        return len(self.q_list)
    def addQ(self,arg_pathlist):
        self.q_list.append(arg_pathlist)
        return
    def popQ(self):
        return self.q_list.pop(0)
    def isEmpty(self):
        if len(self.q_list) <= 0 :
            flag = True
        else:
            flag = False
        return flag

#行先返答関数
def nextGoto(arg_position):
    binary_text = bin(arg_position)
    move = len(binary_text.replace("0b","").replace("0",""))
    goto = [arg_position - move, arg_position + move]
    return goto

if __name__ == "__main__":
    main()
0