LeetCode 542. 01 Matrix

输入:只包含0,1的矩阵

输出:元素1到达最近0的距离

算法思想:广度优先搜索。

元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出了这些不可达区域到达最近可达区域的距离。

每个可达元素都记录了到当前位置的距离,因此在后续的遍历中,如果是经由当前节点到达的下一节点,这个距离会被累加。

 1 #include <iostream>
 2 #include <vector>
 3 #include <queue>
 4 using namespace std;
 5
 6 class Solution {
 7 public:
 8     vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
 9         int nRow = matrix.size();
10         int nCol = matrix[0].size();
11         vector<vector<int>> answer(nRow, vector<int>(nCol));
12         queue<pair<int, int>> reachable;
13         for (int i = 0; i < nRow; i++)
14         {
15             for (int j = 0; j < nCol; j++)
16             {
17                 if (matrix[i][j] == 0) // reachable
18                 {
19                     reachable.push(make_pair(i, j));
20                     answer[i][j] = 0;
21                 }
22                 else
23                     answer[i][j] = INT_MAX;
24             }
25         }
26
27         vector<pair<int, int>> dir = vector<pair<int, int>>({make_pair(-1,0),make_pair(1,0),make_pair(0,-1),make_pair(0,1)});
28
29         while (!reachable.empty())
30         {
31             pair<int, int> cur = reachable.front();
32             for (int i = 0; i < 4; i++)
33             {
34                 int x = dir[i].first;
35                 int y = dir[i].second;
36                 int cx = cur.first;
37                 int cy = cur.second;
38                 if (cx + x < 0 || cx + x > nRow - 1 || cy + y < 0 || cy + y > nCol - 1) // boundary test
39                     continue;
40                 if (matrix[cx+x][cy+y] == 1) // not visited
41                 {
42                     matrix[cx + x][cy + y] = 0; // label visited
43                     answer[cx + x][cy + y] = answer[cx][cy] + 1;
44                     reachable.push(make_pair(cx + x, cy + y));
45                 }
46             }
47             reachable.pop();
48         }
49         return answer;
50     }
51 };
时间: 2024-08-09 23:43:11

LeetCode 542. 01 Matrix的相关文章

[leetcode] 542. 01 Matrix (Medium)

给予一个矩阵,矩阵有1有0,计算每一个1到0需要走几步,只能走上下左右. 解法一: 利用dp,从左上角遍历一遍,再从右下角遍历一遍,dp存储当前位置到0的最短距离. 十分粗心的搞错了col和row,改了半天---- Runtime: 132 ms, faster than 98.88% of C++ online submissions for 01 Matrix. class Solution { public: vector<vector<int>> updateMatrix(

【LeetCode】542. 01 Matrix

Problem 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

Leetcode 542.01矩阵

01矩阵 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 0 0 0 示例 2: 输入: 0 0 0 0 1 0 1 1 1 输出: 0 0 0 0 1 0 1 2 1 注意: 给定矩阵的元素个数不超过 10000. 给定矩阵中至少有一个元素是 0. 矩阵中的元素只在四个方向上相邻: 上.下.左.右. 思路 先把所有0入队,把1置为MAX_VALUE,然

542. 01 Matrix

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

LeetCode——542. 01 矩阵

给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 0 0 1 0 0 0 0 示例 2: 输入: 0 0 0 0 1 0 1 1 1 输出: 0 0 0 0 1 0 1 2 1 注意: 给定矩阵的元素个数不超过 10000. 给定矩阵中至少有一个元素是 0. 矩阵中的元素只在四个方向上相邻: 上.下.左.右. 一 我们可以首先遍历一次矩阵,将值为0的点都存入queue,将

Leetcode 542:01 矩阵 01

Leetcode 542:01 矩阵 01 Matrix### 题目: 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离. 两个相邻元素间的距离为 1 . 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. 示例 1: 输入: 0 0 0 0 1 0 0 0 0 输出

LeetCode:Spiral Matrix II - 将元素1-n^2以螺旋序填充到矩阵

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix-ii/ 3.题目内容 英文:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. 中文:给出一个整数n,生成一个矩阵,使用数字1到n^2以螺旋顺序填充这个矩阵 例如:给出n=3,则生成如下矩阵:

LeetCode:Spiral Matrix - 螺旋输出矩阵中的元素

1.题目名称 Spiral Matrix(螺旋输出矩阵中的元素) 2.题目地址 https://leetcode.com/problems/spiral-matrix/ 3.题目内容 英文:Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order. 中文:给出一个m行n列的矩阵,以螺旋顺序返回矩阵中的所有元素. 例如:现有矩阵如下: [  [ 1,

【LeetCode】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ]此题与Spiral Matrix类似,可以用相同的方法解决,相比之下,此题比前一题简单 publ