-
Notifications
You must be signed in to change notification settings - Fork 0
/
542-01-matrix.cpp
57 lines (50 loc) · 2.25 KB
/
542-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
// Title: 01 Matrix
// Description:
// 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.
// Link: https://leetcode.com/problems/01-matrix/
// Time complexity: O(m*n)
// Space complexity: O(m*n)
class Solution {
public:
std::vector<std::vector<int>> updateMatrix(std::vector<std::vector<int>> &mat) {
assert(mat.size() != 0 && mat[0].size() != 0);
const std::size_t M = mat.size();
const std::size_t N = mat[0].size();
std::vector<std::vector<int>> nearestDistance(M, std::vector<int>(N, INT_MAX));
std::queue<std::pair<std::size_t, std::size_t>> processingQueue; {
// for each 0 cell, set its nearestDistance as 0 and put it into the processingQueue
for (std::size_t i = 0; i != M; ++i) {
for (std::size_t j = 0; j != N; ++j) {
if (mat[i][j] == 0) {
nearestDistance[i][j] = 0;
processingQueue.push({ i, j });
}
}
}
}
// Breadth-First Search (with multiple sources)
while (!processingQueue.empty()) {
auto [i, j] = processingQueue.front(); processingQueue.pop();
// for each neighbor cell of which the nearestDistance is not yet determined,
// set its nearestDistance and put it into the processingQueue
if (i != 0 && nearestDistance[i-1][j] == INT_MAX) {
nearestDistance[i-1][j] = nearestDistance[i][j] + 1;
processingQueue.push({ i-1, j });
}
if (i != M-1 && nearestDistance[i+1][j] == INT_MAX) {
nearestDistance[i+1][j] = nearestDistance[i][j] + 1;
processingQueue.push({ i+1, j });
}
if (j != 0 && nearestDistance[i][j-1] == INT_MAX) {
nearestDistance[i][j-1] = nearestDistance[i][j] + 1;
processingQueue.push({ i, j-1 });
}
if (j != N-1 && nearestDistance[i][j+1] == INT_MAX) {
nearestDistance[i][j+1] = nearestDistance[i][j] + 1;
processingQueue.push({ i, j+1 });
}
}
return nearestDistance;
}
};