leetcode542 01 Matrix

思路:

多个起点的bfs。

实现:

 1 class Solution
 2 {
 3 public:
 4     vector<vector<int>> updateMatrix(vector<vector<int>>& matrix)
 5     {
 6         int n = matrix.size(), m = matrix[0].size();
 7         vector<vector<int>> vis(n, vector<int>(m, 0));
 8         vector<vector<int>> d(n, vector<int>(m, 0x3f3f3f3f));
 9         queue<pair<int, int>> q;
10         int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
11         for (int i = 0; i < n; i++)
12         {
13             for (int j = 0; j < m; j++)
14             {
15                 if (matrix[i][j] == 0)
16                 {
17                     q.push(pair<int, int>(i, j));
18                     vis[i][j] = 1;
19                     d[i][j] = 0;
20                 }
21             }
22         }
23         while (!q.empty())
24         {
25             pair<int, int> tmp = q.front();
26             q.pop();
27             int x = tmp.first, y = tmp.second;
28             for (int i = 0; i < 4; i++)
29             {
30                 int nx = x + dx[i], ny = y + dy[i];
31                 if (nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny])
32                 {
33                     vis[nx][ny] = 1;
34                     d[nx][ny] = d[x][y] + 1;
35                     q.push(pair<int, int>(nx, ny));
36                 }
37             }
38         }
39         return d;
40     }
41 };

原文地址:https://www.cnblogs.com/wangyiming/p/9819665.html

时间: 2024-11-13 01:33:38

leetcode542 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

输入:只包含0,1的矩阵 输出:元素1到达最近0的距离 算法思想:广度优先搜索. 元素为0为可达区域,元素为1为不可达区域,我们的目标是为了从可达区域不断地扩展至不可达区域,在扩展的过程中,也就计算出了这些不可达区域到达最近可达区域的距离. 每个可达元素都记录了到当前位置的距离,因此在后续的遍历中,如果是经由当前节点到达的下一节点,这个距离会被累加. 1 #include <iostream> 2 #include <vector> 3 #include <queue>

[Leetcode] 01 Matrix

问题: https://leetcode.com/problems/01-matrix/#/description 基本思路:广度优先遍历,根据所有最短距离为N的格找到所有距离为N+1的格,直到所有的格都找到了最短距离. 具体步骤: 首先初始化矩阵,值为0的格其最短距离也是0,保持不变:值为1的格是后面需要计算最短距离的格,我们需要一个整数来标识它们,这里可以选择一个负数或者整数的最大值. 然后进行若干轮计算,第N轮会根据已经计算出的最短距离为N-1的所有格,来找出所有最短距离为N的格. 例如,

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

题意: 有一个n*n(n<=1000)的01矩阵 Q次询问(1000) 每次询问有几个大于等于k的全为一的子矩形 从右下角往右上角预处理每个点有一个r x vr代表右边有多少连续1x代表下面有多少连续1v代表以这个为左上角的矩阵最大是多少所以v[i][j]= min(r[i][j+1], x[i+1][j],v[i+1][j+1]) +1 r[i][j]=r[i][j+1]+1; x[i][j]=x[i+1][j]+1; 然后ans[v[i][j]]++ 预处理后缀和  O 1 输出 #incl

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 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] 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] Longest Line of Consecutive One in Matrix 矩阵中最长的连续1

Given a 01 matrix M, find the longest line of consecutive one in the matrix. The line could be horizontal, vertical, diagonal or anti-diagonal. Example: Input: [[0,1,1,0], [0,1,1,0], [0,0,0,1]] Output: 3 Hint: The number of elements in the given matr

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 输出