Moderator: Board moderators
/*
runtime error;
vc6.0 : I 150+ 150+, F X Y COLOR => EXCEPTION !
BUT cygwin -> not exception;
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int row, col;
char table[251][251];
char color, color2;
void myfunc(int x, int y) {
table[y][x] = color;
if ((x+1 <= col) && (table[y][x+1] == color2)) {
myfunc(x+1, y);
}
if ((x-1 >= 1) && (table[y][x-1] == color2)) {
myfunc(x-1, y);
}
if ((y+1 <= row) && (table[y+1][x] == color2)) {
myfunc(x, y+1);
}
if ((y-1 >= 1) && (table[y-1][x] == color2)) {
myfunc(x, y-1);
}
}
void main()
{
char buf[82];
char filename[13];
int i, j;
char cmd;
int x, y;
int x1, y1, x2, y2;
int temp;
int flag = 1;
while (flag && fgets(buf, sizeof(buf), stdin)) {
switch (buf[0]) {
case 'I':
sscanf(buf, "%c %d %d", &cmd, &col, &row);
/* no break; */
case 'C':
for (i=1; i<=row; i++) {
for (j=1; j<=col; j++) {
table[i][j] = 'O';
}
}
break;
case 'L':
sscanf(buf, "%c %d %d %c", &cmd, &x, &y, &color);
table[y][x] = color;
break;
case 'V':
sscanf(buf, "%c %d %d %d %c", &cmd, &x, &y1, &y2, &color);
if (y1 > y2) {
temp = y1;
y1 = y2;
y2 = temp;
}
for (i=y1; i<=y2; i++) table[i][x] = color;
break;
case 'H':
sscanf(buf, "%c %d %d %d %c", &cmd, &x1, &x2, &y, &color);
if (x1 > x2) {
temp = x1;
x1 = x2;
x2 = temp;
}
for (i=x1; i<=x2; i++) table[y][i] = color;
break;
case 'K':
sscanf(buf, "%c %d %d %d %d %c", &cmd, &x1, &y1, &x2, &y2, &color);
for (i=y1; i<=y2; i++)
for (j=x1; j<=x2; j++)
table[i][j] = color;
break;
case 'F':
sscanf(buf, "%c %d %d %c", &cmd, &x, &y, &color);
color2 = table[y][x];
myfunc(x, y);
break;
case 'S':
sscanf(buf, "%c %s", &cmd, filename);
printf("%s\n", filename);
for (i=1; i<=row; i++) {
for (j=1; j<=col; j++) {
printf("%c", table[i][j]);
}
printf("\n");
}
break;
case 'X':
flag = 0;
break;
default:
break;
}
}
}
void fill(int i,int j,char color) {
char oldcolor = M[i][j];
if(oldcolor==color)
return;
M[i][j]=color;
for(int k=0; k<4; k++) {
int ii=i+di[k],jj=j+dj[k];
if(ii>=0&&ii<m&&jj>=0&&jj<n&&M[ii][jj]==oldcolor)
fill(ii,jj,color);
}
}
I 5 6
L 2 3 A
S one.bmp
G 2 3 J
F 3 3 J
V 2 3 4 W
H 3 4 2 Z
F 3 3 J
S two.bmp
Xone.bmp
OOOOO
OOOOO
OAOOO
OOOOO
OOOOO
OOOOO
two.bmp
JJJJJ
JJZZJ
JWJJJ
JWJJJ
JJJJJ
JJJJJflag = true;
do
{
cin >> comm;
if( comm == 'X' )
flag = false;
if( flag && comm != ' ' ) // process the command, the ' ' is because the text for the //problem says: ... "in the very beginning of the line" so I //thought they are using "non-valid" space-started command lines...
{
//here goes the switch - case...
}
}while( flag );
case 'L':
cin >> y >> x >> color;
image[x-1][y-1]= color;
break;
image[y-1][x-1] = color;
- By definition (X,Y) belongs to R.
---> then let's create a vector which holds this pixel, this vector is called R
do
{
- Now, every pixel, sharing sides with at least one of the pixels in R and being the same color, belongs to R. But.... not so fast, let's create another vector, called "conexos" (It's spanish, I'm Colombian :D ) in which I will store the pixels who share one side with at least one of the pixels stored in R, and have the same color, of course.
- Let's fill the pixels stored in R. For every pixel stored in R, perform a single-pixel fill.
- Empty R.
- Swap the data between R and conexos.
} while( R.size > 0 );
.......
...n...
..nRn..
...n...
.......
.......
..nn...
..nR...
.......
.......
...any help that i can provide.
#include <stdio.h>
int xcounter, ycounter; /* Simple counting vars */
int x1, x2, y1, y2, swap; /* Coordinates used in commands */
int C=260, R=260; /* M is num of columns, N is num of rows */
char cmd; /* Actual command to be processed */
char color, oldcolor; /* The color stored in the image array */
char filename[255]; /* Place to the store the file name string */
int rows=260; /* Maximum number of rows */
int cols=260; /* Maximum number of columns */
char image[260][260]; /* Array to hold the image */
int tempx, tempy;
char tempnewcolor, tempoldcolor;
void floodfill(tempx, tempy, tempnewcolor, tempoldcolor)
{
/* Is this a legal pixel */
if ((tempx<1)||(tempx>C)||(tempy<1)||(tempy>R))
return;
if (image[tempy][tempx]==tempoldcolor)
{
image[tempy][tempx]=tempnewcolor;
floodfill(tempx+1,tempy,tempnewcolor,tempoldcolor);
floodfill(tempx-1,tempy,tempnewcolor,tempoldcolor);
floodfill(tempx,tempy+1,tempnewcolor,tempoldcolor);
floodfill(tempx,tempy-1,tempnewcolor,tempoldcolor);
}
else
{
return;
}
}
int main(int argc, char *argv[])
{
while (1)
{
/* Read in a command */
scanf("%c", &cmd);
switch(cmd) /* Process the input */
{
case 'X':
return 0; /* End the program */
break;
case 'I':
/* Create a new image and init to white (O) */
scanf("%d %d",&C, &R);
for (ycounter=1;ycounter<=R;ycounter++)
{
for (xcounter=1;xcounter<=C;xcounter++)
{
image[ycounter][xcounter]='O';
}
}
break;
case 'C':
/* Clear the table already made to white */
for (ycounter=1;ycounter<=R;ycounter++)
{
for (xcounter=1;xcounter<=C;xcounter++)
{
image[ycounter][xcounter]='O';
}
}
break;
case 'L':
/* Get the pixel then color it */
scanf("%d %d %c",&x1, &y1, &color);
image[y1][x1]=color;
break;
case 'V':
/* Draw a vertical segment of color in X, between
rows y1 and y2 inclusive */
scanf("%d %d %d %c",&x1,&y1,&y2,&color);
if (y1<=y2)
{
for(ycounter=y1;ycounter<=y2;ycounter++)
image[ycounter][x1]=color;
}
else
{
for(ycounter=y2;ycounter<=y1;ycounter++)
image[ycounter][x1]=color;
}
break;
case 'H':
/* Draw a horizontal segment of color in Y between cols
x1 and x2 inclusive */
scanf("%d %d %d %c",&x1,&x2,&y1,&color);
if (x1<=x2)
{
for(xcounter=x1;xcounter<=x2;xcounter++)
image[y1][xcounter]=color;
}
else
{
for(xcounter=x2;xcounter<=x1;xcounter++)
image[y1][xcounter]=color;
}
break;
case 'K':
/* Draw a filled in rectangle of color */
scanf("%d %d %d %d %c",&x1,&x2,&y1,&y2,&color);
/* What follows here is a way to always go from 1
end to the other, no matter which number is
bigger than the other */
for (ycounter=y1;
((y1<=y2)?(ycounter<=y2):(ycounter>=y2));
((y1<=y2)?(ycounter++):(ycounter--)) )
{
for (xcounter = x1;
((x1<=x2)?(xcounter<=x2):(xcounter>=x2));
((x1<=x2)?(xcounter++):(xcounter--)) )
{
image[ycounter][xcounter]=color;
}
}
break;
case 'F':
/* Perform the notorious flood fill */
scanf("%d %d %c",&x1,&y1,&color);
oldcolor=image[y1][x1];
floodfill(x1,y1,color,oldcolor);
break;
case 'S':
color=getchar();
gets(filename);
printf("%s\n",filename);
for (ycounter=1;ycounter<=R;ycounter++)
{
for (xcounter=1;xcounter<=C;xcounter++)
{
printf("%c",image[ycounter][xcounter]);
}
printf("\n");
}
break;
}
}
return 0;
}
Users browsing this forum: No registered users and 1 guest