This is a pretty simple grid-BFS problem. To find the tile with minimum distance, we need to count the distances to tiles. We can count these distances by BFS'ing the whole grid at each building and summing up the tile distances in some matrix. Let's assume that we have visited some tiles for some buildings. In some other building, if any tile around it can visit hasn't been visited by all the buildings before it, that means that there is no shortest distance for all buildings.
This can be implemented by adding a check to the BFS, where this visit count property can hinder the process. We can also use the given space to count the visits in the given grid matrix. Eventually, we return the least distance we encountered for the last building.
Resolves: #230
Algorithm:
This is a pretty simple grid-BFS problem. To find the tile with minimum distance, we need to count the distances to tiles. We can count these distances by BFS'ing the whole grid at each building and summing up the tile distances in some matrix. Let's assume that we have visited some tiles for some buildings. In some other building, if any tile around it can visit hasn't been visited by all the buildings before it, that means that there is no shortest distance for all buildings.
This can be implemented by adding a check to the BFS, where this visit count property can hinder the process. We can also use the given space to count the visits in the given grid matrix. Eventually, we return the least distance we encountered for the last building.