Tap here to switch tabs
Problem
Submissions

Problem: Walking Robot Simulation

med
30 min
Try to solve the Walking Robot Simulation problem.

Statement

A robot starts on an infinite XYXY plane at coordinates (0,0)(0, 0) facing north. It receives an array of integers commands, representing a sequence of actions:

  • −2-2: Turn left 9090 degrees.

  • −1-1: Turn right 9090 degrees.

  • kk (where 1≤k≤91 \le k \le 9): Move forward kk units, one unit at a time.

There are obstacles on the grid, provided in an array obstacles where each element obstacles[i] =[xi,yi]= [x_i, y_i] denotes the coordinates of an obstacle. If the robot encounters an obstacle while moving, it stops in the cell immediately before the obstacle and continues with the next command.

Return the maximum squared Euclidean distance from the origin that the robot reaches at any point along its path.

Note: The squared Euclidean distance of a position (x,y)(x, y) from the origin (0,0)(0, 0) is defined as x2+y2x^2 + y^2.

Constraints:

  • 1≤1 \le commands.length ≤104\le 10^4

  • commands[i] is either −2-2, −1-1, or an integer in the range [1,9][1, 9]

  • 0≤0 \le obstacles.length ≤104\le 10^4

  • −3×104≤xi,yi≤3×104-3 \times 10^4 \le x_i, y_i \le 3 \times 10^4

  • The answer is guaranteed to be less than 2312^{31}

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Walking Robot Simulation

med
30 min
Try to solve the Walking Robot Simulation problem.

Statement

A robot starts on an infinite XYXY plane at coordinates (0,0)(0, 0) facing north. It receives an array of integers commands, representing a sequence of actions:

  • −2-2: Turn left 9090 degrees.

  • −1-1: Turn right 9090 degrees.

  • kk (where 1≤k≤91 \le k \le 9): Move forward kk units, one unit at a time.

There are obstacles on the grid, provided in an array obstacles where each element obstacles[i] =[xi,yi]= [x_i, y_i] denotes the coordinates of an obstacle. If the robot encounters an obstacle while moving, it stops in the cell immediately before the obstacle and continues with the next command.

Return the maximum squared Euclidean distance from the origin that the robot reaches at any point along its path.

Note: The squared Euclidean distance of a position (x,y)(x, y) from the origin (0,0)(0, 0) is defined as x2+y2x^2 + y^2.

Constraints:

  • 1≤1 \le commands.length ≤104\le 10^4

  • commands[i] is either −2-2, −1-1, or an integer in the range [1,9][1, 9]

  • 0≤0 \le obstacles.length ≤104\le 10^4

  • −3×104≤xi,yi≤3×104-3 \times 10^4 \le x_i, y_i \le 3 \times 10^4

  • The answer is guaranteed to be less than 2312^{31}