-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.cpp
35 lines (35 loc) · 1.02 KB
/
Solution.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
class Solution {
public:
int numEnclaves(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
const int dirs[5] = {-1, 0, 1, 0, -1};
function<void(int, int)> dfs = [&](int i, int j) {
grid[i][j] = 0;
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) {
dfs(x, y);
}
}
};
for (int j = 0; j < n; ++j) {
for (int i : {0, m - 1}) {
if (grid[i][j] == 1) {
dfs(i, j);
}
}
}
for (int i = 0; i < m; ++i) {
for (int j : {0, n - 1}) {
if (grid[i][j] == 1) {
dfs(i, j);
}
}
}
int ans = 0;
for (const auto& row : grid) {
ans += accumulate(row.begin(), row.end(), 0);
}
return ans;
}
};