leetcode542 01 Matrix
思路:
多个起点的bfs。
实现:
class Solution
{
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix)
{
int n = matrix.size(), m = matrix[].size();
vector<vector<int>> vis(n, vector<int>(m, ));
vector<vector<int>> d(n, vector<int>(m, 0x3f3f3f3f));
queue<pair<int, int>> q;
int dx[] = {, , -, }, dy[] = {, , , -};
for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
if (matrix[i][j] == )
{
q.push(pair<int, int>(i, j));
vis[i][j] = ;
d[i][j] = ;
}
}
}
while (!q.empty())
{
pair<int, int> tmp = q.front();
q.pop();
int x = tmp.first, y = tmp.second;
for (int i = ; i < ; i++)
{
int nx = x + dx[i], ny = y + dy[i];
if (nx >= && nx < n && ny >= && ny < m && !vis[nx][ny])
{
vis[nx][ny] = ;
d[nx][ny] = d[x][y] + ;
q.push(pair<int, int>(nx, ny));
}
}
}
return d;
}
};
leetcode542 01 Matrix的更多相关文章
- [Leetcode Week10]01 Matrix
01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...
- 计算机学院大学生程序设计竞赛(2015’12)01 Matrix
01 Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- [leetcode] 542. 01 Matrix (Medium)
给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...
- leetcode 542. 01 Matrix 、663. Walls and Gates(lintcode) 、773. Sliding Puzzle 、803. Shortest Distance from All Buildings
542. 01 Matrix https://www.cnblogs.com/grandyang/p/6602288.html 将所有的1置为INT_MAX,然后用所有的0去更新原本位置为1的值. 最 ...
- hdu 01 Matrix
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- [Swift]LeetCode542. 01 矩阵 | 01 Matrix
Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance b ...
- [LeetCode] 01 Matrix 题解
题意 # 思路 我一开始的时候想的是嘴 # 实现 ```cpp // // include "../PreLoad.h" class Solution { public: /** ...
- LeetCode 542. 01 Matrix
输入:只包含0,1的矩阵 输出:元素1到达最近0的距离 算法思想:广度优先搜索. 元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出 ...
随机推荐
- [LeetCode] Scramble String -- 三维动态规划的范例
(Version 0.0) 作为一个小弱,这个题目是我第一次碰到三维的动态规划.在自己做的时候意识到了所谓的scramble实际上有两种可能的类型,一类是在较低层的节点进行的两个子节点的对调,这样的情 ...
- [Codeforces 787D] Legacy
[题目链接] https://codeforces.com/contest/787/problem/D [算法] 线段树优化建边 , 然后用Dijkstra算法求单源最短路 时间复杂度 : O((N ...
- C++之构造函数、参数列表、析构函数
参考自:https://blog.csdn.net/sunSHINEEzy/article/details/78122485 构造函数之默认构造函数(调用的构造函数不用传递参数) 两种实例化方式都是默 ...
- 构建一个简单的Angular工程
1.创建一个空的工程,之后用webstorm打开,添加一个bower.json文件: { "name": "AngularTpl", "depende ...
- 【转】Cache Buffer Chain 第二篇
文章转自:http://m.bianceng.cn/database/Oracle/201407/42884.htm 测试环境:版本11gR2 SQL> select * from v$vers ...
- Interval query
题意: 给出数轴上的N个区间,M个询问"QUERY(a, b)", 意为[a, b]之间不相交的集合的最大数量是多少. 解法: 考虑 $O(n)$ 的贪心做法,预处理出对于每一个位 ...
- python如何实现相对导入
如果python中导入的package或module不在环境变量PATH中,可以使用sys.path将要导入的package或module加入到PATH环境变量中,之后便能使用相对导入方法. 拿hom ...
- Cannot set headers after they are sent to the client
D:\le\node_modules\mysql\lib\protocol\Parser.js: throw err; // Rethrow non-MySQL errors ^ Error [ERR ...
- 719. Find K-th Smallest Pair Distance
Given an integer array, return the k-th smallest distance among all the pairs. The distance of a pai ...
- 玲珑OJ1088【蜜汁尺取】
前言(膜法): 早上10点多开始膜的,然后到中午交了一发,感觉膜法不对啊!然后就兴起小窗了一发管理员,然后管理员给我发了in,out数据...可是太大并没有什么可取性... 还是自己试,然后发现自己搞 ...