forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s1.cpp
26 lines (26 loc) · 819 Bytes
/
s1.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
// OJ: https://leetcode.com/problems/number-of-enclaves/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(1)
class Solution {
const int dirs[4][2] = {{0, 1}, {0, -1}, {-1, 0}, {1, 0}};
int M, N;
void dfs(vector<vector<int>> &A, int x, int y) {
if (x < 0 || x >= M || y < 0 || y >= N || !A[x][y]) return;
A[x][y] = 0;
for (auto &dir : dirs) dfs(A, x + dir[0], y + dir[1]);
}
public:
int numEnclaves(vector<vector<int>>& A) {
M = A.size(), N = A[0].size();
int ans = 0;
for (int i = 0; i < M; ++i) dfs(A, i, 0), dfs(A, i, N - 1);
for (int i = 0; i < N; ++i) dfs(A, 0, i), dfs(A, M - 1, i);
for (auto &row : A) {
for (int n : row) {
if (n) ++ans;
}
}
return ans;
}
};