Flood-Fill algorithm calculates relative cost of moving to a Block of an array and fills all consecutive blocks with numbers corresponding to cost. The Source is tagged as 0 as there is no additional cost to move there.
Here is the code:
#include <iostream.h>
#include <conio.h>
#define GridMaxX 5
#define GridMaxY 5
int Grid[GridMaxX][GridMaxY],r,c;
// r and c are coordinates of filling source.
void DisplayGrid()//Display the grid
{
for(int i=0;i<=GridMaxX-1;i++)
{
for(int j=0;j<=GridMaxY-1;j++)
cout<
cout<<"\n\n";
}
}
void Flood()//Flood the grid
{
for(int i=0;i<=GridMaxX-1;i++)
for(int j=0;j<=GridMaxY-1;j++)
Grid[i][j] = (((i-r)>=0)?(i-r):(r-i))+(((j-c)>=0)?(j-c):(c-j));
//x-distance(positive) added to y-distance(positive)
DisplayGrid();
}
void main()
{
clrscr();
//Initialize Grid with junk values of 99.
for(int i=0;i<=GridMaxX-1;i++)
for(int j=0;j<=GridMaxY-1;j++)
Grid[i][j]=99;
DisplayGrid();
//Input the source of Flooding
cout<<"ENTER SOURCE COORDINATES:\nx = ";
cin>>r;
cout<<"\ny = ";
cin>>c;
Flood();
getch();
}
No comments:
Post a Comment