-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathIsland-problem.cpp
More file actions
51 lines (45 loc) · 1.16 KB
/
Island-problem.cpp
File metadata and controls
51 lines (45 loc) · 1.16 KB
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
#include<iostream>
#include<bits/stdc++.h>
#define R 5
#define C 5
using namespace std;
int isSafe(int arr[][C], int row, int col, bool visit[][C])
{
return (row >= 0) && (row < R) && (col >= 0) && (col < C) && (arr[row][col] && !visit[row][col]);
}
int search(int arr[][C],int row,int col,bool visit[][C])
{
static int rowNbr[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
static int colNbr[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
visit[row][col] = true;
for (int k = 0; k < 8; ++k)
if (isSafe(arr, row + rowNbr[k], col + colNbr[k], visit))
search(arr, row + rowNbr[k], col + colNbr[k], visit);
}
int call(int arr[][C])
{
bool visit[R][C];
memset(visit,0,sizeof(visit));
int count =0;
for(int i=0;i<R;i++)
{
for(int j=0;j<C;j++)
{
if(arr[i][j] && !visit[i][j])
{
search(arr,i,j,visit);
++count;
}
}
}
return count;
}
int main()
{
int arr[][C]={ { 1, 1, 0, 0, 0 },
{ 0, 1, 0, 0, 1 },
{ 1, 0, 0, 1, 1 },
{ 0, 0, 0, 0, 0 },
{ 1, 0, 1, 0, 1 } };
cout<<"Islands:"<<call(arr);
}