top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write code in C to find out whether there is pool in the given matrices?

+1 vote
524 views

Assume 1 as land and 0 as water

Input

11111 
10001 
10001 
11111 

Output: matrix is pool

Input

11111 
11001 
11001 
10111 
11111 

Output: matrix is NOT pool

Input

11111 
11001 
11001 
10001 
11111 

Output: matrix is pool

Input

11111 
11101 
11001 
10001 
11111 

Output: matrix is pool

posted Aug 8, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes
#include<iostream>
using namespace std;
#define k 6

int checkBoundry(int arr[k][k])
{
    int i=0;
    while(i<k)
    {
        if(arr[0][i] == 0 || arr[k-1][i] == 0 || arr[i][0] == 0 || arr[i][k-1]==0)
            return 0;
        i++;
    }
    return 1;
}

void findpool(int arr[k][k])
{
    if(checkBoundry(arr))
    { 
        int flag =0;
        for(int i=0; i<k; i++)
            for(int j=0; j<k; j++)
            {
                if((arr[i][j] == 0) &&( arr[i][j+1] == 0 || arr[i][j-1]==0 || arr[i+1][j]==0 || arr[i-1][j] == 0))
                    flag =1;
                else if((arr[i][j]==0) && (arr[i-1][j-1] == 0 || arr[i-1][j+1]==0 || arr[i+1][j-1] == 0 || arr[i+1][j+1]==0))
                {   
                    flag = 0;
                    cout<<"matrix is NOT pool"<<endl;
                    return;
                }           
                else
                    continue;
            }
        if(flag)
            cout<<"matrix is pool"<<endl;
        else
            cout<<"matrix does is NOT pool"<<endl;
    }
    else
        cout<<"matrix is NOT pool"<<endl;
}

int main()
{
    int arr[k][k] = {{1,1,1,1,1,1},
                     {1,1,1,0,0,1},
                     {1,1,0,0,0,1},
                     {1,0,0,0,1,1},
                     {1,1,1,0,1,1},
                     {1,1,1,1,1,1}};

    findpool(arr);
    return 0;
}
answer Aug 8, 2014 by Prakash Singh
Similar Questions
0 votes

Enter character: r
Enter string: programming
Output: Positions of 'r' in programming are: 2 5
Character 'r' occurred for 2 times

+4 votes

Given a dictionary of strings and another string find out if the string is an exact match to some words in the dictionary or varies at most in only one place of some word of the dictionary?

...