-
Notifications
You must be signed in to change notification settings - Fork 522
/
Copy path29. 01 Matrix.cpp
73 lines (63 loc) · 1.41 KB
/
29. 01 Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
01 Matrix
=========
Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.
Example 1:
Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
Output: [[0,0,0],[0,1,0],[0,0,0]]
Example 2:
Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
Output: [[0,0,0],[0,1,0],[1,2,1]]
Constraints:
m == mat.length
n == mat[i].length
1 <= m, n <= 104
1 <= m * n <= 104
mat[i][j] is either 0 or 1.
There is at least one 0 in mat.
*/
class Solution
{
public:
int dirs[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<vector<int>> updateMatrix(vector<vector<int>> &mat)
{
auto ans = mat;
int n = mat.size(), m = mat[0].size();
queue<pair<int, int>> q;
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (mat[i][j] == 0)
{
q.push({i, j});
mat[i][j] = -1;
}
}
}
int steps = 0;
while (q.size())
{
steps++;
for (int i = q.size(); i > 0; --i)
{
auto curr = q.front();
q.pop();
int x = curr.first, y = curr.second;
for (auto &dir : dirs)
{
int nx = x + dir[0], ny = y + dir[1];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && mat[nx][ny] != -1)
{
mat[nx][ny] = -1;
ans[nx][ny] = steps;
q.push({nx, ny});
}
}
}
}
return ans;
}
};