-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path01 Matrix.cpp
More file actions
38 lines (38 loc) · 1.09 KB
/
01 Matrix.cpp
File metadata and controls
38 lines (38 loc) · 1.09 KB
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
//https://leetcode.com/problems/01-matrix/
class Solution {
int n, m;
bool checkIfOut(int x, int y) {
return x < 0 || y < 0 || y >= m || x >= n;
}
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
queue<pair<int, int> > q;
n = matrix.size();
if (n > 0)
m = matrix[0].size();
vector<vector<int>> dp(n, vector<int>(m, INT_MAX / 10));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix[i][j] == 0) {
q.push({ i, j });
dp[i][j] = 0;
}
}
}
while (!q.empty()) {
auto point = q.front();
q.pop();
int x = point.first;
int y = point.second;
if (!checkIfOut(x - 1, y) && dp[x][y] + 1 < dp[x - 1][y])
dp[x - 1][y] = dp[x][y] + 1, q.push({ x - 1, y });
if (!checkIfOut(x, y - 1) && dp[x][y] + 1 < dp[x][y - 1])
dp[x][y - 1] = dp[x][y] + 1, q.push({ x, y - 1 });
if (!checkIfOut(x + 1, y) && dp[x][y] + 1 < dp[x + 1][y])
dp[x + 1][y] = dp[x][y] + 1, q.push({ x + 1, y });
if (!checkIfOut(x, y + 1) && dp[x][y] + 1 < dp[x][y + 1])
dp[x][y + 1] = dp[x][y] + 1, q.push({ x, y + 1 });
}
return dp;
}
};