結果

問題 No.3 ビットすごろく
ユーザー pocarist
提出日時 2015-09-01 13:56:13
言語 F#
(F# 4.0)
結果
AC  
実行時間 1,041 ms / 5,000 ms
コード長 906 bytes
コンパイル時間 11,053 ms
コンパイル使用メモリ 196,376 KB
実行使用メモリ 77,036 KB
最終ジャッジ日時 2024-07-01 07:32:14
合計ジャッジ時間 24,997 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 33
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.fsproj を復元しました (246 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
open System.Collections.Generic

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 q = new Queue<int*int>()
    q.Enqueue(1,1)
    let rec bfs () =
        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 then cnt+1 else
        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 ()
    bfs ()

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