-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4b_DFS.cpp
67 lines (62 loc) · 1.04 KB
/
4b_DFS.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
#include <iostream>
using namespace std;
int visited[10][10];
int m,n;
int a[10][10];
int row[]={-1, -1, -1, 0, 0, 1, 1, 1 };
int col[]={-1, 0, 1, -1, 1, -1, 0, 1 };
bool isSafe(int i,int j)
{
if(i>=0 && i<n && j>=0 && j<n)return true;
return false;
}
void dfs(int i,int j)
{
visited[i][j]=1;
for(int k=0;k<8;k++)
{
if(isSafe(i+row[k],j+col[k]) && a[i+row[k]][j+col[k]] && visited[i+row[k]][j+col[k]]==0)
dfs(i+row[k],j+col[k]);
}
}
int main()
{
int count=0;
cout<<"Enter the size of the matrix\n";
cin>>n;
cin>>m;
cout<<"Enter the adjacency matrix\n";
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
visited[i][j]=0;
}
}
cout<<"No of clusters are:\n";
clock_t start,end,t;
start=clock();
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(visited[i][j]==0 && a[i][j]==1)
{
dfs(i,j);
count++;
}
}
}
cout<<count<<endl;
end=clock();
t = end-start;
cout<<"\nIt took me: "<<(float)t/CLOCKS_PER_SEC<<" sec\n\n";
return 0;
}