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.
int shortestWalk(List<String> park)
-1 if the stand cannot be reached.int shortestWalkWithShoveling(List<String> park, int shovelLimit)
shovelLimit snowy squares along the way.-1 if the stand still cannot be reached.int fewestSquaresToShovel(List<String> park)
1 ≤ rows, cols ≤ 40park is '.' or '*'.(0, 0) and (rows - 1, cols - 1) are '.'.0 ≤ shovelLimit ≤ rows * colsIn the explanations, a route lists the start, every turn and the end. The keeper walks in a straight line between them.
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.
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.
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.
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.
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.
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.