KatInterview

Shortest Path Through a Grid With Keys and One-Way Portals

Trees & GraphsAuto-graded · 21 test cases
MediumAuto-gradedTrees & Graphsbfsdijkstrabitmask

Last updated

Tests BFS/Dijkstra on an augmented state space where position must be combined with a key-collection bitmask, and directional arrow tiles enforce constrained traversal.

Background

You are navigating an N × M grid from the top-left corner (0, 0) toward a single goal cell. The grid is filled with different types of tiles, and some tiles impose movement restrictions or bonuses.

Tile Types

SymbolMeaning
.Open floor — you may pass through freely.
#Wall — impassable.
KKey — picking it up adds it to your inventory.
TExit — you may only finish here once every key on the entire grid is in your inventory. Until then, T acts like a wall.
^ v < >Arrow tile — see rules below.

Arrow Rules

An arrow tile points in one of the four cardinal directions. Two rules govern them:

  1. Entry constraint. You may only step onto an arrow tile if you are already moving in the direction the arrow points. Trying to enter it from any other direction is forbidden.
  2. Forced continuation. The moment you land on an arrow tile, you are immediately pushed one additional cell in the same direction. That extra cell costs 1 step just like any other move.
    • If the forced destination is out of bounds, a wall, or an arrow that you cannot legally enter (wrong direction), the entire move is invalid.
    • If the forced destination is another valid same-direction arrow, the push repeats from that cell, consuming 1 step per cell.

Task

Implement a function that receives the grid and returns the minimum total number of cells entered on any valid path from (0, 0) to T while collecting all keys. Return -1 if no such path exists.

Every cell you move into (including cells entered through arrow-forced slides) counts as one step.

Constraints

  • 1 <= N, M <= 50
  • The number of K tiles in the grid is at most 5.
  • The starting cell (0, 0) is never a wall.
  • There is at most one T tile.
  • Arrow tiles only point in the four cardinal directions (^, v, <, >).

Notes

  • If (0, 0) contains a key, it is collected before any move is made.
  • If (0, 0) is already T and there are no keys anywhere, return 0.
  • Keys already collected are not collected a second time.

Examples

Example 1

Input: shortest_key_path([['.', 'K', '#'], ['^', '.', '.'], ['T', '.', '.']])

Output: 4

Hints

Hint 1

Position alone is not enough to define a state — two visits to the same cell with different keys collected are independent states that must both be explored.

Hint 2

Model this as a weighted shortest-path problem: a move that triggers a chain of k arrow slides costs k steps total, so edge weights are not uniform and a priority queue beats plain BFS.

Hint 3

Precompute a bit-index for every key cell so that your inventory can be represented as a single integer bitmask; the goal condition is simply mask == (1 << total_keys) - 1.