Santa Claus ๐ has to deliver presents in a town represented as a grid map.
Each cell on the map can be:
'S' โ Santa's starting point (where the presents are)'G' โ House that must receive a present'.' โ Free path'#' โ Obstacle (cannot be crossed)Santa makes independent deliveries for each present. He leaves from 'S', delivers the present to a house 'G', and immediately returns to 'S' to pick up the next one. However, for this challenge, we only want to calculate the sum of the minimum one-way distances from 'S' to each house 'G'.
---
Write the function minStepsToDeliver(map) that returns the total number of steps required to reach all the houses with presents from the starting position.
Keep in mind:
'S'.'S' to that house 'G'.'#') cannot be crossed.-1.๐งฉ Examples
minStepsToDeliver([
['S', '.', 'G'],
['.', '#', '.'],
['G', '.', '.']
])
// Result: 4
/*
Explanation:
- Minimum distance from S (0,0) to G (0,2): 2 steps
- Minimum distance from S (0,0) to G (2,0): 2 steps
- Total: 2 + 2 = 4
*/
minStepsToDeliver([
['S', '#', 'G'],
['#', '#', '.'],
['G', '.', '.']
])
// Result: -1
// (The house at (0,2) is unreachable due to obstacles)
minStepsToDeliver([['S', 'G']])
// Result: 1
๐ฏ Rules
'S'.'G').'S'.๐ง Hint
'S' to each 'G' (you can use a Breadth-First Search or BFS algorithm).-1.