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为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出 ...
随机推荐
- shell之cut和tr 的命令的使用
[root@data-1-3 ~]# head /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin ...
- C++之引用&的详解
C++中的引用: 引用引入了对象的一个同义词.定义引用的表示方法与定义指针相似,只是用&代替了*.引用(reference)是c++对c语言的重要扩充.引用就是某一变量(目标)的一个别名,对引 ...
- tabbar 设置样式
app.json配置文件中,使用时需要把注释删除,配置文件不支持备注 tabbar:{ color:'#fff',//字体颜色 需要时HexColor,设置成red 无法识别,下方颜色设置同理 se ...
- Win7点击文件夹右键可打开cmd控制台,并获取当前目录
当我们用cmd时,有时要切换到某个文件夹的目录,可以在当前目录下,按住shift单击右键打开控制台,也可以在鼠标右键中添加cmd启动命令: 1.在开始搜索框输入regedit,打开注册表: 2.打开 ...
- iOS中判断基础字符(大小写、数字等的判断)
函数:isdigit 用法:#include 功能:判断字符c是否为数字 说明:当c为数字0-9时,返回非零值,否则返回零. 函数:islower 用法:#include 功能:判断字符c是否为小写英 ...
- dubbo 使用 filter 报错解决
dubbo可以用filter实现类似tomcat filter过滤器. 实现1.接口请求时间监控. 2.打印输入输出日志(输出日志有应用自己决定) 配置时出现报错. No such extension ...
- Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力
Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between eas ...
- CodeForces 1091G. New Year and the Factorisation Collaboration
题目简述:若你获得“超能力”:固定$n$,对任意$a$,可以快速求出$x \in [0, n)$(若存在),使得$x^2 \equiv a \pmod n$,若存在多个$x$满足条件,则返回其中一个( ...
- sql语句之查询操作
语法顺序: select distinct 字段1,字段2,字段3 from 库.表 where 条件 group by 分组条件 having 过滤 # 执行顺序的话,到这步会返回运行select语 ...
- java 关于getProperty()方法中反斜杠问题
问: 在配置文件a.properties中有一行path=C:\test在java中getProperty("path")后,java把\t认为是一个字符TAB.怎样才能取到正确的 ...