All problems
0466HardArrayBit ManipulationBreadth-First SearchMatrix

Wrench Run Through the Utility Vault

Tracked in this browser only
Write code

Trains the technique from

LeetCode 864Shortest Path to Get All Keys

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A splice crew has a floor plan of an underground utility vault, handed over as vault: a list of equal-length strings, one per row, read top to bottom and left to right.

Each character says what is in that cell:

  • "@" is the access hatch the crew climbs in through. Exactly one cell holds it.
  • "." is bare floor.
  • "#" is solid rock.
  • a lowercase letter is a coded wrench lying on the floor.
  • an uppercase letter is a bolted gate that only the wrench with the same letter can turn.

From the cell it stands on the crew may step to the cell directly above, below, left or right. Each such step counts as one move. It may never step outside the plan and never into rock. Stepping onto a wrench picks it up at no cost, and a wrench once picked up is carried for the rest of the run. Stepping onto a gate is only allowed while carrying its matching wrench, and costs nothing beyond the step itself; the gate stays open afterwards. Bare floor and the hatch cell are always free to step on.

Return the fewest moves needed until the crew is carrying every wrench on the plan. Return -1 when no sequence of moves collects them all.

Examples

Example 1

Input
vault = ["@..a#", "##.#.", "b.A.."]
Output
8

One run of eight moves works: three steps east onto wrench `"a"`, one step back west, two steps south through gate `"A"`, then two steps west onto wrench `"b"`.

Example 2

Input
vault = ["@#a", "###", "A#."]
Output
-1

Rock sits east and south of the hatch, so the crew cannot take a single step and wrench `"a"` is never reached.

Example 3

Input
vault = ["@.a.#.", ".#.#.b", "..A..#", "c#..B.", "...#.C"]
Output
14

Three wrenches lie on this plan and a run of fourteen moves ends with all three in hand, having opened gates `"A"` and `"B"` along the way.

Constraints

  • rows == len(vault)
  • cols == len(vault[i])
  • 1 <= rows, cols <= 30
  • vault[i][j] is an English letter, '.', '#', or '@'.
  • There is exactly one '@' cell.
  • The number of wrenches k satisfies 1 <= k <= 6, and they are labelled with the first k lowercase letters.
  • Each wrench letter appears in exactly one cell, and its uppercase gate appears in exactly one cell.

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def fewest_moves_for_wrenches(vault: list[str]) -> int:
Java
public int fewestMovesForWrenches(String[] vault)
September 7
Apply