-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathNumber of Closed Islands.cpp
46 lines (30 loc) · 1.15 KB
/
Number of Closed Islands.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
/*
Solution by Rahul Surana
***********************************************************
Given a 2D grid consists of 0s (land) and 1s (water).
An island is a maximal 4-directionally connected group of 0s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s.
Return the number of closed islands.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
bool df(int i, int j,vector<vector<int>>& grid){
if(i<0 || j <0 || i > grid.size()-1 || j > grid[0].size()-1) return false;
if(grid[i][j]) return true;
grid[i][j] = 1;
bool r = df(i+1,j,grid), l = df(i-1,j,grid), d = df(i,j+1,grid), u = df(i,j-1,grid);
return ( r && l && d && u);
}
int closedIsland(vector<vector<int>>& grid) {
int ans = 0;
for(int i = 0; i < grid.size(); i++){
for(int j = 0; j < grid[0].size(); j++){
if(!grid[i][j]){
ans+= df(i,j,grid);
}
}
}
return ans;
}
};