-
Notifications
You must be signed in to change notification settings - Fork 11
/
Day-084.cpp
33 lines (32 loc) · 870 Bytes
/
Day-084.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
class Solution {
int row,col;
vector<vector<bool>>visited;
void dfs(int i,int j, vector<vector<char>>&grid){
if(i<0 || i>=row || j<0 || j>=col || visited[i][j] || grid[i][j]!='1'){
return;
}
visited[i][j] = true;
dfs(i+1,j,grid);
dfs(i-1,j,grid);
dfs(i,j+1,grid);
dfs(i,j-1,grid);
}
public:
int numIslands(vector<vector<char>>& grid) {
row = (int)grid.size();
if(!row)
return 0;
col = (int)grid[0].size();
visited.assign(row,vector<bool>(col,false));
int counter{};
for(int i=0;i<row;++i){
for(int j=0;j<col;++j){
if(grid[i][j]=='1' &&!visited[i][j]){
dfs(i,j,grid);
++counter;
}
}
}
return counter;
}
};