-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_matrix.cpp
More file actions
117 lines (109 loc) · 3.29 KB
/
update_matrix.cpp
File metadata and controls
117 lines (109 loc) · 3.29 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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* =====================================================================================
*
* Filename: update_matrix.cpp
*
* Description: 542. 01 Matrix. https://leetcode.com/problems/01-matrix/
*
* Version: 1.0
* Created: 04/03/23 16:23:16
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <algorithm>
#include <array>
#include <climits>
#include <queue>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace std;
// Breadth first search
class Solution1 {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
const int rows = mat.size();
const int cols = mat[0].size();
vector<vector<int>> dis(rows, vector<int>(cols, INT_MAX));
queue<pair<int, int>> visited;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (mat[i][j] == 0) {
dis[i][j] = 0;
visited.push({i, j});
}
}
}
array<pair<int, int>, 4> dir = {make_pair(-1, 0), {1, 0}, {0, -1}, {0, 1}};
while (!visited.empty()) {
auto p = visited.front();
visited.pop();
for (auto& d : dir) {
const int i = p.first + d.first;
const int j = p.second + d.second;
if (i < 0 || i >= rows || j < 0 || j >= cols) {
continue;
}
if (dis[i][j] > dis[p.first][p.second] + 1) {
dis[i][j] = dis[p.first][p.second] + 1;
visited.push({i, j});
}
}
}
return dis;
}
};
// Dynamic programming
class Solution2 {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
const int rows = mat.size();
const int cols = mat[0].size();
vector<vector<int>> dis(rows, vector<int>(cols, INT_MAX - 10000));
// First pass: check from left to right, top to bottom
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (mat[i][j] == 0) {
dis[i][j] = 0;
} else {
if (i > 0) {
dis[i][j] = std::min(dis[i][j], dis[i - 1][j] + 1);
}
if (j > 0) {
dis[i][j] = std::min(dis[i][j], dis[i][j - 1] + 1);
}
}
}
}
// Second pass: check from right to left, bottom to top
for (int i = rows - 1; i >= 0; i--) {
for (int j = cols - 1; j >= 0; j--) {
if (i < rows - 1) {
dis[i][j] = std::min(dis[i][j], dis[i + 1][j] + 1);
}
if (j < cols - 1) {
dis[i][j] = std::min(dis[i][j], dis[i][j + 1] + 1);
}
}
}
return dis;
}
};
TEST(Solution, updateMatrix) {
vector<pair<vector<vector<int>>, vector<vector<int>>>> cases = {
std::make_pair(vector<vector<int>>{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}},
vector<vector<int>>{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}),
std::make_pair(vector<vector<int>>{{0, 0, 0}, {0, 1, 0}, {1, 1, 1}},
vector<vector<int>>{{0, 0, 0}, {0, 1, 0}, {1, 2, 1}}),
};
for (auto& c : cases) {
EXPECT_THAT(Solution1().updateMatrix(c.first), testing::ElementsAreArray(c.second));
EXPECT_THAT(Solution2().updateMatrix(c.first), testing::ElementsAreArray(c.second));
}
}