2069. Walking Robot Simulation II Medium
1/**
2 * [2069] Walking Robot Simulation II
3 *
4 * A width x height grid is on an XY-plane with the bottom-left cell at (0, 0) and the top-right cell at (width - 1, height - 1). The grid is aligned with the four cardinal directions ("North", "East", "South", and "West"). A robot is initially at cell (0, 0) facing direction "East".
5 * The robot can be instructed to move for a specific number of steps. For each step, it does the following.
6 * <ol>
7 * Attempts to move forward one cell in the direction it is facing.
8 * If the cell the robot is moving to is out of bounds, the robot instead turns 90 degrees counterclockwise and retries the step.
9 * </ol>
10 * After the robot finishes moving the number of steps required, it stops and awaits the next instruction.
11 * Implement the Robot class:
12 *
13 * Robot(int width, int height) Initializes the width x height grid with the robot at (0, 0) facing "East".
14 * void step(int num) Instructs the robot to move forward num steps.
15 * int[] getPos() Returns the current cell the robot is at, as an array of length 2, [x, y].
16 * String getDir() Returns the current direction of the robot, "North", "East", "South", or "West".
17 *
18 *
19 * Example 1:
20 * <img alt="example-1" src="https://assets.leetcode.com/uploads/2021/10/09/example-1.png" style="width: 498px; height: 268px;" />
21 * Input
22 * ["Robot", "step", "step", "getPos", "getDir", "step", "step", "step", "getPos", "getDir"]
23 * [[6, 3], [2], [2], [], [], [2], [1], [4], [], []]
24 * Output
25 * [null, null, null, [4, 0], "East", null, null, null, [1, 2], "West"]
26 * Explanation
27 * Robot robot = new Robot(6, 3); // Initialize the grid and the robot at (0, 0) facing East.
28 * robot.step(2); // It moves two steps East to (2, 0), and faces East.
29 * robot.step(2); // It moves two steps East to (4, 0), and faces East.
30 * robot.getPos(); // return [4, 0]
31 * robot.getDir(); // return "East"
32 * robot.step(2); // It moves one step East to (5, 0), and faces East.
33 * // Moving the next step East would be out of bounds, so it turns and faces North.
34 * // Then, it moves one step North to (5, 1), and faces North.
35 * robot.step(1); // It moves one step North to (5, 2), and faces North (not West).
36 * robot.step(4); // Moving the next step North would be out of bounds, so it turns and faces West.
37 * // Then, it moves four steps West to (1, 2), and faces West.
38 * robot.getPos(); // return [1, 2]
39 * robot.getDir(); // return "West"
40 *
41 *
42 * Constraints:
43 *
44 * 2 <= width, height <= 100
45 * 1 <= num <= 10^5
46 * At most 10^4 calls in total will be made to step, getPos, and getDir.
47 *
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/walking-robot-simulation-ii/
52// discuss: https://leetcode.com/problems/walking-robot-simulation-ii/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56struct Robot {
57 false
58 }
59
60
61/**
62 * `&self` means the method takes an immutable reference.
63 * If you need a mutable reference, change it to `&mut self` instead.
64 */
65impl Robot {
66
67 fn new(width: i32, height: i32) -> Self {
68
69 }
70
71 fn step(&self, num: i32) {
72
73 }
74
75 fn get_pos(&self) -> Vec<i32> {
76
77 }
78
79 fn get_dir(&self) -> String {
80
81 }
82}
83
84/**
85 * Your Robot object will be instantiated and called as such:
86 * let obj = Robot::new(width, height);
87 * obj.step(num);
88 * let ret_2: Vec<i32> = obj.get_pos();
89 * let ret_3: String = obj.get_dir();
90 */
91
92// submission codes end
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn test_2069() {
100 }
101}
102
Back
© 2025 bowen.ge All Rights Reserved.