結果

問題 No.3 ビットすごろく
ユーザー pocarist
提出日時 2015-09-01 13:22:09
言語 F#
(F# 4.0)
結果
AC  
実行時間 1,027 ms / 5,000 ms
コード長 921 bytes
コンパイル時間 10,089 ms
コンパイル使用メモリ 196,128 KB
実行使用メモリ 76,388 KB
最終ジャッジ日時 2024-07-01 07:31:08
合計ジャッジ時間 28,263 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.fsproj を復元しました (307 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

// http://yukicoder.me/problems/11
open System

let dprintfn fmt = Printf.kprintf Diagnostics.Debug.WriteLine fmt

let bitcount n =
    let rec loop bits num =
        if bits = 0 then num
        else loop (bits &&& (bits-1)) (num+1)
    loop n 0

let solve N =
    let visited = Array.init (N+1) (fun _ -> false)
    let queue = new System.Collections.Generic.Queue<int*int>()
    let rec bfs (q : System.Collections.Generic.Queue<int*int>) =
        if q.Count = 0 then -1 else
        let i, cnt = q.Dequeue()
        if i = N then cnt else
        visited.[i] <- true
        let n = bitcount i
        if i+n <= N && not visited.[i+n] then
            q.Enqueue(i+n, cnt+1)
        if i-n >= 1 && not visited.[i-n] then
            q.Enqueue(i-n, cnt+1)
        bfs q
    queue.Enqueue(1,1)
    bfs queue

[<EntryPoint>]
let main argv = 
    let N = Console.ReadLine() |> int
    solve N
    |> printfn "%d" 
    0
0