Shortest Path to Get All Keys
Real-world version: Wrench Run Through the Utility VaultThe statement here is our own: the same inputs, outputs and technique.
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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def fewest_moves_for_wrenches(vault: list[str]) -> int:public int fewestMovesForWrenches(String[] vault)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 7 test cases: 7 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's