505. Shortest Walk Through a Snowy Park With Shoveling
Asked in
Shortest Walk Through a Snowy Park With Shoveling

After a snowstorm, a park is a grid of squares. Each square is either clear ('.') or covered with snow ('*').

The park keeper walks from the gate in the top-left square to the hot cocoa stand in the bottom-right square. In one step, the keeper moves to the next square up, down, left or right, and never leaves the park. The keeper can walk through a snowy square only after shoveling it. Shoveling a square makes it clear and does not cost an extra step.

park has rows strings, and each string has cols characters. The character at index c of the string at index r is square (r, c). The gate is (0, 0) and the stand is (rows - 1, cols - 1).

Implement the three methods below. Each one is about the shortest walk under different shoveling rules.

Method Signatures

Part 1

int shortestWalk(List<String> park)
  • No square may be shoveled.
  • Returns the fewest steps from the gate to the stand, or -1 if the stand cannot be reached.

Part 2

int shortestWalkWithShoveling(List<String> park, int shovelLimit)
  • The keeper may shovel at most shovelLimit snowy squares along the way.
  • Returns the fewest steps from the gate to the stand, or -1 if the stand still cannot be reached.

Part 3

int fewestSquaresToShovel(List<String> park)
  • The keeper is in a hurry and wants the walk to be as short as it would be on a day with no snow at all.
  • Returns the fewest snowy squares that must be shoveled to make such a walk possible.

Constraints

  • 1 ≤ rows, cols ≤ 40
  • Every character in park is '.' or '*'.
  • Squares (0, 0) and (rows - 1, cols - 1) are '.'.
  • 0 ≤ shovelLimit ≤ rows * cols

Examples

In the explanations, a route lists the start, every turn and the end. The keeper walks in a straight line between them.

Example 1

Input: shortestWalk(park = ["......", ".***.*", "**....", "*...**", "*.****", "......"])

Output: 16

Explanation: One shortest clear route is (0,0) → (0,4) → (2,4) → (2,2) → (3,2) → (3,1) → (5,1) → (5,5). It has to go left 3 times to get around the snow, so it takes 16 steps.

Example 2

Input: shortestWalk(park = [".*...", "**..*", "*...*", "*.***", "*..*."])

Output: -1

Explanation: Both neighbors of the gate, (0,1) and (1,0), are snowy. So the keeper cannot even leave the gate without shoveling.

Example 3

Input: shortestWalkWithShoveling(park = ["......", ".***.*", "**....", "*...**", "*.****", "......"], shovelLimit = 1)

Output: 12

Explanation: Shovel (1,2) and walk (0,0) → (0,2) → (3,2) → (3,1) → (5,1) → (5,5). This takes 12 steps. A 10-step walk would need 2 shovels.

Example 4

Input: shortestWalkWithShoveling(park = [".*...", "**..*", "*...*", "*.***", "*..*."], shovelLimit = 2)

Output: 10

Explanation: The gate's neighbors, (0,1) and (1,0), and the stand's neighbors, (3,4) and (4,3), are all snowy. So the keeper needs one shovel to leave the gate and another to reach the stand. Shovel (0,1) and (4,3) and walk (0,0) → (0,2) → (2,2) → (2,1) → (4,1) → (4,4). This takes 10 steps. With shovelLimit = 1, the answer would be -1.

Example 5

Input: fewestSquaresToShovel(park = ["......", ".***.*", "**....", "*...**", "*.****", "......"])

Output: 2

Explanation: With no snow, the walk would take 5 + 5 = 10 steps. Shoveling (3,5) and (4,5) allows the 10-step walk (0,0) → (0,4) → (2,4) → (2,5) → (5,5). Every 10-step walk crosses at least 2 snowy squares, even though the clear route in Example 1 needs no shoveling.

Example 6

Input: fewestSquaresToShovel(park = [".*...", "**..*", "*...*", "*.***", "*..*."])

Output: 3

Explanation: With no snow, the walk would take 4 + 4 = 8 steps. Shoveling (0,1), (2,4) and (3,4) allows the 8-step walk (0,0) → (0,3) → (2,3) → (2,4) → (4,4). No 8-step walk crosses fewer than 3 snowy squares. Example 4 reaches the stand with only 2 shovels, but that walk takes 10 steps.



Please use Laptop/Desktop or any other large screen to add/edit code.