-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question82.cpp
78 lines (73 loc) · 1.9 KB
/
Question82.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
82 -> Find the number of islands using DFS
Soution :
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
// Function to find the number of islands.
bool check(vector<vector<char>>& grid,int i,int j,int n,int m){
if(i>=0 && j>=0 && i<n && j<m && grid[i][j]=='1')return true;
return false;
}
void dfs(vector<vector<char>>& grid,int i,int j,int n,int m){
grid[i][j]='0';
if(check(grid,i+1,j,n,m)){
dfs(grid,i+1,j,n,m);
}
if(check(grid,i-1,j,n,m)){
dfs(grid,i-1,j,n,m);
}
if(check(grid,i,j+1,n,m)){
dfs(grid,i,j+1,n,m);
}
if(check(grid,i,j-1,n,m)){
dfs(grid,i,j-1,n,m);
}
if(check(grid,i-1,j-1,n,m)){
dfs(grid,i-1,j-1,n,m);
}
if(check(grid,i+1,j+1,n,m)){
dfs(grid,i+1,j+1,n,m);
}
if(check(grid,i+1,j-1,n,m)){
dfs(grid,i+1,j-1,n,m);
}
if(check(grid,i-1,j+1,n,m)){
dfs(grid,i-1,j+1,n,m);
}
}
int numIslands(vector<vector<char>>& 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]=='1'){
ans++;
dfs(grid,i,j,grid.size(),grid[0].size());
}
}
}
return ans;
}
};
//{ Driver Code Starts.
int main() {
int tc;
cin >> tc;
while (tc--) {
int n, m;
cin >> n >> m;
vector<vector<char>> grid(n, vector<char>(m, '#'));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
}
}
Solution obj;
int ans = obj.numIslands(grid);
cout << ans << '\n';
}
return 0;
}
// } Driver Code Ends