結果

問題 No.3 ビットすごろく
ユーザー nakanolabnakanolab
提出日時 2015-05-31 17:15:23
言語 Python2
(2.7.18)
結果
AC  
実行時間 69 ms / 5,000 ms
コード長 486 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 6,688 KB
実行使用メモリ 7,100 KB
最終ジャッジ日時 2023-09-13 23:11:50
合計ジャッジ時間 2,478 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
6,036 KB
testcase_01 AC 11 ms
6,076 KB
testcase_02 AC 10 ms
5,976 KB
testcase_03 AC 16 ms
6,272 KB
testcase_04 AC 12 ms
6,276 KB
testcase_05 AC 31 ms
6,352 KB
testcase_06 AC 18 ms
6,404 KB
testcase_07 AC 13 ms
6,148 KB
testcase_08 AC 24 ms
6,512 KB
testcase_09 AC 40 ms
6,656 KB
testcase_10 AC 52 ms
6,960 KB
testcase_11 AC 38 ms
6,428 KB
testcase_12 AC 32 ms
6,352 KB
testcase_13 AC 16 ms
6,168 KB
testcase_14 AC 52 ms
7,060 KB
testcase_15 AC 68 ms
6,972 KB
testcase_16 AC 59 ms
7,064 KB
testcase_17 AC 66 ms
6,876 KB
testcase_18 AC 15 ms
6,196 KB
testcase_19 AC 67 ms
7,100 KB
testcase_20 AC 11 ms
6,152 KB
testcase_21 AC 10 ms
5,860 KB
testcase_22 AC 53 ms
7,012 KB
testcase_23 AC 69 ms
7,096 KB
testcase_24 AC 69 ms
6,924 KB
testcase_25 AC 67 ms
7,012 KB
testcase_26 AC 10 ms
5,824 KB
testcase_27 AC 16 ms
6,328 KB
testcase_28 AC 60 ms
6,964 KB
testcase_29 AC 37 ms
6,556 KB
testcase_30 AC 11 ms
5,888 KB
testcase_31 AC 11 ms
5,828 KB
testcase_32 AC 33 ms
6,540 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def ones(m):
  return bin(m).count('1')

def expand(m, n):
  dm = ones(m)
  ms = [m - dm, m + dm]
  return [m for m in ms if 0 < m <= n]

def bfs(frontier, visited, n):
  while frontier:
    path = frontier.pop(0)
    last = path[-1]
    if last == n:
      return path
    elif last not in visited:
      visited.add(last)
      for m in expand(last, n):
        frontier.append(path + [m])

n = int(raw_input())
path = bfs([[1]], set(), n)
if path:
  print len(path)
else:
  print -1
0