forked from keineahnung2345/leetcode-cpp-practices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1020. Number of Enclaves.cpp
64 lines (52 loc) · 1.86 KB
/
1020. Number of Enclaves.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
53
54
55
56
57
58
59
60
61
62
63
64
//Runtime: 84 ms, faster than 14.76% of C++ online submissions for Number of Enclaves.
//Memory Usage: 16.5 MB, less than 62.50% of C++ online submissions for Number of Enclaves.
class Solution {
public:
int numEnclaves(vector<vector<int>>& A) {
int m = A.size(), n = A[0].size();
vector<vector<bool>> visited(m, vector<bool>(n, 0));
vector<vector<int>> dirs = {{0,1},{0,-1},{1,0},{-1,0}};
stack<vector<int>> stk;
for(int i = 0; i < m; i++){
if(A[i][0] == 1){
visited[i][0] = true;
stk.push({i, 0});
}
if(A[i][n-1] == 1){
visited[i][n-1] = true;
stk.push({i, n-1});
}
}
for(int j = 1; j < n-1; j++){
if(A[0][j] == 1){
visited[0][j] = true;
stk.push({0, j});
}
if(A[m-1][j] == 1){
visited[m-1][j] = true;
stk.push({m-1, j});
}
}
while(!stk.empty()){
vector<int> cur = stk.top(); stk.pop();
int curI = cur[0], curJ = cur[1];
for(vector<int>& dir : dirs){
int nextI = curI + dir[0];
int nextJ = curJ + dir[1];
if(nextI >= 0 && nextI < m && nextJ >= 0 && nextJ < n && !visited[nextI][nextJ] && A[nextI][nextJ] == 1){
visited[nextI][nextJ] = true;
stk.push({nextI, nextJ});
}
}
}
int ans = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(A[i][j] == 1 && !visited[i][j]){
ans++;
}
}
}
return ans;
}
};