// #abba
#include <iostream>
#define ROW 5
#define COL 5
using namespace std;
int count = 0;
bool countIslands(int M[][COL],int visited[][COL] ,int i, int j)
{
if( i<0 || j <0 || i>=ROW || j>=COL || visited[i][j]==1 || M[i][j]==0)
{
return false;
}
visited[i][j] = 1;
bool a = countIslands(M,visited,i+1,j);
bool b = countIslands(M,visited,i-1,j);
bool c = countIslands(M,visited,i,j+1);
bool d = countIslands(M,visited,i,j-1);
bool e = countIslands(M,visited,i-1,j-1);
bool f = countIslands(M,visited,i-1,j+1);
bool g = countIslands(M,visited,i+1,j+1);
bool h = countIslands(M,visited,i+1,j-1);
if(!a && !b && !c && !d && !e && !f && !g && !h )
{
count++;
return true;
}
return a || b || c || d || e || f || g || h;
}
int main()
{
int M[][COL] = { { 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 } };
int N[][COL] = { { 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 } };
countIslands(M,N,0,0);
for(int i = 0 ; i < ROW ; i++)
{
for(int j = 0 ; j < COL ; j++)
{
countIslands(M,N,i,j);
}
}
cout << "Number of islands is: " << count;;
return 0;
}
Comments
Post a Comment