結果

問題 No.48 ロボットの操縦
ユーザー deruiderui
提出日時 2016-06-27 23:36:24
言語 OCaml
(5.1.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 2,260 bytes
コンパイル時間 509 ms
コンパイル使用メモリ 19,220 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-07-30 06:59:07
合計ジャッジ時間 1,450 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

type dir = N | E | W | S

let move_with_dir (x, y) pitch = function
  | N -> (x, y + pitch)
  | W -> (x - pitch, y)
  | E -> (x + pitch, y)
  | S -> (x, y - pitch)

let turn_for_x v current =
  let to_west = v < 0
  and to_east = v > 0 in
  match current with
  | N when to_east -> Some (E, 1)
  | N when to_west -> Some (W, 1)
  | S when to_east -> Some (E, 1)
  | S when to_west -> Some (W, 1)
  | E when to_west -> Some (W, 2)
  | W when to_east -> Some (E, 2)
  | _ -> None

let turn_for_y v  current =
  let to_south = v < 0
  and to_north = v > 0 in
  match current with
  | E when to_south -> Some (S, 1)
  | E when to_north -> Some (N, 1)
  | W when to_south -> Some (S, 1)
  | W when to_north -> Some (N, 1)
  | N when to_south -> Some (S, 2)
  | S when to_north -> Some (N, 2)
  | _ -> None

let solve tag_x tag_y max_pitch =
  let move_to_x x dir =
      let dir, act_count =
        match turn_for_x tag_x dir with
        | None -> dir, 0
        | Some v -> v in
      let quo = (abs tag_x) / max_pitch
      and rem = (abs tag_x) mod max_pitch in
      let rem = if rem > 0 then 1 else 0 in
      dir, (act_count + quo + rem)
  and move_to_y y dir =
      let dir, act_count =
        match turn_for_y tag_y dir with
        | None -> dir, 0
        | Some v -> v in
      let quo = (abs tag_y) / max_pitch
      and rem = (abs tag_y) mod max_pitch in
      let rem = if rem > 0 then 1 else 0 in
      dir, (act_count + quo + rem)
  in
  let rec solve' (x, y) dir count =
    if (x, y) = (tag_x, tag_y) then count
    else if x = tag_x then
      let dir, act_count = move_to_y y dir in
      solve' (x, tag_y) dir (count + act_count)
    else if y = tag_y then
      let dir, act_count = move_to_x x dir in
      solve' (tag_x, y) dir (count + act_count)
    else
      let dir_x, act_count_x = move_to_x x dir
      and dir_y, act_count_y = move_to_y y dir in
      if dir_y = dir then
        solve' (x, tag_y) dir_y (count + act_count_y)
      else
        solve' (tag_x, y) dir_x (count + act_count_x)
  in
  solve' (0, 0) N 0

let () =
  let x_pos = read_line () |> int_of_string in
  let y_pos = read_line () |> int_of_string in
  let max_movable = read_line () |> int_of_string in
  Printf.printf "%d\n" (solve x_pos y_pos max_movable)
0