結果

問題 No.3 ビットすごろく
ユーザー むらためむらため
提出日時 2017-07-30 21:56:56
言語 Nim
(2.0.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 858 bytes
コンパイル時間 845 ms
コンパイル使用メモリ 61,056 KB
最終ジャッジ日時 2023-09-12 13:48:18
合計ジャッジ時間 1,208 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 50) Warning: Use the new 'sugar' module instead; future is deprecated [Deprecated]
/home/judge/data/code/Main.nim(1, 62) Error: cannot open file: queues

ソースコード

diff #

import sequtils,strutils,strscans,algorithm,math,future,sets,queues,tables
template get():string = stdin.readLine()
template times(n:int,body:untyped): untyped = (for _ in 0..<n: body)
#proc popcount(n:int):cint{.importC: "__builtin_popcount", noDecl .} # sum of "1"

#close = initTable[int,int]() # hash is slow
#if not (succ in close): # hash is slow
#let diff = n.toBin(64).count('1') # string is slow

let N = get().parseInt
var
  open = initQueue[int]() # n
  close = newSeqWith(N+1,-1)# depth
  resdepth = 0
open.add(1)  # 1 *-> N
close[1] = 1
while open.len > 0:
  let
    n = open.pop()
    depth = close[n]
  if n == N:
    echo depth
    quit()
  let diff = countBits32(n.int32)
  for succ in @[n + diff,n - diff]:
    if succ > N or succ < 1:
      continue
    if close[succ] == -1:
      close[succ] = depth + 1
      open.enqueue(succ)
echo -1
0