結果

問題 No.48 ロボットの操縦
ユーザー deruiderui
提出日時 2016-06-27 23:20:43
言語 OCaml
(5.1.0)
結果
TLE  
実行時間 -
コード長 2,252 bytes
コンパイル時間 433 ms
コンパイル使用メモリ 20,992 KB
実行使用メモリ 10,496 KB
最終ジャッジ日時 2024-10-08 23:47:21
合計ジャッジ時間 7,008 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 pitch = min max_pitch (abs (tag_x - x)) in
      dir, act_count, pitch
  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 pitch = min max_pitch (abs (tag_y - y)) in
      dir, act_count, pitch
  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, pitch = move_to_y y dir in
      solve' (move_with_dir (x, y) pitch dir) dir (count + act_count + 1)
    else if y = tag_y then
      let dir, act_count, pitch = move_to_x x dir in
      solve' (move_with_dir (x, y) pitch dir) dir (count + act_count + 1)
    else
      let dir_x, act_count_x, pitch_x = move_to_x x dir
      and dir_y, act_count_y, pitch_y = move_to_y y dir in
      if act_count_x > act_count_y then
        solve' (move_with_dir (x, y) pitch_y dir_y) dir_y (count + act_count_y + 1)
      else
        solve' (move_with_dir (x, y) pitch_x dir_x) dir_x (count + act_count_x + 1)
  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