542. 01 Matrix 寻找最近的0 重点 BFS 动态规划
2017-09-15 10:52
281 查看
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.
Example 1:
Input:
Output:
Example 2:
Input:
Output:
Note:
The number of elements of the given matrix will not exceed 10,000.
There are at least one 0 in the given matrix.
The cells are adjacent in only four directions: up, down, left and right.
寻找最近的0
(2)宽度优先遍历 时间复杂度
1.设置一个距离二维数组全部设置为最大值
2.遍历一遍数组找到所有的0,讲他的距离设置成0,并将其位置加入到队列中去
3.做下面的循环:当队列不为空时,依次弹出队头,对于每一个队头,考察他的四周,如果四周可以变得更小,就把四周的值更新,并把这个位置加入到队列末尾
思考:
1.为什么使用队列?
新加入的数组放到队列的末尾,这样保证所有的0先处理完,然后再处理0周围加入队列的位置,这些位置的值要大于0,。这么做是首先就让0附近的位置值得到最优结果,以后如果再遇到这个位置就不会再计算了。这个过程相当于从每个0位置向外扩散遍历,先把所有的0都遍历一遍,这样,所有距离为一的点就遍历完了,然后再遍历所有的距离为1的周围的四个点,如果正好有个点是0,这个位置不做处理。
宽度优先遍历的思路和树的层序遍历类似。0位置的点相当于上一层的节点,距离为1位置的点相当于下一层的点。
2.只把当前节点可以更新的位置加到队列中去,会不会存在没有遍历到的位置?
不会,哪怕是只有一个0位置,因为周围的初始值都是比他大的,会逐渐的扩散出去。另外,如果一个点某个周围的点的距离值比他小,说明这个周围的点已经遍历过了,这个点的周围能加入的点已经加入到队列中去了。
如果不这样做,程序会进入死循环。
代码如下:
直接抄袭代码:
The distance between two adjacent cells is 1.
Example 1:
Input:
0 0 0 0 1 0 0 0 0
Output:
0 0 0 0 1 0 0 0 0
Example 2:
Input:
0 0 0 0 1 0 1 1 1
Output:
0 0 0 0 1 0 1 2 1
Note:
The number of elements of the given matrix will not exceed 10,000.
There are at least one 0 in the given matrix.
The cells are adjacent in only four directions: up, down, left and right.
寻找最近的0
(1)暴力解法
对于每一个位置,都搜索所有的位置,并计算与当前位置的距离,找到最小的那个0的位置(2)宽度优先遍历 时间复杂度
O(r
\cdot c) O(r⋅c)
长宽
1.设置一个距离二维数组全部设置为最大值2.遍历一遍数组找到所有的0,讲他的距离设置成0,并将其位置加入到队列中去
3.做下面的循环:当队列不为空时,依次弹出队头,对于每一个队头,考察他的四周,如果四周可以变得更小,就把四周的值更新,并把这个位置加入到队列末尾
思考:
1.为什么使用队列?
新加入的数组放到队列的末尾,这样保证所有的0先处理完,然后再处理0周围加入队列的位置,这些位置的值要大于0,。这么做是首先就让0附近的位置值得到最优结果,以后如果再遇到这个位置就不会再计算了。这个过程相当于从每个0位置向外扩散遍历,先把所有的0都遍历一遍,这样,所有距离为一的点就遍历完了,然后再遍历所有的距离为1的周围的四个点,如果正好有个点是0,这个位置不做处理。
宽度优先遍历的思路和树的层序遍历类似。0位置的点相当于上一层的节点,距离为1位置的点相当于下一层的点。
2.只把当前节点可以更新的位置加到队列中去,会不会存在没有遍历到的位置?
不会,哪怕是只有一个0位置,因为周围的初始值都是比他大的,会逐渐的扩散出去。另外,如果一个点某个周围的点的距离值比他小,说明这个周围的点已经遍历过了,这个点的周围能加入的点已经加入到队列中去了。
如果不这样做,程序会进入死循环。
代码如下:
class Solution { public: vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) { int row=matrix.size(); if(row==0) return matrix; int col=matrix[0].size(); vector<vector<int>> res(row,vector<int>(col,numeric_limits<int>::max())); queue<pair<int,int>> q; for(int i=0;i<row;i++) for(int j=0;j<col;j++) { if(matrix[i][j]==0) { q.push({i,j}); res[i][j]=0; } } int sear[4][2]={{1,0},{-1,0},{0,1},{0,-1}};//设置遍历的方向数组 while(!q.empty()) { pair<int,int> cur=q.front(); q.pop(); for(int i=0;i<4;i++) { int newRow=cur.first+sear[i][0],newCol=cur.second+sear[i][1]; if(newRow>=0&&newRow<row&&newCol>=0&&newCol<col) { if(res[newRow][newCol]>res[cur.first][cur.second]+1) { res[newRow][newCol]=res[cur.first][cur.second]+1; q.push({newRow,newCol}); } } } } return res; } };
(3)动态规划的方法
对于每一个1,他的距离值是周围距离最小的那个加一。使用动态规划,第一次从左上角遍历到有下角,第二次从右下角遍历到左上角。动态规划的效率更高一些,省去了一些的比较(虽然在BFS中这些多于的比较只是做了比较)。直接抄袭代码:
vector<vector<int> > updateMatrix(vector<vector<int> >& matrix) { int rows = matrix.size(); if (rows == 0) return matrix; int cols = matrix[0].size(); vector<vector<int> > dist(rows, vector<int>(cols, INT_MAX - 100000)); //First pass: check for left and top for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (matrix[i][j] == 0) dist[i][j] = 0; else { if (i > 0) dist[i][j] = min(dist[i][j], dist[i - 1][j] + 1); if (j > 0) dist[i][j] = min(dist[i][j], dist[i][j - 1] + 1); } } } //Second pass: check for bottom and right for (int i = rows - 1; i >= 0; i--) { for (int j = cols - 1; j >= 0; j--) { if (i < rows - 1) dist[i][j] = min(dist[i][j], dist[i + 1][j] + 1); if (j < cols - 1) dist[i][j] = min(dist[i][j], dist[i][j + 1] + 1); } } return dist; }
相关文章推荐
- [Leetcode] #542 01 Matrix (BFS)
- Leetcode 542. 01 Matrix(BFS)
- leetcode 542. 01 Matrix 距离0最近的距离 + 正反遍历 + 动态规划DP
- 542. 01 Matrix(BFS)
- (M)BFS:542. 01 Matrix
- 542. 01 Matrix
- 542. 01 Matrix
- 算法分析与设计课程06——542. 01 Matrix(Medium)
- 542. 01 Matrix -- Medium
- Add to List 542. 01 Matrix
- git是一种分布式代码管理工具,git通过树的形式记录文件的更改历史,比如: base'<--base<--A<--A' ^ | --- B<--B' 小米工程师常常需要寻找两个分支最近的分割点,即base.假设git 树是多叉树,请实现一个算法,计算git树上任意两点的最近分割点。 (假设git树节点数为n,用邻接矩阵的形式表示git树:字符串数组matrix包含n个字符串,每个字符串由字符'0
- leetcode 542. 01 Matrix
- LeetCode 542. 01 Matrix
- [LeetCode] 542. 01 Matrix
- 【算法分析与设计】【第十二周】542. 01 Matrix
- Leetcode-542. 01 Matrix
- [LeetCode]542. 01 Matrix
- [leetcode]-542 01 Matrix
- 【LeetCode】542. 01 Matrix
- LeetCode - 542 - 01 Matrix