-
Notifications
You must be signed in to change notification settings - Fork 0
/
803. Bricks Falling When Hit.cpp
52 lines (50 loc) · 1.71 KB
/
803. Bricks Falling When Hit.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
class Solution {
public:
vector<int> hitBricks(vector<vector<int>>& grid, vector<vector<int>>& hits) {
vector<int>res;
int m = grid.size(), n = grid[0].size();
for (auto& v: hits) {
int r = v[0], c = v[1], count = 0;
grid[r][c] = 0;
if(!reachTop(grid, r - 1, c, m, n)) {
erase(grid, r - 1, c, m, n, count);
}
if(!reachTop(grid, r + 1, c, m, n)) {
erase(grid, r + 1, c, m, n, count);
}
if(!reachTop(grid, r, c - 1, m, n)) {
erase(grid, r, c - 1, m, n, count);
}
if(!reachTop(grid, r, c + 1, m, n)) {
erase(grid, r, c + 1, m, n, count);
}
res.push_back(count);
}
return res;
}
bool reachTop(vector<vector<int>>& grid, int r, int c, int m, int n) {
if (r < 0 || c < 0 || r == m || c == n || grid[r][c] == 0) {
return false;
}
if (r == 0) {
return true;
}
int tmp = grid[r][c];
grid[r][c] = 0;
bool res = reachTop(grid, r + 1, c, m, n) || reachTop(grid, r, c + 1, m, n)
|| reachTop(grid, r - 1, c, m, n) || reachTop(grid, r, c - 1, m, n);
grid[r][c] = tmp;
return res;
}
void erase(vector<vector<int>>& grid, int r, int c, int m, int n, int& count) {
if (r < 0 || c < 0 || r == m || c == n || grid[r][c] == 0) {
return;
}
++count;
grid[r][c] = 0;
erase(grid, r + 1, c, m, n, count);
erase(grid, r - 1, c, m, n, count);
erase(grid, r, c + 1, m, n, count);
erase(grid, r, c - 1, m, n, count);
}
};