結果

問題 No.3 ビットすごろく
ユーザー kashiwagi513kashiwagi513
提出日時 2019-03-01 11:44:27
言語 OCaml
(5.1.0)
結果
WA  
実行時間 -
コード長 1,052 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 19,788 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-17 08:45:35
合計ジャッジ時間 1,140 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 WA -
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 3 ms
6,944 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 3 ms
6,940 KB
testcase_23 AC 3 ms
6,944 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1 ms
6,944 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

let get arr i   = arr.(i-1)
let set arr i v = arr.(i-1) <- v

let rec value_of_cell n acc =
  if n = 0 then acc
  else value_of_cell (n / 2) (acc + (n mod 2))

let value_of_cell n = value_of_cell n 0

let gen_graph n =
  let g = Array.make n [] in
  for i = 1 to n do
    let v = value_of_cell i in
    if i+v <= n then set g (i+v) (i::(get g (i+v)));
    if 1 <= i-v then set g (i-v) (i::(get g (i-v)))
  done; g

let bfs n graph nodes =
  let q = Queue.create () in
  Queue.push n q;
  let rec inner () =
    if Queue.is_empty q then (-1)
    else
      begin
        let v = Queue.pop q in
        if v = 1 then get nodes v
        else 
          begin
            List.iter (fun l -> 
                if get nodes l = (-1) then Queue.push l q; set nodes l ((get nodes v) + 1))
              (get graph v);
            inner ()
          end
      end
  in inner ()

let solve n =
  let nodes = Array.make n (-1) in
  set nodes n 1;
  let g = gen_graph n in
  print_int @@ bfs n g nodes

let _ =
  let n = int_of_string @@ read_line() in
  solve n
0