Find the number of islands

PROBLEM :
A group of connected 1s forms an island now your task is to complete the method findIslands which returns the no of islands present. The function  takes three arguments the first is the boolean matrix A and then the next two arguments are N and M denoting the size of the matrix A .

Input:
The first line of input will be the no of test cases T then T test cases follow. The first line of each test case contains Two space separated integers N and M. Then in the next line are the NxM inputs of the matrix A separated by space .

Output:
The output in the expected output will be the total no of islands present.

Constraints:
1<=T<=100
1<=N,M<=50
0<=A[][]<=1

Example(To be used only for expected output) :
Input
1
3 3
1 1 0 0 0 1 1 0 1

Output
2

Explanation
The graph will look like
1 1 0
0 0 1
1 0 1
Here  two islands will be formed
First island will be formed by elements { A[0][0] ,  A[0][1], A[1][2], A[2][2] }
Sec island will be formed by  { A[2][0] }

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*you are required to complete this method*/

void IslandDFS(int [][MAX],int ,int ,int ,int ,bool [][MAX]) ;
bool isConnected(int [][MAX],int ,int ,int ,int ,bool [][MAX]) ;

int findIslands(int A[MAX][MAX], int N, int M)
{
    bool visited[MAX][MAX] ;
    memset(visited, 0, sizeof(visited));
    int count=0 ;
   
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<M;j++)
        {
            if(A[i][j]&&!visited[i][j])
            {
                IslandDFS(A,N,M,i,j,visited) ;
                    count++ ;
            }
        }
    }
    return count ;
}

void IslandDFS(int arr[][MAX],int N,int M,int i,int j,bool visited[][MAX])
{
    visited[i][j]=true ;
   
    int t1[]={-1,-1,-1,0,0,1,1,1} ;
    int t2[]={-1,0,1,-1,1,-1,0,1} ;
   
    for(int k=0;k<8;k++)
    {
        if(isConnected(arr,N,M,i+t1[k],j+t2[k],visited))
            IslandDFS(arr,N,M,i+t1[k],j+t2[k],visited) ;
    }
}

bool isConnected(int arr[][MAX],int N,int M,int i,int j,bool visited[][MAX])
{
    return ((i>=0)&&(j>=0)&&(i<N)&&(j<M)&&(arr[i][j])&&(!visited[i][j])) ;
}

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/*you are required to complete this method*/

void IslandBFS(int [][MAX],int ,int ,int ,int ,bool [][MAX]) ;

int findIslands(int A[MAX][MAX], int N, int M)
{
    bool visited[MAX][MAX] ;
    memset(visited, 0, sizeof(visited));
    int count=0 ;
   
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<M;j++)
        {
            if(A[i][j]&&!visited[i][j])
            {
                IslandBFS(A,N,M,i,j,visited) ;
                    count++ ;
            }
        }
    }
    return count ;
}

void IslandBFS(int arr[][MAX],int N,int M,int i,int j,bool visited[][MAX])
{
    if(i>N||i<0)
        return ;
    if(j>M||j<0)
        return ;
    if(visited[i][j]||arr[i][j]==0)
        return ;
    visited[i][j]=true ;
   
    int t1[]={-1,-1,-1,0,0,1,1,1} ;
    int t2[]={-1,0,1,-1,1,-1,0,1} ;
   
    for(int k=0;k<8;k++)
        IslandBFS(arr,N,M,i+t1[k],j+t2[k],visited) ;
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )