You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or 2, where:
- Each 0 marks an empty land which you can pass by freely.
- Each 1 marks a building which you cannot pass through.
- Each 2 marks an obstacle which you cannot pass through.
For example, given three buildings at
(0,0)
, (0,4)
, (2,2)
, and an obstacle at (0,2)
:1 - 0 - 2 - 0 - 1 | | | | | 0 - 0 - 0 - 0 - 0 | | | | | 0 - 0 - 1 - 0 - 0
The point
(1,2)
is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal. So return 7.
Note:
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.
Solution:
Use BFS, and only expand to empty land which can be reached by all previous tested buildings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int shortestDistance(vector<vector<int>>& grid) { | |
if(grid.empty()) | |
return -1; | |
int rows = grid.size(); | |
int cols = grid[0].size(); | |
vector<vector<int> > sumDist(rows, vector<int>(cols, 0)); | |
for(int i=0; i<rows; i++){ | |
for(int j=0; j<cols; j++){ | |
if(grid[i][j]!=1) | |
continue; | |
vector<vector<int> > dist(rows, vector<int>(cols, -1)); | |
dist[i][j]=0; | |
queue<vector<int> > myQ; | |
vector<int> tmp={i, j, 0}; | |
myQ.push(tmp); | |
while(!myQ.empty()){ | |
vector<int> front = myQ.front(); | |
myQ.pop(); | |
int r=front[0]; | |
int c=front[1]; | |
int s=front[2]; | |
if(r!=0 && grid[r-1][c]!=2 && grid[r-1][c]!=1 && (s+1<dist[r-1][c] || dist[r-1][c]==-1) && sumDist[r-1][c]!=-1){ | |
vector<int> tmp={r-1, c, s+1}; | |
dist[r-1][c]=s+1; | |
myQ.push(tmp); | |
} | |
if(r!=rows-1 && grid[r+1][c]!=2 && grid[r+1][c]!=1 && (s+1<dist[r+1][c] || dist[r+1][c]==-1) && sumDist[r+1][c]!=-1){ | |
vector<int> tmp={r+1, c, s+1}; | |
dist[r+1][c]=s+1; | |
myQ.push(tmp); | |
} | |
if(c!=0 && grid[r][c-1]!=2 && grid[r][c-1]!=1 && (s+1<dist[r][c-1] || dist[r][c-1]==-1) && sumDist[r][c-1]!=-1){ | |
vector<int> tmp={r, c-1, s+1}; | |
dist[r][c-1]=s+1; | |
myQ.push(tmp); | |
} | |
if(c!=cols-1 && grid[r][c+1]!=2 && grid[r][c+1]!=1 && (s+1<dist[r][c+1] || dist[r][c+1]==-1) && sumDist[r][c+1]!=-1){ | |
vector<int> tmp={r, c+1, s+1}; | |
dist[r][c+1]=s+1; | |
myQ.push(tmp); | |
} | |
} | |
for(int r=0; r<rows; r++){ | |
for(int c=0; c<cols; c++){ | |
if((grid[r][c]==0&&dist[r][c]==-1)||(sumDist[r][c]==-1)){ | |
sumDist[r][c]=-1; | |
continue; | |
} | |
sumDist[r][c]+=dist[r][c]; } | |
} | |
} | |
} | |
int minv=INT_MAX; | |
for(int i=0; i<rows; i++){ | |
for(int j=0; j<cols; j++){ | |
if(grid[i][j]!=0||sumDist[i][j]==-1) | |
continue; | |
minv = min(minv, sumDist[i][j]); | |
} | |
} | |
return minv==INT_MAX? -1:minv; | |
} | |
}; |
No comments:
Post a Comment