Walking Robot Simulation
A robot starts at (0,0) on an infinite grid facing north. It processes a command list: -2 turns it left 90 degrees, -1 turns it right 90 degrees, and any value k from 1 to 9 moves it forward k unit-cells one at a time. If the next cell in its path is an obstacle, it stays put and ignores the remaining forward steps of that command. Return the maximum squared Euclidean distance (x*x + y*y) from the origin the robot ever reaches.
Open official problem prompt ↗Faithfully replay a robot's turn/move script on a grid with blocking obstacles and report the farthest squared distance from origin it ever attains.
Like a Roomba following a taped-in program: it pivots in place on turn commands and rolls forward on move commands, bumping to a stop when it hits a wall, while you note the farthest spot from its dock.
- Input
- commands = [4,-1,3], obstacles = []
- Output
- 25
- Why
- The robot moves north to (0,4), turns right to face east, moves to (3,4); the farthest point is (3,4) with 3*3 + 4*4 = 25.
1 <= commands.length <= 10^4commands[i] is -2, -1, or an integer in [1, 9]0 <= obstacles.length <= 10^4-3 * 10^4 <= obstacles[i][0], obstacles[i][1] <= 3 * 10^4The answer is guaranteed to be less than 2^31