atul
BAN USERHere is a sample solution for above input, can be easily converted for dynamic input:
#include <iostream>
using namespace std;
struct block_type
{
char data;
bool visited;
};
int main()
{
char input_array [5][5] = {{'.','.','B','.','.'},
{'.','.','G','R','R'},
{'.','.','B','.','.'},
{'R','.','.','.','.'},
{'R','.','.','.','.'}};
block_type array[5][5];
int i,j,k;
int block_count=0;
for (i =0; i<5; i++)
{
for (j= 0; j<5; j++)
{
array[i][j].data = input_array[i][j];
array[i][j].visited = false;
}
}
for (i =0; i<5; i++)
{
for (j= 0; j<5; j++)
{
if(!array[i][j].visited)
{
if(array[i][j].data == 'B')
{
block_count++;
// mark all B and G nodes in length as visited
for (k=i; k<5; k++)
{
if(array[k][j].data == 'B' || array[k][j].data == 'G')
{
array[k][j].visited = true;
}
else
{
break;
}
}
}
if(array[i][j].data == 'R')
{
block_count++;
// mark all R and G nodes in width as visited
for (k=j; k<5; k++)
{
if(array[i][k].data == 'R' || array[i][k].data =='G')
{
array[i][k].visited = true;
}
else
break;
}
}
}
}
}
cout << "\n " << block_count << "\n";
return 0;
}
Repcassicjohnson, Android Engineer at Accenture
Hi, I work as an Painter, making art Paintings for the world’s top art galleries. When I have an ...
Repmarierlewis2, AT&T Customer service email at ASAPInfosystemsPvtLtd
I am Marie and I live in Los Angeles USA, I have a strong base in marketing and I believe ...
Not complete...but it would give you an idea :
- atul June 21, 2015