思路:

多个起点的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的更多相关文章

  1. [Leetcode Week10]01 Matrix

    01 Matrix 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/01-matrix/description/ Description Given a ...

  2. 计算机学院大学生程序设计竞赛(2015’12)01 Matrix

    01 Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  3. [leetcode] 542. 01 Matrix (Medium)

    给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改 ...

  4. 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的值. 最 ...

  5. hdu 01 Matrix

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission ...

  6. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  7. [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 ...

  8. [LeetCode] 01 Matrix 题解

    题意 # 思路 我一开始的时候想的是嘴 # 实现 ```cpp // // include "../PreLoad.h" class Solution { public: /** ...

  9. LeetCode 542. 01 Matrix

    输入:只包含0,1的矩阵 输出:元素1到达最近0的距离 算法思想:广度优先搜索. 元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出 ...

随机推荐

  1. js 弹出QQ对话框

    首先打开下面链接,开通QQ推广. http://shang.qq.com/v3/index.html 然后在页面代码中写入 <a target="_blank" href=& ...

  2. js 判断滚动条是不是在浏览器底部

    http://jingyan.baidu.com/album/86f4a73e91da7837d65269d5.html?picindex=2

  3. Java原子属性更新器AtomicReferenceFieldUpdater的使用

    AtomicReferenceFieldUpdater是基于反射的工具类,用来将指定类型的指定的volatile引用字段进行原子更新,对应的原子引用字段不能是private的.通常一个类volatil ...

  4. nohup command > myout.file 2>&1 &

    nohup command > myout.file 2>&1 &

  5. vector 大小

    vector定义以后就最好确定大小resize(),否则在vector析构时可能出现 "double free or corruption"这样的错误

  6. app自动化测试工具robotium

    robotium基于instramentation框架,可对app白盒黑盒测试,缺点是测试进程和被测进程需要在一个进程中,不能跨应用 白盒测试时,需要app源代码,在eclipse里新建android ...

  7. Flex AIR操作文件系统

    1.各种文件操作http://blog.csdn.net/zdingxin/article/details/6635376 2.file.browseForDirectory("请选择一个目 ...

  8. ASP.NET学习笔记(一)相关概念

    ASP.NET 是一个开发框架,用于通过 HTML.CSS.JavaScript 以及服务器脚本来构建网页和网站. ASP.NET 支持三种开发模式: Web Pages MVC Web Forms ...

  9. LeetCode: 521 Longest Uncommon Subsequence I(easy)

    题目: anysubsequence of the other strings. A subsequence is a sequence that can be derived from one se ...

  10. Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.

    一.前言 Job for mysqld.service failed because the control process exited with error code. See "sys ...