#874

Walking Robot Simulation

specialist · 760 · lc medium +31 · 58.4% accepted · 899 likes · top 55%

Description

A robot on an infinite XY-plane starts at (0, 0) facing north and follows an array of integer commands:

- -2: rotate left by 90 degrees.

- -1: rotate right by 90 degrees.

- 1 <= k <= 9: move forward k steps, one step at a time.

Some grid cells contain obstacles. When the robot would step into an obstacle, it stays in its current cell and moves on to the next command.

Return the maximum squared Euclidean distance the robot reaches from the origin at any point during its path (i.e., if the farthest distance is 5, return 25).

Notes:

- An obstacle at (0, 0) is ignored until the robot leaves the origin; after that, the robot cannot return to (0, 0).

- North means +Y, east means +X, south means -Y, west means -X.

Code

1
2
3