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为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出 ...
随机推荐
- webpack 小demo
1 安装node.js 2 安装cnpm 3 安装webpack cnpm install --save-dev webpack 对于大多数项目,我们建议本地安装.这可以使我们在引入破坏式变更的依赖时 ...
- 网卡流量检测.py
network_speed网卡流量检测 #!/usr/bin/env python #coding:utf-8 import sys import os import atexit import ti ...
- SpringMVC注解说明
@controller 通过@controller标注即可将class定义为一个controller类. @RequestMapping value 表示需要匹配的url的格式. method 表示所 ...
- Ubuntu install JDK
1.#下载JDK,记住保存的目录 2. sudo mkdir /usr/java 3. sudo tar zxvf jdk-7u75-linux-x64.tar.gz -C /usr/java 4. ...
- A-Z,a-z,0-9的unicode编码表
1.转自:https://blog.csdn.net/fedawn/article/details/7307993 A-Z 的 Unicode 字符编码表 十进制 十六进制 1.“A”的 U ...
- ** Error in `./g2o_viewer': realloc(): invalid pointer:
问题: defe@defe-Precision-Tower-3620:~/project/Demo/UseG2OforPoseGraph/useg2oforposegraph$ ./g2o_viewe ...
- 5、overflow、hover
一.overflow 1.属性介绍 说明: 这个属性定义溢出元素内容区的内容会如何处理.如果值为 scroll,不论是否需要,用户代理都会提供一种滚动机制.因此,有可能即使元素框中可以放下所有内容也会 ...
- java的环境变量classpath中加点号 ‘.’ 的作用
java的环境变量classpath中加点号 ‘.’ 的作用 “.”表示当前目录,就是编译或者执行程序时,你的.class文件所在的目录: 当找.class文件时,先去“.”路径下找,找不到的话,在去 ...
- springboot2 -广播式WebSocket
1.WebSocket,STOMP,SockJS含义 WebSocket:WebSocket是HTML5开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. SockJS:SockJS 是 We ...
- C#高级参数out,ref,params
在C#中通过使用方法来获取返回值时,通常只能得到一个返回值.因此,当一个方法需要返回多个值的时候,就需要用到ref和out,那么这两个方法区别在哪儿呢? out 当需要返回一系列返回值时可用out高级 ...