KatInterview

Sliding Marble on a Tiltable Grid with Scheduled Tilts

Arrays & HashingAuto-graded · 21 test cases
EasyAuto-gradedArrays & Hashingsimulationgridtime-segments

Last updated

Simulate a marble on a grid whose tilt changes at scheduled times, and find its final cell efficiently when the end time can reach a billion seconds.

A rectangular grid contains a single marble and sits on a platform that can be tilted. The platform is always in exactly one of five modes: "up", "down", "left", "right" or "flat".

Whenever the platform is tilted (any mode other than "flat"), the marble tries to move one cell per second in the tilt direction. It moves only if the neighboring cell in that direction is inside the grid and open.

You are given the grid, the marble's starting cell, a list of scheduled tilt changes, and a finishing time. Return the cell where the marble is at the finishing time.

Function

Implement slide_marble(grid, start, changes, end_time).

  • grid is a list of strings. grid[i][j] is '#' for a wall and '.' for an open cell.
  • start is [row, col], the starting cell.
  • changes is a list of (time, mode) pairs. Each pair sets the platform to mode starting at second time.
  • end_time is the finishing time.
  • Return [row, col], the marble's cell at time end_time.

Rules

  • Row 0 is the top row and column 0 is the leftmost column. From (row, col), "up" leads to (row - 1, col), "down" to (row + 1, col), "left" to (row, col - 1) and "right" to (row, col + 1).
  • Time is counted in whole seconds. Before the first change, the platform is "flat". A change (t, mode) takes effect at the start of second t and lasts until the next change.
  • For each second s = 0, 1, ..., end_time - 1, the mode during that second is the mode of the last change with time <= s, or "flat" if there is no such change.
  • If that mode is not "flat" and the neighbor in that direction is inside the grid and open, the marble moves into it at the end of that second. Otherwise it stays where it is.
  • Cells outside the grid count as walls, so the marble can never leave the grid.
  • Changes with time >= end_time have no effect.
  • If end_time is 0, the answer is start.

Constraints

  • 1 <= len(grid) <= 100 and 1 <= len(grid[0]) <= 100. All rows have equal length and contain only '#' and '.'.
  • start is an open cell.
  • 0 <= len(changes) <= 10^4
  • 0 <= time <= 10^9 for every change, and change times are strictly increasing.
  • Every mode is one of "up", "down", "left", "right", "flat".
  • 0 <= end_time <= 10^9

Examples

Example 1

Input: slide_marble(['#######', '#.....#', '#.###.#', '#.....#', '#######'], [1, 1], [[0, 'right'], [2, 'flat'], [5, 'down'], [8, 'right']], 12)

Output: [1, 5]

Example 2

Input: slide_marble(['#######', '#.....#', '#.###.#', '#.....#', '#######'], [1, 5], [[1, 'down'], [3, 'left'], [100, 'up']], 103)

Output: [1, 1]

Example 3

Input: slide_marble(['..', '..'], [0, 0], [[0, 'left'], [1, 'down'], [2, 'right'], [4, 'up']], 4)

Output: [1, 1]

Hints

Hint 1

Simulating every single second is too slow when end_time can be 10^9. Notice that the tilt mode only changes at the scheduled times.

Hint 2

Split the timeline into segments between consecutive changes (the last segment ends at end_time, and changes at or after end_time are dropped). Inside one segment the direction is fixed.

Hint 3

Within a segment, once the marble is blocked by a wall or the edge it stays blocked for the rest of that segment, so stop early. Each segment then costs at most about max(rows, cols) moves.