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

+1 vote
211 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

``````#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;
}
``````