-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathnumber_of_island.cpp
59 lines (56 loc) · 1.17 KB
/
number_of_island.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
#include <bits/stdc++.h>
using namespace std;
bool isSafe(int N,int M,int i,int j)
{
if(i < 0 || i >= N)
return false;
if(j < 0 || j >= M)
return false;
return true;
}
void delIsland(int **A,int N,int M,int i,int j)
{
if(isSafe(N,M,i,j) && A[i][j] == 1)
{
A[i][j] = 0;
delIsland(A, N , M , i-1, j-1);
delIsland(A, N, M, i-1, j);
delIsland(A, N, M, i-1, j+1);
delIsland(A, N, M, i, j-1);
delIsland(A, N, M, i, j+1);
delIsland(A, N, M, i+1, j-1);
delIsland(A, N, M, i+1, j);
delIsland(A, N, M, i+1, j+1);
}
}
int TotalBridge(int **A, int N, int M)
{
int count = 0 ;
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
if(A[i][j] == 1)
{
count++;
delIsland(A,N,M,i,j);
}
}
if(count == 0)
return 0;
return count;
}
int main()
{
int N,M;
cout << “nEnter the order of the islands : “;
cin >> N >> M;
int **A = (int **)malloc(N * sizeof(int *));
for (int i = 0; i < N; i++)
A[i] = (int *)malloc(M * sizeof(int));
cout << “nEnter the island matrix : n”;
for(int i = 0; i < N ; i++)
for(int j = 0; j < M; j++)
cin >> A[i][j];
cout << “Total Number of islands : ” << TotalBridge(A,N,M)<<endl;
return 0;
}