-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_LCR_107_updateMatrix.cc
49 lines (47 loc) · 1.15 KB
/
Problem_LCR_107_updateMatrix.cc
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
#include <queue>
#include <vector>
using namespace std;
// @sa https://leetcode.cn/problems/01-matrix/
// @sa Problem_542_updateMatrix.cc
class Solution
{
public:
// bfs
vector<vector<int>> updateMatrix(vector<vector<int>>& mat)
{
int n = mat.size();
int m = mat[0].size();
vector<vector<int>> distance(n, vector<int>(m));
vector<vector<bool>> seen(n, vector<bool>(m));
queue<std::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});
seen[i][j] = true;
}
}
}
static constexpr int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while (!q.empty())
{
auto [x, y] = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
int nextx = x + directions[i][0];
int nexty = y + directions[i][1];
if (nextx >= 0 && nextx < n && nexty >= 0 && nexty < m && !seen[nextx][nexty])
{
distance[nextx][nexty] = distance[x][y] + 1;
q.push({nextx, nexty});
seen[nextx][nexty] = true;
}
}
}
return distance;
}
};