Shortest Atom Action Path
Problem statement
An atom starts at cell start in a rectangular grid and must reach cell target. A cell containing . is allowed; a cell containing # is forbidden. Coordinates are zero-based [row, column] pairs, with rows increasing downward and columns increasing to the right.
The input actions defines the available moves. Action i changes cell (r, c) to (r + actions[i][0], c + actions[i][1]). Every action costs one step and may be used repeatedly.
Examples
Example 1
grid = ["...","..."]start = [0,0]target = [1,2]actions = [[0,1],[1,0],[0,-1],[-1,0]]return = [0,0,1]The atom visits (0,0), (0,1), (0,2), (1,2). Three actions are necessary. Of the shortest sequences, [0,0,1] is lexicographically smallest.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.