結果

問題 No.3 ビットすごろく
ユーザー Tsubasa
提出日時 2018-02-05 20:57:09
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 3,294 ms / 5,000 ms
コード長 1,074 bytes
コンパイル時間 151 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 12,672 KB
最終ジャッジ日時 2024-07-01 08:55:28
合計ジャッジ時間 40,670 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

from inspect import currentframe

def chkprint(*args):
    names = {id(v):k for k,v in currentframe().f_back.f_locals.items()}
    print(', '.join(names.get(id(arg),'???')+' = '+repr(arg) for arg in args))

def main():
  # 総マス数
  N = int(input())

  queue = [1]

  move = 1
  pre_reached_len = -1
  reached = []

  while len(queue) != 0:
    new_queue = []
    # chkprint(move)

    for cell in queue:
      reached.append(cell)

      if cell == N:
        print(move)
        return

      n_one_bit = '{:b}'.format(cell).count('1')

      if cell + n_one_bit <= N:
        forward = cell + n_one_bit
      else:
        forward = -1

      if cell - n_one_bit >= 1:
        backward = cell - n_one_bit
      else:
        backward = -1

      if forward != 1 and forward not in reached and forward not in new_queue:
        new_queue.append(forward)

      if backward != -1 and backward not in reached and backward not in new_queue:
        new_queue.append(backward)

    move += 1
    queue = new_queue
    # chkprint(new_queue)

  print(-1)
  return


main()
0