KatInterview

Rover on a Walled Floor Plan with Blocked Cells

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashingsimulationhash-setgrid

Last updated

Simulate a rover on a bounded rectangular grid that turns, steps forward, and steps backward, skipping any step that leaves the grid or enters a blocked cell.

A rover drives around a rectangular floor plan. Some cells are blocked by crates. Your job is to replay a string of instructions and report where the rover ends up and which way it faces.

Floor plan

The floor has cols columns and rows rows. Cell (0, 0) is the bottom-left corner and cell (cols - 1, rows - 1) is the top-right corner. A cell (x, y) is on the floor only if 0 <= x < cols and 0 <= y < rows.

The rover begins at a given cell and faces one of four compass directions:

  • "N" points toward increasing y.
  • "E" points toward increasing x.
  • "S" points toward decreasing y.
  • "W" points toward decreasing x.

Instructions

The instruction string is read one character at a time, from left to right:

  • L: rotate 90 degrees counter-clockwise in place (for example N becomes W).
  • R: rotate 90 degrees clockwise in place (for example N becomes E).
  • M: advance one cell in the direction the rover currently faces.
  • B: reverse one cell, i.e. step opposite to the facing direction. The facing direction stays the same.

Blocked steps

  • If an M or B step would take the rover off the floor, the step is skipped and the rover stays where it is.
  • If an M or B step would take the rover onto a crate cell, the step is skipped and the rover stays where it is.
  • A skipped step does not stop the run. Processing simply continues with the next instruction.
  • Rotations (L, R) always succeed.

Task

Implement the function so that it processes every instruction and returns the final state as a list [x, y, direction], where direction is one of the strings "N", "E", "S", "W".

Input format

  • cols: number of columns (width of the floor).
  • rows: number of rows (height of the floor).
  • start: a list [x, y, direction] giving the initial cell and facing.
  • crates: a list of [cx, cy] pairs, each a blocked cell.
  • instructions: the string of commands.

Constraints

  • 1 <= cols, rows <= 10^4
  • start is [x, y, direction] with 0 <= x < cols, 0 <= y < rows, and direction in {"N", "E", "S", "W"}.
  • 0 <= len(crates) <= 10^5
  • Every crate [cx, cy] satisfies 0 <= cx < cols and 0 <= cy < rows. Duplicate crates may appear.
  • The starting cell is never a crate.
  • 0 <= len(instructions) <= 10^5
  • instructions contains only the characters L, R, M, B.

Examples

Example 1

Input: run_rover(5, 5, [1, 1, 'N'], [[1, 3]], 'MMRMB')

Output: [1, 2, 'E']

Example 2

Input: run_rover(3, 3, [0, 0, 'S'], [], 'MLM')

Output: [1, 0, 'E']

Hints

Hint 1

The rover's whole state is just its x, y and facing. Each instruction can be applied to that state in constant time, so there is no need to build the grid (it can have 10^8 cells).

Hint 2

Checking whether a target cell holds a crate by scanning the list would be too slow for 10^5 steps. Convert the crate list once into a set of tuples so membership checks are O(1).

Hint 3

Keep the four directions in clockwise order, N, E, S, W. A right turn adds 1 to the index and a left turn subtracts 1, both modulo 4. For a backward step, negate the forward (dx, dy) vector. Compute the target cell first and only commit it if it is on the floor and not a crate.