I'm gonna kill myself! OMG! Please take a look at my program. I can't find ANYTHING wrong with it.
-I use iterative and VERY FAST method for flood fill.
-I tested my program with HUGE inputs and it worked excellent.
-I tested it with any kind of test data.
-I used my debug skills.
So it leaves me with this idea that I've done something silly again!


So here's my code.
Thank you guys for helping me out.
Code: Select all
#include <iostream>
#include <cmath>
#include <memory>
#include <queue>
using std::cout;
using std::endl;
using std::cin;
using std::queue;
char image[252][252];
int n, m;
char name[12];
enum Shape {HLINE, VLINE, RECT, REG};
#define ISGOOD(i,j) ((i > 0 && i <= m) && (j > 0 && j <= n) ? true : false)
void floodFillScanlineStack(int x, int y, char newColor, char oldColor)
{
if(oldColor == newColor) return;
queue< std::pair<int, int> > q;
int y1;
bool spanLeft, spanRight;
std::pair<int, int> p;
p.first = x;
p.second = y;
q.push(p);
while(!q.empty())
{
p = q.front();
x = p.first;
y = p.second;
q.pop();
y1 = y;
while(y1 >= 0 && image[x][y1] == oldColor) y1--;
y1++;
spanLeft = spanRight = false;
while(y1 <= n && image[x][y1] == oldColor )
{
image[x][y1] = newColor;
if(!spanLeft && x > 0 && image[x - 1][y1] == oldColor)
{
p.first = x - 1;
p.second = y1;
q.push(p);
spanLeft = true;
}
else if(spanLeft && x > 0 && image[x - 1][y1] != oldColor)
{
spanLeft = false;
}
if(!spanRight && x <= m - 1 && image[x + 1][y1] == oldColor)
{
p.first = x + 1;
p.second = y1;
q.push(p);
spanRight = true;
}
else if(spanRight && x <= m - 1 && image[x + 1][y1] != oldColor)
{
spanRight = false;
}
y1++;
}
}
}
void AddShape(int x1, int y1, int x2, int y2, char color, Shape sh)
{
int i, j;
char col;
switch(sh)
{
case VLINE:
if((y1 > y2) && (y1 != -1) && (y2 != -1))
std::swap(y1, y2);
for(i = y1; i <= y2; i++)
image[i][x1] = color;
break;
case HLINE:
if((x1 > x2) && (x1 != -1) && (x2 != -1))
std::swap(x1, x2);
for(i = x1; i <= x2; i++)
image[y1][i] = color;
break;
case RECT:
for(i = x1; i <= x2; i++)
{
for(j = y1; j <= y2; j++)
image[j][i] = color;
}
break;
case REG:
col = image[y1][x1];
floodFillScanlineStack(y1, x1, color, col);
break;
}
}
void printImage()
{
cout << name << endl;
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
cout << image[i][j];
cout << endl;
}
}
void IgnoreLine()
{
while(cin.get() != '\n');
}
int main()
{
char command, c;
int x1, x2, y1, y2;
while(cin >> command)
{
if(command == 'X')
break;
switch(command)
{
case 'I': memset(image, 'O', sizeof image); cin >> n >> m; break;
case 'C': memset(image, 'O', sizeof image); break;
case 'L': cin >> x1 >> y1 >> c; image[y1][x1] = c; break;
case 'V': cin >> x1 >> y1 >> y2 >> c; AddShape(x1, y1, -1, y2, c, VLINE); break;
case 'H': cin >> x1 >> x2 >> y1 >> c; AddShape(x1, y1, x2, -1, c, HLINE); break;
case 'K': cin >> x1 >> y1 >> x2 >> y2 >> c; AddShape(x1, y1, x2, y2, c, RECT); break;
case 'F': cin >> x1 >> y1 >> c; AddShape(x1, y1, -1, -1, c, REG); break;
case 'S': cin >> name; printImage(); break;
default: IgnoreLine(); break;
}
while(iswspace(cin.peek()))
cin.ignore();
}
return 0;
}