結果

問題 No.48 ロボットの操縦
ユーザー m0ntBL4Ncm0ntBL4Nc
提出日時 2019-09-28 16:13:43
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 5,107 bytes
コンパイル時間 1,024 ms
コンパイル使用メモリ 152,252 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-10 08:32:56
合計ジャッジ時間 3,864 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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);
}
0