-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1020.number-of-enclaves.cpp
More file actions
65 lines (60 loc) · 1.63 KB
/
1020.number-of-enclaves.cpp
File metadata and controls
65 lines (60 loc) · 1.63 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
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
/*
* @lc app=leetcode id=1020 lang=cpp
*
* [1020] Number of Enclaves
*/
// @lc code=start
auto speedup = [](){
cin.tie(nullptr);
cout.tie(nullptr);
ios::sync_with_stdio(false);
return 0;
}();
class Solution {
const int MOVES[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
int numEnclaves(vector<vector<int>>& grid) {
queue<pair<int, int>> q;
int rows = grid.size();
int cols = grid.front().size();
vector<vector<bool>> visit(rows, vector<bool>(cols));
int answer = 0;
for(int r = 0; r < rows; ++r) {
for(int c = 0; c < cols; ++c) {
if(!grid[r][c] || visit[r][c]) {
visit[r][c] = true;
continue;
}
q.push({r, c});
visit[r][c] = true;
bool border = false;
int count = 0;
while(q.size()) {
auto [row, col] = q.front();
q.pop();
count += 1;
if(!row || !col || row == rows - 1 || col == cols - 1) {
border = true;
}
for(int m = 0; m < 4; ++m) {
int newRow = row + MOVES[m][0];
int newCol = col + MOVES[m][1];
if(newRow < 0 || newCol < 0 || newRow >= rows || newCol >= cols) continue;
if(!grid[newRow][newCol] || visit[newRow][newCol]) continue;
visit[newRow][newCol] = true;
q.push({newRow, newCol});
}
}
if(!border) {
answer += count;
}
}
}
return answer;
}
};
// Accepted
// 58/58 cases passed (84 ms)
// Your runtime beats 39.64 % of cpp submissions
// Your memory usage beats 91.97 % of cpp submissions (27.2 MB)
// @lc code=end