KatInterview

Locked Gates Grid: Fewest Steps With Collected Keys

Trees & GraphsAuto-graded · 20 test cases
MediumAuto-gradedTrees & Graphsbfsgridstate-space-search

Last updated

Find the fewest moves through a grid where lowercase cells give keys and uppercase cells are gates, using BFS over position plus the set of collected keys.

You are given a rectangular grid of text rows. Starting from the cell marked S, find the fewest moves needed to stand on the cell marked T.

Cell meanings

  • S is the starting cell. You begin there holding no keys.
  • T is the destination cell.
  • # is a wall and can never be entered.
  • A lowercase letter is a key cell. Stepping onto it permanently adds that letter to your key set. Keys are not consumed.
  • An uppercase letter is a gate. You may step onto it only if you already hold the matching lowercase key.
  • Any other character (for example .) is an open cell.

Important rule about S and T

Every uppercase letter is a gate, and this includes S and T. This has two consequences:

  • You may step onto a T cell only if you already hold the key t, which means you must have stepped onto a lowercase t cell earlier.
  • You may step back onto an S cell after leaving it only if you hold the key s.

The start position itself needs no key. If the grid contains several S or several T characters, the last one in row-major order (top to bottom, left to right) is the real start or destination. All other copies behave as ordinary gates that need s or t respectively.

Movement

  • One move goes to an adjacent cell: up, down, left, or right.
  • You cannot leave the grid.
  • Each move costs 1.
  • The key check applies to the cell you are moving onto. A key you pick up counts immediately for the next move.

Task

Return the minimum number of moves needed to stand on T. Return -1 if it cannot be reached. Also return -1 if the grid is empty, its first row is empty, or it has no S or no T.

Constraints

  • maze is a list of strings, all of equal length.
  • The grid is small enough for a search over (cell, key set) states.
  • Letters are English letters; keys use lowercase a-z and gates use uppercase A-Z.
  • A gate whose key does not exist in the grid acts as a wall.

Examples

Example 1

Input: min_steps_with_keys(['S.aA.T'])

Output: -1

Example 2

Input: min_steps_with_keys(['S#.#T', '.a.b.', '#####'])

Output: -1

Example 3

Input: min_steps_with_keys(['S..a#', '##.A#', '...T#'])

Output: -1

Example 4

Input: min_steps_with_keys(['S..#B', 'a..#.', '...bT'])

Output: -1

Hints

Hint 1

Your position alone is not enough to describe where you are. Which cells you may enter depends on the keys you hold, so the state must include them.

Hint 2

Use BFS from the start, with each state being (row, column, set of keys). Mark a state visited when you enqueue it, and stop when you dequeue a state whose cell is the destination.

Hint 3

Test each neighbor in this order: bounds, wall, then any uppercase letter (including S and T) against the key set. Only after that do you add a lowercase letter to the key set.