中级农民
- 积分
- 217
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-11-30
- 最后登录
- 1970-1-1
|
317. Shortest Distance from All Buildings (hard)
直观的BFS题目,写起来比较冗长,55分钟AC
- class Solution {
- public:
- int shortestDistance(vector<vector<int>>& grid){
- /* 0. MISC */
-
- /* 1. prep */
- vector<vector<int> > totalDistGrid(grid.size(), vector<int>(grid[0].size(), 0));
- vector<vector<int> > reachableBuildings(grid.size(), vector<int>(grid[0].size(), 0));
- int numBuilding = CountBuilding(grid);
-
- /* 2. key algo */
- for(int i = 0; i < grid.size(); ++i){
- for(int j = 0; j < grid[i].size(); ++j){
- if(grid[i][j] == 1){
- UpdateGridBFS(grid, totalDistGrid, reachableBuildings, {i, j});
- }
- }
- }
-
- /* 3. answer */
- return MinQualifiedElement(grid, totalDistGrid, reachableBuildings, numBuilding);
- }
-
- void UpdateGridBFS( const vector<vector<int>>& grid,
- vector<vector<int> >& totalDistGrid,
- vector<vector<int> >& reachableBuildings,
- const pair<int, int>& loc){
- /* 0. MISC */
-
- /* 1. prep */
- vector<vector<int> > distGrid(grid.size(), vector<int>(grid[0].size(), -1));
- vector<vector<int> > visited(grid.size(), vector<int>(grid[0].size(), 0));
-
- queue<pair<int, int> > bfsQueue;
- bfsQueue.emplace(loc);
- visited[loc.first][loc.second] = 1;
- distGrid[loc.first][loc.second] = 0;
-
- vector<pair<int, int> > directions =
- {
- pair<int, int>(-1, 0),
- pair<int, int>(1, 0),
- pair<int, int>(0, -1),
- pair<int, int>(0, 1)
- };
-
- /* 2. key algo */
- while(!bfsQueue.empty()){
- auto node = bfsQueue.front();
- bfsQueue.pop();
-
- for(auto dir: directions){
- pair<int, int> neighbor(node.first + dir.first, node.second + dir.second);
- if(!InRange(grid, neighbor))continue;
- if(grid[neighbor.first][neighbor.second] != 0)continue;
- if(visited[neighbor.first][neighbor.second] != 0)continue;
-
- visited[neighbor.first][neighbor.second] = 1;
- distGrid[neighbor.first][neighbor.second] = distGrid[node.first][node.second] + 1;
-
- bfsQueue.emplace(neighbor);
- }
- }
-
- /* 3. answer */
- MatrixAdd(reachableBuildings, move(visited));
- MatrixAdd(totalDistGrid, move(distGrid));
- }
-
- void MatrixAdd(vector<vector<int>>& base, const vector<vector<int>>& addition){
- if(base.empty() || addition.empty())return;
- if(base.size() != addition.size() || base[0].size() != addition[0].size())return;
-
- for(int i = 0; i < base.size(); ++i){
- for(int j = 0; j < base[i].size(); ++j){
- base[i][j] += addition[i][j];
- }
- }
- }
-
- int MinQualifiedElement(const vector<vector<int>>& grid,
- const vector<vector<int>>& distGrid,
- const vector<vector<int>>& buildingCountGrid,
- int numBuilding){
- int ans = -1;
- for(int i = 0; i < distGrid.size(); ++i){
- for(int j = 0; j < distGrid[i].size(); ++j){
- if(buildingCountGrid[i][j] != numBuilding)continue;
- if(grid[i][j] != 0)continue;
- if(ans == -1)ans = distGrid[i][j];
- else ans = min(ans, distGrid[i][j]);
- }
- }
- return ans;
- }
-
- bool InRange(const vector<vector<int>>& grid, const pair<int, int>& loc){
- return loc.first >= 0 && loc.first < grid.size() &&
- loc.second >= 0 && loc.second < grid[loc.first].size();
- }
-
- int CountBuilding(const vector<vector<int>>& grid){
- int ans = 0;
- for(auto row: grid){
- for(int n: row){
- if(n == 1)++ans;
- }
- }
- return ans;
- }
-
- void Print2DMatrix(const vector<vector<int> >& matrix){
- cout << "matrix: " << endl;
- for(auto row: matrix){
- for(int n: row)
- cout << n << " ";
- cout << endl;
- }
- }
- };
复制代码 |
|