-
Notifications
You must be signed in to change notification settings - Fork 8
/
number-of-islands.cpp
62 lines (61 loc) · 1.26 KB
/
number-of-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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
int findIslands(int A[MAX][MAX],int N,int M);
int main() {
// your code goes here
int T;
cin>>T;
int A[MAX][MAX];
while(T--)
{
int N,M;
cin>>N>>M;
memset(A,0,sizeof A);
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
cin>>A[i][j];
cout<<findIslands(A,N,M)<<endl;
}
return 0;
}
void dfs(int i,int j,int A[MAX][MAX],int N,int M,bool visited[MAX][MAX])
{
if(i>=0 && i<N && j>=0 && j<M)
{
if(A[i][j]==1 && !visited[i][j])
{
visited[i][j]=true;
dfs(i-1,j-1,A,N,M,visited);
dfs(i-1,j,A,N,M,visited);
dfs(i-1,j+1,A,N,M,visited);
dfs(i,j-1,A,N,M,visited);
dfs(i,j+1,A,N,M,visited);
dfs(i+1,j-1,A,N,M,visited);
dfs(i+1,j,A,N,M,visited);
dfs(i+1,j+1,A,N,M,visited);
}
}
}
int findIslands(int A[MAX][MAX], int N, int M)
{
bool visited[MAX][MAX];
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
visited[i][j]=false;
}
int components=0;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
if(A[i][j]==1 && visited[i][j]==false)
{
dfs(i,j,A,N,M,visited);
components++;
}
}
}
return components;
}