-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclosedIsland.cpp
33 lines (31 loc) · 887 Bytes
/
closedIsland.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
class Solution {
public:
int closedIsland(vector<vector<int>>& grid) {
if(grid.empty() || grid[0].empty()) return 0;
int h = grid.size();
int w = grid[0].size();
int count = 0;
int i,j;
for(i=0; i < h; ++i) {
for (j=0; j < w; ++j){
if(grid[i][j]==0) {
count+=dfs(grid, i , j)?1:0;
}
}
}
return count;
}
private:
bool dfs(vector<vector<int>>&grid, int i, int j) {
if (i < 0 || j < 0|| i >= grid.size() || j >= grid[0].size()) {
return false;
}
if (grid[i][j]==1) return true;
grid[i][j] = 1;
bool d1 = dfs(grid, i+1, j);
bool d2 = dfs(grid, i-1, j);
bool d3 = dfs(grid, i, j+1);
bool d4 = dfs(grid, i, j-1);
return d1 && d2 && d3 && d4;
}
};