Shortest Distance from All Buildings
来源:互联网 发布:dash for mac 破解版 编辑:程序博客网 时间:2024/06/03 19:19
Problem
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.
Solution
对于每一个building, 计算它能到达的每一个空地的距离。具体来讲用 dist 数组纪录,最后找最小的就好。
到下一个building时,上一个如果没有到达这个空地,那这个空地就不考虑了。这个用 canReach 数组实现。
class Solution { vector<pair<int,int>> dirs = { make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)} ; void bfs( int curRow,int curCol,const vector<vector<int>>& grid,int numBuild,vector<vector<int>>& dist,vector<vector<int>>& canReach) { const int M = grid.size(), N = grid[0].size(); queue<int> curLvl, nextLvl; unordered_set<int> visited; curLvl.push(curRow*N + curCol); visited.insert(curRow*N + curCol); int curDist = 0; while(!curLvl.empty()) { int curBuild = curLvl.front(); curLvl.pop(); int curRow = curBuild/N, curCol = curBuild%N; for( int i = 0; i < dirs.size(); i++) { int nextRow = curRow + dirs[i].first, nextCol = curCol + dirs[i].second; int nextBuild = nextRow * N + nextCol; if( nextRow >= 0 && nextRow < M && nextCol >= 0 && nextCol < N && visited.find(nextBuild) == visited.end() && grid[nextRow][nextCol] == 0 && canReach[nextRow][nextCol] == numBuild ) { nextLvl.push(nextBuild); visited.insert(nextBuild); dist[nextRow][nextCol] += curDist + 1; canReach[nextRow][nextCol] = numBuild + 1; } } if(curLvl.empty()) { swap(curLvl, nextLvl); curDist++; } } return; }public: int shortestDistance(vector<vector<int>>& grid) { if(grid.empty() || grid[0].empty()) return 0; const int M = grid.size(), N = grid[0].size(); vector<vector<int>> dist( M, vector<int>(N, 0) ); vector<vector<int>> canReach( M, vector<int>(N, 0) ); int numBuild = 0; for( int row = 0; row < M; row++ ){ for( int col = 0; col < N; col++){ if( grid[row][col] == 1 ) { bfs(row, col, grid, numBuild++, dist, canReach ); } } } int rst = INT_MAX; for( int row = 0; row < M; row++ ){ for( int col = 0; col < N; col++){ if(canReach[row][col] == numBuild ) { rst = min( rst, dist[row][col]); } } } return rst==INT_MAX ? -1 : rst; }};
0 0
- Shortest Distance from All Buildings
- [LeetCode317]Shortest Distance from All Buildings
- Leetcode: Shortest Distance from All Buildings
- Leetcode 317. Shortest Distance from All Buildings
- 317. Shortest Distance from All Buildings
- [leetcode] 317. Shortest Distance from All Buildings 解题报告
- Leetcode 317. Shortest Distance from All Buildings(多次BFS)
- [Leetcode] 317. Shortest Distance from All Buildings 解题报告
- LeetCode 317. Shortest Distance from All Buildings(最短建筑距离)
- 1046. Shortest Distance
- pat 1046 Shortest Distance
- 1046. Shortest Distance (20)
- 1046. Shortest Distance
- 1046. Shortest Distance (20)
- 1046. Shortest Distance (20)
- 1046. Shortest Distance (20)
- 1046. Shortest Distance (20)
- PAT 1046. Shortest Distance
- Mac--快捷键
- LinkedBlockingQueue和ConcurrentLinkedQueue
- ArrayBlockingQueue、LinkedBlockingQueue、ConcurrentLinkedQueue
- 码农小汪-struts2学习3-struts2 配置文件说明
- 关于文件管理
- Shortest Distance from All Buildings
- Ext3.4 Column layout 设置form表单组件隐藏
- 关于size_t, ptrdiff_t, size_type, difference_type
- 简单的对象池实现
- 对象池的设计
- Asp.Net 关于FindControl方法使用
- googleCollection map 定时清除数据
- common,Google Guava,Guice
- Centos 6.5 更改系统时间