結果
| 問題 |
No.48 ロボットの操縦
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-28 16:13:43 |
| 言語 | Rust (1.83.0 + proconio) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 5,107 bytes |
| コンパイル時間 | 12,484 ms |
| コンパイル使用メモリ | 399,604 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-02 10:14:43 |
| 合計ジャッジ時間 | 15,144 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 WA * 1 |
ソースコード
struct Robot {
x: i32,
y: i32,
direction: String,
}
impl Robot {
fn new() -> Robot {
Robot {
x: 0,
y: 0,
direction: String::from("N"),
}
}
fn move_x(&mut self, dx: i32) {
self.x += dx;
}
fn move_y(&mut self, dy: i32) {
self.y += dy;
}
fn rotate_90_degree(&mut self) {
let now = &self.direction;
if *now == String::from("N") {
self.direction = String::from("E");
} else if *now == String::from("E") {
self.direction = String::from("S");
} else if *now == String::from("S") {
self.direction = String::from("W");
} else if *now == String::from("W") {
self.direction = String::from("N")
}
}
fn rotate_minus_90_degree(&mut self) {
let now = &self.direction;
if *now == String::from("N") {
self.direction = String::from("W");
} else if *now == String::from("W") {
self.direction = String::from("S");
} else if *now == String::from("S") {
self.direction = String::from("E");
} else if *now == String::from("E") {
self.direction = String::from("N")
}
}
}
fn getline() -> String {
let mut __ret = String::new();
std::io::stdin().read_line(&mut __ret).ok();
return __ret;
}
fn main() {
let x_goal: i32 = getline().trim().parse().unwrap();
let y_goal: i32 = getline().trim().parse().unwrap();
let robot_can_move_distance: i32 = getline().trim().parse().unwrap();
let mut remain_x: i32 = x_goal.abs();
let mut remain_y: i32 = y_goal.abs();
let mut order_count: i32 = 0;
let mut robot = Robot::new();
// 1. 最初の方向を決める
if y_goal < 0 {
if x_goal > 0 {
robot.rotate_90_degree();
} else if x_goal < 0 {
robot.rotate_minus_90_degree();
}
order_count += 1;
}
// 2. 進行する
if robot.direction == String::from("N") {
loop {
if remain_y <= 0 {
break;
}
if remain_y - robot_can_move_distance <= 0 {
robot.move_y(remain_y);
} else {
robot.move_y(robot_can_move_distance);
}
remain_y -= robot_can_move_distance;
order_count += 1;
}
} else if robot.direction == String::from("E") {
loop {
if remain_x <= 0 {
break;
}
if remain_x - robot_can_move_distance <= 0 {
robot.move_x(remain_x);
} else {
robot.move_x(robot_can_move_distance);
}
remain_x -= robot_can_move_distance;
order_count += 1;
}
} else if robot.direction == String::from("W") {
loop {
if remain_x <= 0 {
break;
}
if remain_x - robot_can_move_distance <= 0 {
robot.move_x(remain_x * -1);
} else {
robot.move_x(robot_can_move_distance * -1);
}
remain_x -= robot_can_move_distance;
order_count += 1;
}
}
// 3. 回転する
if robot.direction == String::from("N") {
if x_goal > 0 {
robot.rotate_90_degree();
order_count += 1;
} else if x_goal < 0 {
robot.rotate_minus_90_degree();
order_count += 1;
}
} else if robot.direction == String::from("E") {
robot.rotate_90_degree();
order_count += 1;
} else if robot.direction == String::from("W") {
robot.rotate_minus_90_degree();
order_count += 1;
}
// 4. 残りの分進行する
if robot.direction == String::from("E") {
loop {
if remain_x <= 0 {
break;
}
if remain_x - robot_can_move_distance <= 0 {
robot.move_x(remain_x);
} else {
robot.move_x(robot_can_move_distance);
}
remain_x -= robot_can_move_distance;
order_count += 1;
}
} else if robot.direction == String::from("W") {
loop {
if remain_x <= 0 {
break;
}
if remain_x - robot_can_move_distance <= 0 {
robot.move_x(remain_x * -1);
} else {
robot.move_x(robot_can_move_distance * -1);
}
remain_x -= robot_can_move_distance;
order_count += 1;
}
} else if robot.direction == String::from("S") {
loop {
if remain_y <= 0 {
break;
}
if remain_y - robot_can_move_distance <= 0 {
robot.move_y(remain_y * -1);
} else {
robot.move_y(robot_can_move_distance * -1);
}
remain_y -= robot_can_move_distance;
order_count += 1;
}
}
println!("{}", order_count);
}