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:

  1. 0 0 0
  2. 0 1 0
  3. 0 0 0

Output:

  1. 0 0 0
  2. 0 1 0
  3. 0 0 0

Example 2:

Input:

  1. 0 0 0
  2. 0 1 0
  3. 1 1 1

Output:

  1. 0 0 0
  2. 0 1 0
  3. 1 2 1

Solution

  1. class Solution {
  2. private:
  3. struct vertex
  4. {
  5. int r, c;
  6. vertex(int _r, int _c) : r(_r), c(_c) {}
  7. };
  8. int row, col;
  9. vector<vector<int> > res;
  10. public:
  11. bool isValid(int r, int c) {
  12. return r >= 0 && r < row && c >= 0 && c < col;
  13. }
  14. void insertQ(queue<vertex>& q, int r, int c, int val) {
  15. if (!isValid(r, c))
  16. return;
  17. if (res[r][c] == -1) {
  18. res[r][c] = val + 1;
  19. q.push(vertex(r, c));
  20. } else if (res[r][c] > val + 1) {
  21. res[r][c] = val + 1;
  22. }
  23. }
  24. vector<vector<int> > updateMatrix(vector<vector<int> >& A) {
  25. this -> row = A.size();
  26. this -> col = A[0].size();
  27. vector<int> rowvec(col, -1);
  28. vector<vector<int> > resRef(row, rowvec);
  29. this -> res = resRef;
  30. int i, j;
  31. queue<vertex> q;
  32. for (i = 0; i < row; i++) {
  33. for (j = 0; j < col; j++) {
  34. if (A[i][j] == 0) {
  35. res[i][j] = 0;
  36. q.push(vertex(i, j));
  37. }
  38. }
  39. }
  40. while (!q.empty()) {
  41. vertex v = q.front();
  42. q.pop();
  43. int val = res[v.r][v.c];
  44. insertQ(q, v.r + 1, v.c, val);
  45. insertQ(q, v.r - 1, v.c, val);
  46. insertQ(q, v.r, v.c + 1, val);
  47. insertQ(q, v.r, v.c - 1, val);
  48. }
  49. return res;
  50. }
  51. };

解题描述

这道题是典型的搜索类问题,我采用了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. C 计算金额

    #include <stdio.h> int main(int argc, char **argv) { \\定义两个变量 a金额 z跟票面 int a=0; int z=0;\\ 输入金 ...

  2. Java并发基础--Thread类

    一.Thread类的构成 Thread类实现Runnable接口.部分源码如下: 二.Thread类常用方法 1.currentThread()方法 currentThread()方法可以返回代码段正 ...

  3. 洛谷P2346四子连棋

    题目描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步. 黑白双方交替走棋,任意一方可 ...

  4. vue2.0介绍

    1.vue.js 是什么 vue(view)是一套构建用户界面的渐进式框架 Vue (pronounced /vjuː/, like view) is a progressive framework  ...

  5. Java中Model1和Model2

    Model1和Model2是java web的两种架构模式.这两种模式各有优缺点,都有各自适合使用的场景. Model1 首先,从分层的角度说,Model1模式可以看作是由两层组成:视图层和模型层. ...

  6. 在浏览器中从FTP下载文件

    public static class FTPHelper { /// <summary> /// 得到特定FTP目录的文件列表 /// </summary> /// < ...

  7. [C/C++] C++声明和定义的区别

    ·变量定义:用于为变量分配存储空间,还可为变量指定初始值.程序中,变量有且仅有一个定义. ·变量声明:用于向程序表明变量的类型和名字. ·定义也是声明:当定义变量时我们声明了它的类型和名字. ·ext ...

  8. JAVA多线程及补充

    进程 运行中的应用程序叫进程,每个进程运行时,都有自已的地址空间(内存空间)如IE浏览器在任务管器中可以看到操作系统都是支持多进程的 线程 线程是轻量级的进程,是进程中一个负责程序执行的控制单元线程没 ...

  9. Java23个设计模式的简明教程

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 毫无疑问,设计模式于 ...

  10. spring事务不回滚 自己抛的异常

    在service代码中   throw new Excepion("自定义异常“) 发现没有回滚, 然后百度了下, 改为抛出运行时异常  throw new RuntimeException ...