KatInterview

Sliding Token on a Grid with Timed Heading Changes

Arrays & HashingAuto-graded · 21 test cases
MediumAuto-gradedArrays & Hashingsimulationgridintervals

Last updated

Compute a token's final cell on a grid with blocked cells when its heading changes at given timestamps up to 10^9, which requires processing time segments instead of simulating each tick.

A token sits on a rectangular board. A schedule of timed commands controls which way it heads: up, down, left or right. While a heading is active, the token tries to advance one cell per tick. If the next cell in that heading cannot be entered, the token stays where it is until a later command changes the heading.

Given the board, the starting cell, the schedule and a final time, return the token's cell at the final time.

Board legend (read carefully): the character 'o' is a free cell the token may enter, and the character '.' is a wall the token can never enter.

Function signature

def final_position(grid: List[str], start: List[int], actions: List[Tuple[int, str]], end_time: int) -> List[int]:

start is [row, col]. Each command is a pair (time, direction).

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).
  • Before the first command the token is idle. A command (t, d) makes d the active heading starting at tick t, and it remains active until the next command takes over.
  • Time is measured in whole ticks. For each tick s = 0, 1, ..., end_time - 1, the active heading is the one from the command with the largest time <= s. If no command qualifies, the token does not move in that tick.
  • If a heading is active and the adjacent cell in that heading lies inside the board and is 'o', the token moves into it at the end of that tick. Otherwise it stays put.
  • Anything outside the board counts as a wall, so the token never leaves the board.
  • Commands with time >= end_time have no effect.
  • Return the token's [row, col] after tick end_time - 1 has been processed, that is, at time end_time. If end_time is 0, return start.

Constraints

  • 1 <= len(grid) <= 500 and 1 <= len(grid[0]) <= 500; all rows have equal length and contain only 'o' and '.'.
  • start is a free ('o') cell inside the board.
  • 0 <= len(actions) <= 10^5
  • 0 <= time <= 10^9 for each command, and command times are strictly increasing.
  • Each direction is one of "up", "down", "left", "right".
  • 0 <= end_time <= 10^9

Examples

Example 1

Input: final_position(['ooooo', 'o.o.o', 'ooooo'], [0, 0], [[0, 'right'], [3, 'down'], [5, 'left']], 7)

Output: [0, 1]

Example 2

Input: final_position(['ooooo', 'o.o.o', 'ooooo'], [2, 4], [[2, 'left'], [4, 'up'], [50, 'right']], 60)

Output: [0, 4]

Example 3

Input: final_position(['ooo'], [0, 2], [[0, 'left'], [1, 'right'], [3, 'left']], 3)

Output: [0, 2]

Hints

Hint 1

end_time can reach 10^9, so stepping through every tick is too slow. The heading only changes at command times, so think in terms of the stretches between commands.

Hint 2

Command i controls the half-open time range [t_i, t_{i+1}), and the last remaining command controls [t_last, end_time). Discard commands with time >= end_time first. Inside one stretch the heading is fixed, so the token travels along a straight line.

Hint 3

Within a stretch, advance while you have time budget left and the next cell is inside the board and equal to 'o'. The moment it is blocked, stop that stretch, because nothing can change until the next command. Remember that 'o' is free and '.' is a wall.