-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumberofEnclaves.cpp
More file actions
39 lines (37 loc) · 1.06 KB
/
numberofEnclaves.cpp
File metadata and controls
39 lines (37 loc) · 1.06 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
// Source: https://leetcode.com/problems/number-of-enclaves/
// Author: Miao Zhang
// Date: 2021-04-02
class Solution {
public:
int numEnclaves(vector<vector<int>>& grid) {
int res = 0;
for (int i = 0; i < grid.size(); i++) {
for (int j = 0; j < grid[0].size(); j++) {
int cnt = 0;
if (!dfs(grid, i, j, cnt)) {
res += cnt;
}
}
}
return res;
}
private:
vector<vector<int>> dirs{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
// true: arrive boundary
// false: can not
bool dfs(vector<vector<int>>& grid, int x, int y, int& cnt) {
// arrive boundary
if (x < 0 || x >= grid.size() || y < 0 || y >= grid[0].size()) {
return true;
}
// Beyond the scope of Statistics
if (grid[x][y] == 0) return false;
cnt++;
grid[x][y] = 0;
bool valid = false;
for (auto d: dirs) {
valid |= dfs(grid, x + d[0], y + d[1], cnt);
}
return valid;
}
};