KatInterview

Clockwise Grid Walk Around Obstacles and Visited Cells

Arrays & HashingAuto-graded · 26 test cases
MediumAuto-gradedArrays & Hashingmatrixsimulationspiral

Last updated

Simulate a clockwise inward walk over a grid with obstacle cells, turning on edges, obstacles and visited cells, and stopping when every direction is closed.

You are given a rectangular grid of integers and a list of obstacle cells. Starting at the top-left corner, you walk across the grid in a clockwise, inward-spiralling pattern, and you must report the values of the cells in the order you step on them.

With no obstacles, the walk is the familiar spiral: along the top row to the right, down the right column, back along the bottom row to the left, up the left column, and then inward around the remaining cells. Obstacles change this: the walk never enters an obstacle cell and instead turns clockwise, exactly as it does at the grid boundary or at a cell it has already stepped on. One function must handle both the obstacle-free and the obstacle case.

Task

Implement walk_spiral_with_obstacles(grid, obstacles), which returns the list of values visited, in visiting order.

  • grid is a list of m rows, each with n integers.
  • obstacles is a list of [row, col] pairs, each naming an obstacle cell.

Rules

  • Row 0 is the top row and column 0 is the leftmost column. From (r, c): right is (r, c + 1), down is (r + 1, c), left is (r, c - 1), up is (r - 1, c). A clockwise turn changes right to down, down to left, left to up, and up to right.
  • A cell is available if it lies inside the grid, is not an obstacle, and has not been visited yet.
  • The walk starts on (0, 0) facing right. That cell counts as visited and its value is output first.
  • At each step, look at the cell next in the current facing direction. If it is available, move onto it and output its value. Otherwise turn clockwise by 90 degrees and look again, trying at most four directions in total (the current one plus up to three turns). The first direction whose next cell is available becomes the new facing direction, and the walk moves one cell that way.
  • If none of the four directions leads to an available cell, the walk ends. Open cells that were never reached are not output.
  • Obstacle cells are never output.

Constraints

  • 1 <= m, n <= 100, where m = len(grid) and n = len(grid[0]); every row has length n.
  • -10^9 <= grid[r][c] <= 10^9
  • 0 <= len(obstacles) <= m * n - 1
  • All obstacle cells are inside the grid and pairwise distinct, and [0, 0] is never an obstacle.

Examples

Example 1

Input: walk_spiral_with_obstacles([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [])

Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]

Example 2

Input: walk_spiral_with_obstacles([[1, 2, 3], [4, 5, 6]], [[0, 1]])

Output: [1, 4, 5, 6, 3]

Example 3

Input: walk_spiral_with_obstacles([[1, 2, 3, 4], [5, 6, 7, 8]], [[1, 3]])

Output: [1, 2, 3, 4]

Hints

Hint 1

Do not shrink top/bottom/left/right boundaries: obstacles make the unvisited area non-rectangular. Simulate the walk cell by cell instead.

Hint 2

Keep one boolean grid meaning 'cannot enter', pre-filled with the obstacles and updated whenever you visit a cell. Then a single test (in bounds and not marked) covers all three reasons to turn.

Hint 3

Store the heading as an index into the list right, down, left, up. At each step try headings (d + t) % 4 for t = 0..3, and only commit the heading when a move succeeds. If all four fail, stop.