01 Matrix 题解

原创文章,拒绝转载

题目来源:https://leetcode.com/problems/01-matrix/description/


Description

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:

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

Solution

class Solution {
private:
struct vertex
{
int r, c;
vertex(int _r, int _c) : r(_r), c(_c) {}
};
int row, col;
vector<vector<int> > res;
public:
bool isValid(int r, int c) {
return r >= 0 && r < row && c >= 0 && c < col;
} void insertQ(queue<vertex>& q, int r, int c, int val) {
if (!isValid(r, c))
return;
if (res[r][c] == -1) {
res[r][c] = val + 1;
q.push(vertex(r, c));
} else if (res[r][c] > val + 1) {
res[r][c] = val + 1;
}
} vector<vector<int> > updateMatrix(vector<vector<int> >& A) {
this -> row = A.size();
this -> col = A[0].size();
vector<int> rowvec(col, -1);
vector<vector<int> > resRef(row, rowvec);
this -> res = resRef;
int i, j;
queue<vertex> q;
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (A[i][j] == 0) {
res[i][j] = 0;
q.push(vertex(i, j));
}
}
} while (!q.empty()) {
vertex v = q.front();
q.pop();
int val = res[v.r][v.c];
insertQ(q, v.r + 1, v.c, val);
insertQ(q, v.r - 1, v.c, val);
insertQ(q, v.r, v.c + 1, val);
insertQ(q, v.r, v.c - 1, val);
}
return res;
}
};

解题描述

这道题是典型的搜索类问题,我采用了BFS,从为0的顶点开始,逐步更新临近圈层的步数直到矩阵中所有的点的步数都计算出来。

[Leetcode Week10]01 Matrix的更多相关文章

  1. 【LeetCode】01 Matrix 解题报告

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

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

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

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

  4. LeetCode 542. 01 Matrix

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

  5. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

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

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

  7. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

    Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...

  8. hdu 01 Matrix

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

  9. [LeetCode] 01 Matrix 零一矩阵

    Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell. The distance b ...

随机推荐

  1. jmeter接口测试--响应结果Unicode转码成中文

    jmeter接口测试-响应结果Unicode转码成中文 一般情况下,接口返回数据都会经过加密,所以有时相应结果会显示为Unicode,因此,需添加BeanShell PostProcessor,加入代 ...

  2. react实现网站换肤功能

    一.目标   提供几种主题色给用户选择,然后根据用户的选择改变应用的主题色: 二.实现原理   1.准备不同主题色的样式文件:   2.将用户的选择记录在本地缓存中:   3.每次进入应用时,读取缓存 ...

  3. ThinkPHP5作业管理系统中处理学生未交作业与已交作业信息

    在作业管理系统中,学生登陆到个人中心后可以通过左侧的菜单查看自己已经提交的作业和未提交作业.那么在系统中如何实现这些数据的查询的呢?首先我们需要弄清楚学生(Student).班级(class).作业提 ...

  4. 使用vue和web3创建你的第一个以太坊APP

    欢迎回到这个很牛的教程系列的第2部分,在教程中我们亲手构建我们的第一个分布式应用程序. 在第二部分中,我们将介绍VueJS和Vuex的核心概念,并引入web3js以与metamask进行交互. 如果你 ...

  5. 有向图的强连通分量——kosaraju算法

    一.前人种树 博客:Kosaraju算法解析: 求解图的强连通分量

  6. C++ 中神奇的头文件,懒人专用

    今天在做题的时候,偶然发现了一种神奇头文件.他的使用方法以及内容如下: #include <bits/stdc++.h> // C++ includes used for precompi ...

  7. Alpha 冲刺5/10

    队名:我头发呢队 组长博客 杰(组长) 过去两天完成了哪些任务 继续翻阅Material Design2文档 翻阅网易云的web端网页 接下来的计划 音源爬取 还剩下哪些任务 app开发 燃尽图 有哪 ...

  8. springmvc项目搭建五-postgresql+easyui的数据显示

    上一篇虽然完成了页面的显示,但是是假数据,本篇添加了postgresql的数据库,将登陆的校验和数据的显示都通过数据库来完成. 我是在本地搭建了一个postgre的数据库,就先新建两张表吧,一个用于用 ...

  9. 第四次JAVA作业

    public class TvbDog { public static void main(String[] args) {  Dog per=new Dog("陈狗"," ...

  10. 【bzoj1257】[CQOI2007]余数之和sum 数论

    题目描述 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如j(5, 3)=3 m ...