874. Walking Robot Simulation Medium

@problem@discussion
#Array#Simulation



1/**
2 * [874] Walking Robot Simulation
3 *
4 * A robot on an infinite XY-plane starts at point (0, 0) facing north. The robot can receive a sequence of these three possible types of commands:
5 * 
6 * 	-2: Turn left 90 degrees.
7 * 	-1: Turn right 90 degrees.
8 * 	1 <= k <= 9: Move forward k units, one unit at a time.
9 * 
10 * Some of the grid squares are obstacles. The i^th obstacle is at grid point obstacles[i] = (xi, yi). If the robot runs into an obstacle, then it will instead stay in its current location and move on to the next command.
11 * Return the maximum Euclidean distance that the robot ever gets from the origin squared (i.e. if the distance is 5, return 25).
12 * Note:
13 * 
14 * 	North means +Y direction.
15 * 	East means +X direction.
16 * 	South means -Y direction.
17 * 	West means -X direction.
18 * 
19 *  
20 * Example 1:
21 * 
22 * Input: commands = [4,-1,3], obstacles = []
23 * Output: 25
24 * Explanation: The robot starts at (0, 0):
25 * 1. Move north 4 units to (0, 4).
26 * 2. Turn right.
27 * 3. Move east 3 units to (3, 4).
28 * The furthest point the robot ever gets from the origin is (3, 4), which squared is 3^2 + 4^2 = 25 units away.
29 * 
30 * Example 2:
31 * 
32 * Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
33 * Output: 65
34 * Explanation: The robot starts at (0, 0):
35 * 1. Move north 4 units to (0, 4).
36 * 2. Turn right.
37 * 3. Move east 1 unit and get blocked by the obstacle at (2, 4), robot is at (1, 4).
38 * 4. Turn left.
39 * 5. Move north 4 units to (1, 8).
40 * The furthest point the robot ever gets from the origin is (1, 8), which squared is 1^2 + 8^2 = 65 units away.
41 * 
42 * Example 3:
43 * 
44 * Input: commands = [6,-1,-1,6], obstacles = []
45 * Output: 36
46 * Explanation: The robot starts at (0, 0):
47 * 1. Move north 6 units to (0, 6).
48 * 2. Turn right.
49 * 3. Turn right.
50 * 4. Move south 6 units to (0, 0).
51 * The furthest point the robot ever gets from the origin is (0, 6), which squared is 6^2 = 36 units away.
52 * 
53 *  
54 * Constraints:
55 * 
56 * 	1 <= commands.length <= 10^4
57 * 	commands[i] is either -2, -1, or an integer in the range [1, 9].
58 * 	0 <= obstacles.length <= 10^4
59 * 	-3 * 10^4 <= xi, yi <= 3 * 10^4
60 * 	The answer is guaranteed to be less than 2^31.
61 * 
62 */
63pub struct Solution {}
64
65// problem: https://leetcode.com/problems/walking-robot-simulation/
66// discuss: https://leetcode.com/problems/walking-robot-simulation/discuss/?currentPage=1&orderBy=most_votes&query=
67
68// submission codes start here
69
70impl Solution {
71    pub fn robot_sim(commands: Vec<i32>, obstacles: Vec<Vec<i32>>) -> i32 {
72        0
73    }
74}
75
76// submission codes end
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn test_874() {
84    }
85}
86


Back
© 2025 bowen.ge All Rights Reserved.