Skip to content

Commit d071a92

Browse files
authored
Create number of island
1 parent 35abaa6 commit d071a92

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

number of island

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
#include<bits/stdc++.h>
3+
using namespace std;
4+
#define V 5
5+
6+
void fun(int mat[V][V] , bool visited[V][V] , int u, int v)
7+
{
8+
if((u<0)|| (v<0) || (u>=V) || (v>=V) || (mat[u][v] == 0) || (visited[u][v] == true))
9+
{
10+
return ;
11+
}
12+
13+
visited[u][v] = true;
14+
15+
fun(mat,visited,u+1,v);
16+
fun(mat,visited,u,v+1);
17+
fun(mat,visited,u+1,v+1);
18+
fun(mat,visited,u-1,v);
19+
fun(mat,visited,u,v-1);
20+
fun(mat,visited,u-1,v-1);
21+
fun(mat,visited,u+1,v-1);
22+
fun(mat,visited,u-1,v+1);
23+
}
24+
25+
void island(int mat[V][V])
26+
{
27+
bool visited[V][V];
28+
29+
for(int i=0;i<V;i++)
30+
{
31+
for(int j=0;j<V;j++)
32+
{
33+
visited[i][j] = false;
34+
}
35+
}
36+
int count =0;
37+
for(int i=0;i<V;i++)
38+
{
39+
for(int j=0;j<V;j++)
40+
{
41+
if((mat[i][j] == 1)&&(visited[i][j] == false))
42+
{
43+
//visited[i][j] = true;
44+
count++;
45+
fun(mat,visited,i,j);
46+
}
47+
}
48+
}
49+
50+
51+
cout<<count<<endl;
52+
}
53+
54+
int main()
55+
{
56+
57+
int mat[V][V] = { {1, 1, 0, 0, 0},
58+
{0, 1, 0, 0, 1},
59+
{1, 0, 0, 1, 1},
60+
{0, 0, 0, 0, 0},
61+
{1, 0, 1, 0, 1} } ;
62+
63+
island(mat);
64+
65+
return 0;
66+
}

0 commit comments

Comments
 (0)