10267 - Graphical Editor
Moderator: Board moderators
-
- Experienced poster
- Posts: 187
- Joined: Wed Dec 11, 2002 2:03 pm
- Location: Mount Papandayan, Garut
No I got WA with my recursive function. The old color is and the new color is
Code: Select all
fillRegion()
Code: Select all
regionColor
Code: Select all
ch
WA :( huh!
[cpp]
/* @JUDGE_ID: 37008HZ 10267 C++ "hi" */
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
using namespace std;
char map[300][300];
int m=0,n=0;
int my_min(int a, int b) { if (a<b) return a; return b; }
int my_max(int a, int b) { if (a>b) return a; return b; }
void full(char c)
{
int i,j;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
map[j] = c;
}
void write_map()
{
int i,j;
for(j=1;j<=n;j++)
{
for(i=1;i<=m;i++)
cout<<map[j];
cout<<endl;
}
}
bool invalid(int x, int y){
if (x<1 || x>m || y<1 || y>n) return true;
return false;
}
struct xy{
int x,y;
};
void fillit(int x, int y, char oldc, char c){
int px[4] = {-1,1,0,0};
int py[4] = {0,0,1,-1};
int i;
if ( invalid(x,y) ) return;
if ( map[x][y] == c ) return;
xy clss[255*255];
int beg=0, end =0;
clss[0].x = x;
clss[0].y = y;
map[x][y]=c;
while( beg <= end )
{
xy a = clss[beg++];
for(i=0;i<4;i++)
{
int nx = a.x+px, ny= a.y+py;
if (!invalid(nx,ny))
{
if ( map[nx][ny]==oldc)
{
clss[++end].x = nx;
clss[end].y = ny;
map[nx][ny] = c;
}
}
}
}
}
void hl(int x, int y, int endx, char c)
{
int i;
if (y<1 || y>n) return;
for(i=my_max(1, x); i<= my_min(endx, m); i++)
map[ y ] = c;
}
void vl(int x, int y, int endy, char c)
{
int i;
if (x<1 || x>m) return;
for(i=my_max(1, y); i<= my_min(endy, n); i++)
map[x][ i ] = c;
}
int main()
{
char command;
int i,j;
while(true){
cin>>command;
char t[1000];
int x,y,x1,y1;
char c;
switch(command){
case 'X': return 0;
case 'S':
{ char name[1000];
cin>>name;
cout<<name<<endl;
write_map(); break;
}
case 'F':
cin>>x>>y>>c;
fillit(x,y,map[x][y],c);
break;
case 'K':
cin>>x>>y>>x1>>y1>>c;
for(i=x;i<=x1;i++)
vl(i,y,y1,c);
break;
case 'H':
cin>>x>>x1>>y>>c;
hl(x,y,x1,c);
break;
case 'V':
cin>>x>>y>>y1>>c;
vl(x,y,y1,c);
break;
case 'L':
cin>>x>>y>>c;
map[x][y] = c;
break;
case 'C':
full('O');
break;
case 'I':
cin>>m>>n;
full('O');
break;
default:
cin.getline(t,1000);
} //end of switch
}
}[/cpp]
any suggestions?
/* @JUDGE_ID: 37008HZ 10267 C++ "hi" */
#include<iostream>
#include<string>
#include<cstdlib>
#include<cstdio>
using namespace std;
char map[300][300];
int m=0,n=0;
int my_min(int a, int b) { if (a<b) return a; return b; }
int my_max(int a, int b) { if (a>b) return a; return b; }
void full(char c)
{
int i,j;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
map[j] = c;
}
void write_map()
{
int i,j;
for(j=1;j<=n;j++)
{
for(i=1;i<=m;i++)
cout<<map[j];
cout<<endl;
}
}
bool invalid(int x, int y){
if (x<1 || x>m || y<1 || y>n) return true;
return false;
}
struct xy{
int x,y;
};
void fillit(int x, int y, char oldc, char c){
int px[4] = {-1,1,0,0};
int py[4] = {0,0,1,-1};
int i;
if ( invalid(x,y) ) return;
if ( map[x][y] == c ) return;
xy clss[255*255];
int beg=0, end =0;
clss[0].x = x;
clss[0].y = y;
map[x][y]=c;
while( beg <= end )
{
xy a = clss[beg++];
for(i=0;i<4;i++)
{
int nx = a.x+px, ny= a.y+py;
if (!invalid(nx,ny))
{
if ( map[nx][ny]==oldc)
{
clss[++end].x = nx;
clss[end].y = ny;
map[nx][ny] = c;
}
}
}
}
}
void hl(int x, int y, int endx, char c)
{
int i;
if (y<1 || y>n) return;
for(i=my_max(1, x); i<= my_min(endx, m); i++)
map[ y ] = c;
}
void vl(int x, int y, int endy, char c)
{
int i;
if (x<1 || x>m) return;
for(i=my_max(1, y); i<= my_min(endy, n); i++)
map[x][ i ] = c;
}
int main()
{
char command;
int i,j;
while(true){
cin>>command;
char t[1000];
int x,y,x1,y1;
char c;
switch(command){
case 'X': return 0;
case 'S':
{ char name[1000];
cin>>name;
cout<<name<<endl;
write_map(); break;
}
case 'F':
cin>>x>>y>>c;
fillit(x,y,map[x][y],c);
break;
case 'K':
cin>>x>>y>>x1>>y1>>c;
for(i=x;i<=x1;i++)
vl(i,y,y1,c);
break;
case 'H':
cin>>x>>x1>>y>>c;
hl(x,y,x1,c);
break;
case 'V':
cin>>x>>y>>y1>>c;
vl(x,y,y1,c);
break;
case 'L':
cin>>x>>y>>c;
map[x][y] = c;
break;
case 'C':
full('O');
break;
case 'I':
cin>>m>>n;
full('O');
break;
default:
cin.getline(t,1000);
} //end of switch
}
}[/cpp]
any suggestions?
10267
Hello everybody,
I am currently trying to solve this problem. However, I always get WA. Does anyone of you have some test cases with which I can check whether my problem is working correctly?
Alternatively, myy source is the following (the uva compiler gives a compile error; the judge on programming-challenges gives WA). Can anybody find errors in my source?
[/code]
I am currently trying to solve this problem. However, I always get WA. Does anyone of you have some test cases with which I can check whether my problem is working correctly?
Alternatively, myy source is the following (the uva compiler gives a compile error; the judge on programming-challenges gives WA). Can anybody find errors in my source?
Code: Select all
[java]
import java.io.*;
import java.util.*;
public class GraphicalEditor {
public static void main(String[] args) {
try {
String tmp;
boolean start = true;
char[][] matrix = null;
while (!(tmp = readLn().trim()).equals("X")) {
StringTokenizer tok = new StringTokenizer(tmp);
tmp = tok.nextToken();
if (tmp.equals("I")) {
int t = Integer.parseInt(tok.nextToken());
matrix =
new char[Integer.parseInt(tok.nextToken()) + 2][t + 2];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 'O';
}
}
} else if (tmp.equals("C")) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[i][j] = 'O';
}
}
} else if (tmp.equals("L")) {
int t = Integer.parseInt(tok.nextToken());
matrix[Integer.parseInt(tok.nextToken())][t] =
tok.nextToken().charAt(0);
} else if (tmp.equals("V")) {
int x = Integer.parseInt(tok.nextToken());
int y1 = Integer.parseInt(tok.nextToken());
int y2 = Integer.parseInt(tok.nextToken());
char color = tok.nextToken().charAt(0);
int max = Math.max(y1, y2);
for (int i = Math.min(y1, y2); i <= max; i++) {
matrix[i][x] = color;
}
} else if (tmp.equals("H")) {
int x1 = Integer.parseInt(tok.nextToken());
int x2 = Integer.parseInt(tok.nextToken());
int y = Integer.parseInt(tok.nextToken());
char color = tok.nextToken().charAt(0);
int max = Math.max(x1, x2);
for (int i = Math.min(x1, x2); i <= max; i++) {
matrix[y][i] = color;
}
} else if (tmp.equals("K")) {
int x1 = Integer.parseInt(tok.nextToken());
int y1 = Integer.parseInt(tok.nextToken());
int x2 = Integer.parseInt(tok.nextToken());
int y2 = Integer.parseInt(tok.nextToken());
char color = tok.nextToken().charAt(0);
int max = Math.max(x1, x2);
int maxy = Math.max(y1, y2);
for (int i = Math.min(x1, x2); i <= max; i++) {
for (int j = Math.min(y1, y2); j <= max; j++) {
matrix[j][i] = color;
}
}
} else if (tmp.equals("F")) {
int x = Integer.parseInt(tok.nextToken());
int y = Integer.parseInt(tok.nextToken());
char color = tok.nextToken().charAt(0);
boolean[][] inRegion =
new boolean[matrix.length][matrix[0].length];
inRegion[y][x] = true;
char prevColor = matrix[y][x];
// System.out.println(prevColor);
matrix[y][x] = color;
inRegion(x, y, matrix, inRegion, color, prevColor);
} else if (tmp.equals("S")) {
if (!start) {
System.out.println();
}
start = false;
System.out.println(tok.nextToken());
for (int i = 1; i < matrix.length - 1; i++) {
for (int j = 1; j < matrix[i].length - 1; j++) {
System.out.print(matrix[i][j]);
}
if (i != matrix.length-2)
System.out.println();
}
} else if (tmp.equals("X")) {
return;
}
}
} catch (Exception e) {
while (true);
}
}
static void inRegion(
int x,
int y,
char[][] matrix,
boolean[][] inRegion,
char color,
char prevcolor) {
x--;
if (x != 0
&& y != 0
&& x != matrix[0].length - 1
&& y != matrix.length - 1
&& matrix[y][x] == prevcolor
&& !inRegion[y][x]) {
inRegion[y][x] = true;
matrix[y][x] = color;
inRegion(x, y, matrix, inRegion, color, prevcolor);
}
x += 2;
if (x != 0
&& y != 0
&& x != matrix[0].length - 1
&& y != matrix.length - 1
&& matrix[y][x] == prevcolor
&& !inRegion[y][x]) {
inRegion[y][x] = true;
matrix[y][x] = color;
inRegion(x, y, matrix, inRegion, color, prevcolor);
}
x--;
y--;
if (x != 0
&& y != 0
&& x != matrix[0].length - 1
&& y != matrix.length - 1
&& matrix[y][x] == prevcolor
&& !inRegion[y][x]) {
inRegion[y][x] = true;
matrix[y][x] = color;
inRegion(x, y, matrix, inRegion, color, prevcolor);
}
y += 2;
if (x != 0
&& y != 0
&& x != matrix[0].length - 1
&& y != matrix.length - 1
&& matrix[y][x] == prevcolor
&& !inRegion[y][x]) {
inRegion[y][x] = true;
matrix[y][x] = color;
inRegion(x, y, matrix, inRegion, color, prevcolor);
}
y--;
}
static String readLn() // utility function to read from stdin
{
int maxLg = 200;
byte lin[] = new byte[maxLg];
int lg = 0, car = -1;
String line = "";
try {
while (lg < maxLg) {
car = System.in.read();
if (car < 0)
break;
else if (car == '\n') {
lin[lg++] += car;
break;
}
lin[lg++] += car;
}
} catch (IOException e) {
return (null);
}
if ((car < 0) && (lg == 0))
return (new String("")); // eof
return new String(lin, 0, lg);
}
}
[/java]
10267 WA
I just can't make the prob. AC. Can you help? plz
[cpp]
#include<stdio.h>
struct Vertex
{
int X,Y;
char C;
};
char board[250][250]; //Drawing Board
int d1,d2;
int Visited[250][250];
void Explore(Vertex V)
{
int x[4][2]={-1,0,0,1,1,0,0,-1};
int cX,cY,i;
Vertex v;
if(Visited[V.Y][V.X]==1)
{
return;
}
for(i=0;i<4;i++)
{
cX=V.X+x[0];
cY=V.Y+x[1];
Visited[V.Y][V.X]=1;
if((cX>0&&cY>0)&&(cX<=d2&&cY<=d1))
{
if(board[cY][cX]=='O'||board[cY][cX]==V.C)
{
v.X=cX;
v.Y=cY;
v.C=V.C;
board[cY][cX]=V.C;
Explore(v);
}
}
}
}
int main()
{
char C; //Color
int M,N; //New Image
int X1,Y1,X2,Y2; //Co-ordinates
char fName[14];
char CMD[100];
int tmp;
int i,j;
bool exit;
while(1)
{
exit=false;
gets(CMD);
switch(CMD[0])
{
case 'I':
sscanf(CMD,"%*c %d %d",&M,&N);
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
{
board[j]='O';
Visited[j]=0;
}
}
break;
case 'C':
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
{
board[j]='O';
Visited[j]=0;
}
}
break;
case 'L':
sscanf(CMD,"%*c %d %d %c",&X1,&Y1,&C);
board[Y1][X1]=C;
break;
case 'V':
sscanf(CMD,"%*c %d %d %d %c",&X1,&Y1,&Y2,&C);
if(Y2<Y1)
{
tmp=Y1;
Y1=Y2;
Y2=tmp;
}
for(i=Y1;i<=Y2;i++)
{
board[X1]=C;
}
break;
case 'H':
sscanf(CMD,"%*c %d %d %d %c",&X1,&X2,&Y1,&C);
if(X2<X1)
{
tmp=X1;
X1=X2;
X2=tmp;
}
for(i=X1;i<=X2;i++)
{
board[Y1]=C;
}
break;
case 'K':
sscanf(CMD,"%*c %d %d %d %d %c",&X1,&Y1,&X2,&Y2,&C);
for(i=X1;i<=X2;i++)
{
for(j=Y1;j<=Y2;j++)
{
board[j]=C;
}
}
break;
case 'S':
sscanf(CMD,"%*c %s",fName);
printf("%s\n",fName);
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
{
printf("%c",board[j]);
}
printf("\n");
}
break;
case 'F':
sscanf(CMD,"%*c %d %d %c",&X1,&Y1,&C);
Vertex V;
board[Y1][X1]=C;
V.X=X1;
V.Y=Y1;
V.C=C;
d1=N;
d2=M;
Explore(V);
break;
case 'X':
exit=true;
break;
default:
;
}
if(exit)
{
break;
}
}
return 0;
}
[/cpp]

[cpp]
#include<stdio.h>
struct Vertex
{
int X,Y;
char C;
};
char board[250][250]; //Drawing Board
int d1,d2;
int Visited[250][250];
void Explore(Vertex V)
{
int x[4][2]={-1,0,0,1,1,0,0,-1};
int cX,cY,i;
Vertex v;
if(Visited[V.Y][V.X]==1)
{
return;
}
for(i=0;i<4;i++)
{
cX=V.X+x[0];
cY=V.Y+x[1];
Visited[V.Y][V.X]=1;
if((cX>0&&cY>0)&&(cX<=d2&&cY<=d1))
{
if(board[cY][cX]=='O'||board[cY][cX]==V.C)
{
v.X=cX;
v.Y=cY;
v.C=V.C;
board[cY][cX]=V.C;
Explore(v);
}
}
}
}
int main()
{
char C; //Color
int M,N; //New Image
int X1,Y1,X2,Y2; //Co-ordinates
char fName[14];
char CMD[100];
int tmp;
int i,j;
bool exit;
while(1)
{
exit=false;
gets(CMD);
switch(CMD[0])
{
case 'I':
sscanf(CMD,"%*c %d %d",&M,&N);
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
{
board[j]='O';
Visited[j]=0;
}
}
break;
case 'C':
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
{
board[j]='O';
Visited[j]=0;
}
}
break;
case 'L':
sscanf(CMD,"%*c %d %d %c",&X1,&Y1,&C);
board[Y1][X1]=C;
break;
case 'V':
sscanf(CMD,"%*c %d %d %d %c",&X1,&Y1,&Y2,&C);
if(Y2<Y1)
{
tmp=Y1;
Y1=Y2;
Y2=tmp;
}
for(i=Y1;i<=Y2;i++)
{
board[X1]=C;
}
break;
case 'H':
sscanf(CMD,"%*c %d %d %d %c",&X1,&X2,&Y1,&C);
if(X2<X1)
{
tmp=X1;
X1=X2;
X2=tmp;
}
for(i=X1;i<=X2;i++)
{
board[Y1]=C;
}
break;
case 'K':
sscanf(CMD,"%*c %d %d %d %d %c",&X1,&Y1,&X2,&Y2,&C);
for(i=X1;i<=X2;i++)
{
for(j=Y1;j<=Y2;j++)
{
board[j]=C;
}
}
break;
case 'S':
sscanf(CMD,"%*c %s",fName);
printf("%s\n",fName);
for(i=1;i<=N;i++)
{
for(j=1;j<=M;j++)
{
printf("%c",board[j]);
}
printf("\n");
}
break;
case 'F':
sscanf(CMD,"%*c %d %d %c",&X1,&Y1,&C);
Vertex V;
board[Y1][X1]=C;
V.X=X1;
V.Y=Y1;
V.C=C;
d1=N;
d2=M;
Explore(V);
break;
case 'X':
exit=true;
break;
default:
;
}
if(exit)
{
break;
}
}
return 0;
}
[/cpp]
For those who still didn't get AC:
This problem is relativelly simple, but took me hours. I found 1 bad trick:
--in K command, X2>X1 and Y2>Y1, but this is not always true for V and H commands.
I got stack overflow (R.E. SIGSEGV) with my simple resursive function, so I switched for a more complex, memory optimised and interative fill function. But some people are stating they got AC with recursive fill function.
This problem is relativelly simple, but took me hours. I found 1 bad trick:
--in K command, X2>X1 and Y2>Y1, but this is not always true for V and H commands.
I got stack overflow (R.E. SIGSEGV) with my simple resursive function, so I switched for a more complex, memory optimised and interative fill function. But some people are stating they got AC with recursive fill function.
Last edited by Algoritmo on Fri Jan 23, 2004 12:38 pm, edited 2 times in total.
-
- New poster
- Posts: 35
- Joined: Sat Jan 05, 2002 2:00 am
- Contact:
I think "X2 is not always greater than X1" is the standard trick. See, for instance, problem 100Algoritmo wrote: --in K command, X2>X1 and Y2>Y1, but this is not always true for V and H commands.

Last edited by Alexander Denisjuk on Thu Jan 29, 2004 9:28 am, edited 1 time in total.
In fact, the sintax is as expected: K X1 Y1 X2 Y2 CAlexander Denisjuk wrote:Why do you think K X1 Y1 X2 Y2 C would be better?Algoritmo wrote:I found 2 bad tricks:
--K comand synthax is not K X1 Y1 X2 Y2 C, but K X1 X2 Y1 Y2 C.
I made a mistake with that by myself. Maybe I was stressed. Note that I have edited and removed that issue from my post.
10267
I am a beginner...
I don't know how to make it works my code, I understand the problem and I think I know how to resolve but I don't know how to use in the right way the strings...
here is my code:
[c]
#include <stdio.h>
#include <string.h>
int main()
{
int m,n,x,x1,y,y1;
int i,j; /*contadores*/
char command,pixel[251][251],c,filename;
while(command!='X')
{
scanf("%c",&command);
if (command=='I')
{
scanf("%d %d", m,n);
break;
}
else if (command=='C')
{
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
pixel[m][n]='0';
}
else if (command=='L')
{
scanf("%d %d %c",&x,&y,&c);
strcpy(pixel[x][y],c);
}
else if (command=='V')
{
scanf("%d %d %d %c",&x,&y,&y1,&c);
for (i=y; i<=y1; i++)
strcpy(pixel[x],c);
}
else if (command=='H')
{
scanf("%d %d %d %c",&x,&x1,&y,&c);
for (i=x; i<=x1; i++)
strcpy(pixel[x],c);
}
else if (command=='K')
{
scanf("%d %d %d %c",&x,&y,&x1,&y1,&c);
for (i=x; i<=x1; i++)
for (j=y; j<=y1; j++)
strcpy(pixel[j],c);
}
else if (command=='F')
{
scanf("%d %d %c",&x,&y,&c);
strcpy(pixel[x][y],c);
}
else if (command=='S')
{
scanf("%s", &filename);
printf("%s", filename);
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
{
print("%c", pixel[j]);
if (j==n)
printf("\n");
}
}
else if (command=='X')
{
return 0;
}
}
return 0;
}
[/c]
If someone could give me an idea of how to do it I beg to help me!
bye
I don't know how to make it works my code, I understand the problem and I think I know how to resolve but I don't know how to use in the right way the strings...
here is my code:
[c]
#include <stdio.h>
#include <string.h>
int main()
{
int m,n,x,x1,y,y1;
int i,j; /*contadores*/
char command,pixel[251][251],c,filename;
while(command!='X')
{
scanf("%c",&command);
if (command=='I')
{
scanf("%d %d", m,n);
break;
}
else if (command=='C')
{
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
pixel[m][n]='0';
}
else if (command=='L')
{
scanf("%d %d %c",&x,&y,&c);
strcpy(pixel[x][y],c);
}
else if (command=='V')
{
scanf("%d %d %d %c",&x,&y,&y1,&c);
for (i=y; i<=y1; i++)
strcpy(pixel[x],c);
}
else if (command=='H')
{
scanf("%d %d %d %c",&x,&x1,&y,&c);
for (i=x; i<=x1; i++)
strcpy(pixel[x],c);
}
else if (command=='K')
{
scanf("%d %d %d %c",&x,&y,&x1,&y1,&c);
for (i=x; i<=x1; i++)
for (j=y; j<=y1; j++)
strcpy(pixel[j],c);
}
else if (command=='F')
{
scanf("%d %d %c",&x,&y,&c);
strcpy(pixel[x][y],c);
}
else if (command=='S')
{
scanf("%s", &filename);
printf("%s", filename);
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
{
print("%c", pixel[j]);
if (j==n)
printf("\n");
}
}
else if (command=='X')
{
return 0;
}
}
return 0;
}
[/c]
If someone could give me an idea of how to do it I beg to help me!
bye
10267 (Graphical Editor) W.A.
I don't know that is wrong with my code...I have test several Inputs/Outputs and it works perfectly...or that's what I think...
If you know some Strange Input/Output or examples for Input/Output that doesn't work with my code plz tell me!
Here is my code:
[c]
#include <stdio.h>
void floodfill(char pixel[260][260],int x,int y,char c, int m, int n);
int main()
{
int m,n,x,x1,y,y1;
int temp;
int i,j; /*contadores*/
char command,pixel[260][260],c,filename[500];
while(scanf("%c",&command)!=EOF)
{
if (command=='I')
{
scanf("%d %d", &m,&n);
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
pixel[j]='O';
}
else if (command=='C')
{
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
pixel[j]='O';
}
else if (command=='L')
{
scanf("%d %d %c",&x,&y,&c);
pixel[x][y]=c;
}
else if (command=='V')
{
scanf("%d %d %d %c",&x,&y,&y1,&c);
if (y>y1)
{
temp=y1;
y1=y;
y=temp;
}
for (i=y; i<=y1; i++)
pixel[x]=c;
}
else if (command=='H')
{
scanf("%d %d %d %c",&x,&x1,&y,&c);
if (x>x1)
{
temp=x1;
x1=x;
x=temp;
}
for (i=x; i<=x1; i++)
pixel[y]=c;
}
else if (command=='K')
{
scanf("%d %d %d %c",&x,&y,&x1,&y1,&c);
if (x>x1)
{
temp=x1;
x1=x;
x=temp;
}
if (y>y1)
{
temp=y1;
y1=y;
y=temp;
}
for (i=x; i<=x1; i++)
for (j=y; j<=y1; j++)
pixel[j]=c;
}
else if (command=='F')
{
scanf("%d %d %c",&x,&y,&c);
floodfill(pixel,x,y,c,m,n);
}
else if (command=='S')
{
scanf("%s", &filename);
printf("%s\n", filename);
for (i=1; i<=n; i++)
for (j=1; j<=m; j++)
{
printf("%c", pixel[j]);
if (j==m)
printf("\n");
}
}
else if (command=='X')
{
return 0;
}
}
}
void floodfill(char pixel[260][260],int x,int y,char c,int m, int n)
{
char f;
f=pixel[x][y];
pixel[x][y]=c;
if (x>=0 && x<=m)
if (y>=0 && y<=n)
if (f!=c)
{
if (f==pixel[x+1][y])
floodfill(pixel,x+1,y,c,m,n);
if (f==pixel[x][y+1])
floodfill(pixel,x,y+1,c,m,n);
if (f==pixel[x-1][y])
floodfill(pixel,x-1,y,c,m,n);
if (f==pixel[x][y-1])
floodfill(pixel,x,y-1,c,m,n);
}
} [/c]
bye!
If you know some Strange Input/Output or examples for Input/Output that doesn't work with my code plz tell me!
Here is my code:
[c]
#include <stdio.h>
void floodfill(char pixel[260][260],int x,int y,char c, int m, int n);
int main()
{
int m,n,x,x1,y,y1;
int temp;
int i,j; /*contadores*/
char command,pixel[260][260],c,filename[500];
while(scanf("%c",&command)!=EOF)
{
if (command=='I')
{
scanf("%d %d", &m,&n);
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
pixel[j]='O';
}
else if (command=='C')
{
for (i=1; i<=m; i++)
for (j=1; j<=n; j++)
pixel[j]='O';
}
else if (command=='L')
{
scanf("%d %d %c",&x,&y,&c);
pixel[x][y]=c;
}
else if (command=='V')
{
scanf("%d %d %d %c",&x,&y,&y1,&c);
if (y>y1)
{
temp=y1;
y1=y;
y=temp;
}
for (i=y; i<=y1; i++)
pixel[x]=c;
}
else if (command=='H')
{
scanf("%d %d %d %c",&x,&x1,&y,&c);
if (x>x1)
{
temp=x1;
x1=x;
x=temp;
}
for (i=x; i<=x1; i++)
pixel[y]=c;
}
else if (command=='K')
{
scanf("%d %d %d %c",&x,&y,&x1,&y1,&c);
if (x>x1)
{
temp=x1;
x1=x;
x=temp;
}
if (y>y1)
{
temp=y1;
y1=y;
y=temp;
}
for (i=x; i<=x1; i++)
for (j=y; j<=y1; j++)
pixel[j]=c;
}
else if (command=='F')
{
scanf("%d %d %c",&x,&y,&c);
floodfill(pixel,x,y,c,m,n);
}
else if (command=='S')
{
scanf("%s", &filename);
printf("%s\n", filename);
for (i=1; i<=n; i++)
for (j=1; j<=m; j++)
{
printf("%c", pixel[j]);
if (j==m)
printf("\n");
}
}
else if (command=='X')
{
return 0;
}
}
}
void floodfill(char pixel[260][260],int x,int y,char c,int m, int n)
{
char f;
f=pixel[x][y];
pixel[x][y]=c;
if (x>=0 && x<=m)
if (y>=0 && y<=n)
if (f!=c)
{
if (f==pixel[x+1][y])
floodfill(pixel,x+1,y,c,m,n);
if (f==pixel[x][y+1])
floodfill(pixel,x,y+1,c,m,n);
if (f==pixel[x-1][y])
floodfill(pixel,x-1,y,c,m,n);
if (f==pixel[x][y-1])
floodfill(pixel,x,y-1,c,m,n);
}
} [/c]
bye!
-
- New poster
- Posts: 9
- Joined: Sat Nov 30, 2002 4:09 am
- Contact:
10267 Graphical Editor WA Why Please...
I'm getting wrong answer and i don't know why, i would appreciate some kind of help. Thank you.
[c]
#include <stdio.h>
#define MAX 10000 /* max depth of stack */
typedef struct {short y, xl, xr, dy;} Segment;
/*
* Filled horizontal segment of scanline y for xl<=x<=xr.
* Parent segment was on line y-dy. dy=1 or -1
*/
int colunas, linhas;
void initialize_table();
void color_pixel(int X, int Y, char COLOR);
void paint_vertical(int X, int Y1, int Y2, char COLOR);
void paint_horizontal(int X1, int X2, int Y, char COLOR);
void paint_rectangle(int X1, int Y1, int X2, int Y2, char COLOR);
void savePicture(char fileName[]);
void clear_table();
void fill(int x, int y, char nv);
void push(int Y, int XL, int XR, int DY, Segment* sp,Segment stack[]);
void pop(int* Y, int* XL, int* XR, int* DY, Segment* sp);
char table[251][251];
int main(){
int X, Y;
char COLOR;
int Y1, Y2;
int X1, X2;
char command[2];
char fileName[12];
for(; ; ){
scanf("%s", command);
if ((command[0] == 'I') && (command[1] == '\0'))
{
scanf("%i %i", &colunas, &linhas);
clear_table();
}
else if ((command[0] == 'C') && (command[1] == '\0'))
clear_table();
else if ((command[0] == 'L') && (command[1] == '\0'))
{
scanf ("%i %i %c", &X, &Y, &COLOR);
color_pixel(X-1, Y-1, COLOR);
}
else if ((command[0] == 'V') && (command[1] == '\0'))
{
scanf("%i %i %i %c", &X, &Y1, &Y2, &COLOR);
if (Y1 < Y2)
paint_vertical(X-1, Y1-1, Y2-1, COLOR);
else
paint_vertical(X-1, Y2-1, Y1-1, COLOR);
}
else if ((command[0] == 'H') && (command[1] == '\0'))
{
scanf ("%i %i %i %c", &X1, &X2, &Y, &COLOR);
if (X1 < X2)
paint_horizontal(X1-1, X2-1, Y-1, COLOR);
else
paint_horizontal(X2-1, X1-1, Y-1, COLOR);
}
else if ((command[0] == 'K') && (command[1] == '\0'))
{
scanf ("%i %i %i %i %c", &X1, &Y1, &X2, &Y2, &COLOR);
paint_rectangle(X1-1, Y1-1, X2-1, Y2-1, COLOR);
}
else if ((command[0] == 'F') && (command[1] == '\0'))
{
scanf("%i %i %c", &X, &Y, &COLOR);
fill(X-1, Y-1, COLOR);
}
else if ((command[0] == 'S') && (command[1] == '\0'))
{
scanf("%s", fileName);
savePicture(fileName);
}
else if ((command[0] == 'X') && (command[1] == '\0'))
break;
}
return 0;
}
void clear_table(){
int i, j;
for(i = 0; i < 251; i++)
for (j = 0; j < 251; j++)
table[j] = 'O';
}
void color_pixel(int X, int Y, char COLOR){
table[Y][X] = COLOR;
}
void paint_vertical(int X, int Y1, int Y2, char COLOR){
int i;
for (i = Y1; i <= Y2; i++)
table[X] = COLOR;
}
void paint_horizontal(int X1, int X2, int Y, char COLOR){
int i;
for (i = X1; i <= X2; i++)
table[Y] = COLOR;
}
void paint_rectangle(int X1, int Y1, int X2, int Y2, char COLOR){
int i;
for (i = Y1; i <= Y2; i++)
paint_horizontal(X1, X2, i, COLOR);
}
void savePicture(char fileName[]){
int i, j;
printf("%s\n", fileName);
for (i = 0; i < linhas; i++){
for (j = 0; j < colunas; j++)
printf("%c", table[j]);
printf("\n");
}
}
/*
* A Seed Fill Algorithm
* by Paul Heckbert
* from "Graphics Gems", Academic Press, 1990
*
* user provides pixelread() and pixelwrite() routines
*/
/*
* fill.c : simple seed fill program
* Calls pixelread() to read pixels, pixelwrite() to write pixels.
*
* Paul Heckbert 13 Sept 1982, 28 Jan 1987
*/
void fill(int x, int y, char nv)
{
int l, x1, x2, dy;
char ov = table[y][x]; /* old pixel value */
Segment stack[MAX], *sp = stack; /* stack of filled segments */
if ((ov == nv) || (x<0) || (x>colunas) || (y<0) || (y>linhas))
return;
push(y, x, x, 1, sp, stack); /* needed in some cases */
sp++;
push(y+1, x, x, -1, sp, stack); /* seed segment (popped 1st) */
sp++;
while (sp>stack) {
/* pop segment off stack and fill a neighboring scan line */
sp--;
pop(&y, &x1, &x2, &dy, sp);
/*
* segment of scan line y-dy for x1<=x<=x2 was previously filled,
* now explore adjacent pixels in scan line y
*/
for (x=x1; (x>=0) && (table[y][x]==ov); x--)
table[y][x] = nv;
if (x>=x1)
goto skip;
l = x+1;
if (l<x1) {
push(y, l, x1-1, -dy, sp, stack); /* leak on left? */
sp++;
}
x = x1+1;
do {
for (; (x<=colunas) && (table[y][x]==ov); x++)
table[y][x] = nv;
push(y, l, x-1, dy, sp, stack);
sp++;
if (x>x2+1) {
push(y, x2+1, x-1, -dy, sp, stack); /* leak on right? */
sp++;
}
skip: for (x++; (x<=x2) && (table[y][x]!=ov); x++);
l = x;
} while (x<=x2);
}
}
void push(int Y, int XL, int XR, int DY, Segment* sp, Segment* stack){
if ((sp<stack+MAX) && (Y+(DY)>=0) && (Y+(DY)<=linhas)) {
sp->y = Y;
sp->xl =XL;
sp->xr = XR;
sp->dy = DY;
}
}
void pop(int* Y, int* XL, int* XR, int* DY, Segment* sp){
*Y = sp->y+(*DY = sp->dy);
*XL = sp->xl;
*XR = sp->xr;
}
[/c][/code]
[c]
#include <stdio.h>
#define MAX 10000 /* max depth of stack */
typedef struct {short y, xl, xr, dy;} Segment;
/*
* Filled horizontal segment of scanline y for xl<=x<=xr.
* Parent segment was on line y-dy. dy=1 or -1
*/
int colunas, linhas;
void initialize_table();
void color_pixel(int X, int Y, char COLOR);
void paint_vertical(int X, int Y1, int Y2, char COLOR);
void paint_horizontal(int X1, int X2, int Y, char COLOR);
void paint_rectangle(int X1, int Y1, int X2, int Y2, char COLOR);
void savePicture(char fileName[]);
void clear_table();
void fill(int x, int y, char nv);
void push(int Y, int XL, int XR, int DY, Segment* sp,Segment stack[]);
void pop(int* Y, int* XL, int* XR, int* DY, Segment* sp);
char table[251][251];
int main(){
int X, Y;
char COLOR;
int Y1, Y2;
int X1, X2;
char command[2];
char fileName[12];
for(; ; ){
scanf("%s", command);
if ((command[0] == 'I') && (command[1] == '\0'))
{
scanf("%i %i", &colunas, &linhas);
clear_table();
}
else if ((command[0] == 'C') && (command[1] == '\0'))
clear_table();
else if ((command[0] == 'L') && (command[1] == '\0'))
{
scanf ("%i %i %c", &X, &Y, &COLOR);
color_pixel(X-1, Y-1, COLOR);
}
else if ((command[0] == 'V') && (command[1] == '\0'))
{
scanf("%i %i %i %c", &X, &Y1, &Y2, &COLOR);
if (Y1 < Y2)
paint_vertical(X-1, Y1-1, Y2-1, COLOR);
else
paint_vertical(X-1, Y2-1, Y1-1, COLOR);
}
else if ((command[0] == 'H') && (command[1] == '\0'))
{
scanf ("%i %i %i %c", &X1, &X2, &Y, &COLOR);
if (X1 < X2)
paint_horizontal(X1-1, X2-1, Y-1, COLOR);
else
paint_horizontal(X2-1, X1-1, Y-1, COLOR);
}
else if ((command[0] == 'K') && (command[1] == '\0'))
{
scanf ("%i %i %i %i %c", &X1, &Y1, &X2, &Y2, &COLOR);
paint_rectangle(X1-1, Y1-1, X2-1, Y2-1, COLOR);
}
else if ((command[0] == 'F') && (command[1] == '\0'))
{
scanf("%i %i %c", &X, &Y, &COLOR);
fill(X-1, Y-1, COLOR);
}
else if ((command[0] == 'S') && (command[1] == '\0'))
{
scanf("%s", fileName);
savePicture(fileName);
}
else if ((command[0] == 'X') && (command[1] == '\0'))
break;
}
return 0;
}
void clear_table(){
int i, j;
for(i = 0; i < 251; i++)
for (j = 0; j < 251; j++)
table[j] = 'O';
}
void color_pixel(int X, int Y, char COLOR){
table[Y][X] = COLOR;
}
void paint_vertical(int X, int Y1, int Y2, char COLOR){
int i;
for (i = Y1; i <= Y2; i++)
table[X] = COLOR;
}
void paint_horizontal(int X1, int X2, int Y, char COLOR){
int i;
for (i = X1; i <= X2; i++)
table[Y] = COLOR;
}
void paint_rectangle(int X1, int Y1, int X2, int Y2, char COLOR){
int i;
for (i = Y1; i <= Y2; i++)
paint_horizontal(X1, X2, i, COLOR);
}
void savePicture(char fileName[]){
int i, j;
printf("%s\n", fileName);
for (i = 0; i < linhas; i++){
for (j = 0; j < colunas; j++)
printf("%c", table[j]);
printf("\n");
}
}
/*
* A Seed Fill Algorithm
* by Paul Heckbert
* from "Graphics Gems", Academic Press, 1990
*
* user provides pixelread() and pixelwrite() routines
*/
/*
* fill.c : simple seed fill program
* Calls pixelread() to read pixels, pixelwrite() to write pixels.
*
* Paul Heckbert 13 Sept 1982, 28 Jan 1987
*/
void fill(int x, int y, char nv)
{
int l, x1, x2, dy;
char ov = table[y][x]; /* old pixel value */
Segment stack[MAX], *sp = stack; /* stack of filled segments */
if ((ov == nv) || (x<0) || (x>colunas) || (y<0) || (y>linhas))
return;
push(y, x, x, 1, sp, stack); /* needed in some cases */
sp++;
push(y+1, x, x, -1, sp, stack); /* seed segment (popped 1st) */
sp++;
while (sp>stack) {
/* pop segment off stack and fill a neighboring scan line */
sp--;
pop(&y, &x1, &x2, &dy, sp);
/*
* segment of scan line y-dy for x1<=x<=x2 was previously filled,
* now explore adjacent pixels in scan line y
*/
for (x=x1; (x>=0) && (table[y][x]==ov); x--)
table[y][x] = nv;
if (x>=x1)
goto skip;
l = x+1;
if (l<x1) {
push(y, l, x1-1, -dy, sp, stack); /* leak on left? */
sp++;
}
x = x1+1;
do {
for (; (x<=colunas) && (table[y][x]==ov); x++)
table[y][x] = nv;
push(y, l, x-1, dy, sp, stack);
sp++;
if (x>x2+1) {
push(y, x2+1, x-1, -dy, sp, stack); /* leak on right? */
sp++;
}
skip: for (x++; (x<=x2) && (table[y][x]!=ov); x++);
l = x;
} while (x<=x2);
}
}
void push(int Y, int XL, int XR, int DY, Segment* sp, Segment* stack){
if ((sp<stack+MAX) && (Y+(DY)>=0) && (Y+(DY)<=linhas)) {
sp->y = Y;
sp->xl =XL;
sp->xr = XR;
sp->dy = DY;
}
}
void pop(int* Y, int* XL, int* XR, int* DY, Segment* sp){
*Y = sp->y+(*DY = sp->dy);
*XL = sp->xl;
*XR = sp->xr;
}
[/c][/code]
-
- New poster
- Posts: 9
- Joined: Sat Nov 30, 2002 4:09 am
- Contact:
10267 Graphical Editor WA Why Please...
I'm getting wrong answer and i don't know why, i would appreciate some kind of help. Thank you.
[c]
#include <stdio.h>
#define MAX 10000 /* max depth of stack */
typedef struct {short y, xl, xr, dy;} Segment;
/*
* Filled horizontal segment of scanline y for xl<=x<=xr.
* Parent segment was on line y-dy. dy=1 or -1
*/
int colunas, linhas;
void initialize_table();
void color_pixel(int X, int Y, char COLOR);
void paint_vertical(int X, int Y1, int Y2, char COLOR);
void paint_horizontal(int X1, int X2, int Y, char COLOR);
void paint_rectangle(int X1, int Y1, int X2, int Y2, char COLOR);
void savePicture(char fileName[]);
void clear_table();
void fill(int x, int y, char nv);
void push(int Y, int XL, int XR, int DY, Segment* sp,Segment stack[]);
void pop(int* Y, int* XL, int* XR, int* DY, Segment* sp);
char table[251][251];
int main(){
int X, Y;
char COLOR;
int Y1, Y2;
int X1, X2;
char command[2];
char fileName[12];
for(; ; ){
scanf("%s", command);
if ((command[0] == 'I') && (command[1] == '\0'))
{
scanf("%i %i", &colunas, &linhas);
clear_table();
}
else if ((command[0] == 'C') && (command[1] == '\0'))
clear_table();
else if ((command[0] == 'L') && (command[1] == '\0'))
{
scanf ("%i %i %c", &X, &Y, &COLOR);
color_pixel(X-1, Y-1, COLOR);
}
else if ((command[0] == 'V') && (command[1] == '\0'))
{
scanf("%i %i %i %c", &X, &Y1, &Y2, &COLOR);
if (Y1 < Y2)
paint_vertical(X-1, Y1-1, Y2-1, COLOR);
else
paint_vertical(X-1, Y2-1, Y1-1, COLOR);
}
else if ((command[0] == 'H') && (command[1] == '\0'))
{
scanf ("%i %i %i %c", &X1, &X2, &Y, &COLOR);
if (X1 < X2)
paint_horizontal(X1-1, X2-1, Y-1, COLOR);
else
paint_horizontal(X2-1, X1-1, Y-1, COLOR);
}
else if ((command[0] == 'K') && (command[1] == '\0'))
{
scanf ("%i %i %i %i %c", &X1, &Y1, &X2, &Y2, &COLOR);
paint_rectangle(X1-1, Y1-1, X2-1, Y2-1, COLOR);
}
else if ((command[0] == 'F') && (command[1] == '\0'))
{
scanf("%i %i %c", &X, &Y, &COLOR);
fill(X-1, Y-1, COLOR);
}
else if ((command[0] == 'S') && (command[1] == '\0'))
{
scanf("%s", fileName);
savePicture(fileName);
}
else if ((command[0] == 'X') && (command[1] == '\0'))
break;
}
return 0;
}
void clear_table(){
int i, j;
for(i = 0; i < 251; i++)
for (j = 0; j < 251; j++)
table[j] = 'O';
}
void color_pixel(int X, int Y, char COLOR){
table[Y][X] = COLOR;
}
void paint_vertical(int X, int Y1, int Y2, char COLOR){
int i;
for (i = Y1; i <= Y2; i++)
table[X] = COLOR;
}
void paint_horizontal(int X1, int X2, int Y, char COLOR){
int i;
for (i = X1; i <= X2; i++)
table[Y] = COLOR;
}
void paint_rectangle(int X1, int Y1, int X2, int Y2, char COLOR){
int i;
for (i = Y1; i <= Y2; i++)
paint_horizontal(X1, X2, i, COLOR);
}
void savePicture(char fileName[]){
int i, j;
printf("%s\n", fileName);
for (i = 0; i < linhas; i++){
for (j = 0; j < colunas; j++)
printf("%c", table[j]);
printf("\n");
}
}
/*
* A Seed Fill Algorithm
* by Paul Heckbert
* from "Graphics Gems", Academic Press, 1990
*
* user provides pixelread() and pixelwrite() routines
*/
/*
* fill.c : simple seed fill program
* Calls pixelread() to read pixels, pixelwrite() to write pixels.
*
* Paul Heckbert 13 Sept 1982, 28 Jan 1987
*/
void fill(int x, int y, char nv)
{
int l, x1, x2, dy;
char ov = table[y][x]; /* old pixel value */
Segment stack[MAX], *sp = stack; /* stack of filled segments */
if ((ov == nv) || (x<0) || (x>colunas) || (y<0) || (y>linhas))
return;
push(y, x, x, 1, sp, stack); /* needed in some cases */
sp++;
push(y+1, x, x, -1, sp, stack); /* seed segment (popped 1st) */
sp++;
while (sp>stack) {
/* pop segment off stack and fill a neighboring scan line */
sp--;
pop(&y, &x1, &x2, &dy, sp);
/*
* segment of scan line y-dy for x1<=x<=x2 was previously filled,
* now explore adjacent pixels in scan line y
*/
for (x=x1; (x>=0) && (table[y][x]==ov); x--)
table[y][x] = nv;
if (x>=x1)
goto skip;
l = x+1;
if (l<x1) {
push(y, l, x1-1, -dy, sp, stack); /* leak on left? */
sp++;
}
x = x1+1;
do {
for (; (x<=colunas) && (table[y][x]==ov); x++)
table[y][x] = nv;
push(y, l, x-1, dy, sp, stack);
sp++;
if (x>x2+1) {
push(y, x2+1, x-1, -dy, sp, stack); /* leak on right? */
sp++;
}
skip: for (x++; (x<=x2) && (table[y][x]!=ov); x++);
l = x;
} while (x<=x2);
}
}
void push(int Y, int XL, int XR, int DY, Segment* sp, Segment* stack){
if ((sp<stack+MAX) && (Y+(DY)>=0) && (Y+(DY)<=linhas)) {
sp->y = Y;
sp->xl =XL;
sp->xr = XR;
sp->dy = DY;
}
}
void pop(int* Y, int* XL, int* XR, int* DY, Segment* sp){
*Y = sp->y+(*DY = sp->dy);
*XL = sp->xl;
*XR = sp->xr;
}
[/c][/code]
[c]
#include <stdio.h>
#define MAX 10000 /* max depth of stack */
typedef struct {short y, xl, xr, dy;} Segment;
/*
* Filled horizontal segment of scanline y for xl<=x<=xr.
* Parent segment was on line y-dy. dy=1 or -1
*/
int colunas, linhas;
void initialize_table();
void color_pixel(int X, int Y, char COLOR);
void paint_vertical(int X, int Y1, int Y2, char COLOR);
void paint_horizontal(int X1, int X2, int Y, char COLOR);
void paint_rectangle(int X1, int Y1, int X2, int Y2, char COLOR);
void savePicture(char fileName[]);
void clear_table();
void fill(int x, int y, char nv);
void push(int Y, int XL, int XR, int DY, Segment* sp,Segment stack[]);
void pop(int* Y, int* XL, int* XR, int* DY, Segment* sp);
char table[251][251];
int main(){
int X, Y;
char COLOR;
int Y1, Y2;
int X1, X2;
char command[2];
char fileName[12];
for(; ; ){
scanf("%s", command);
if ((command[0] == 'I') && (command[1] == '\0'))
{
scanf("%i %i", &colunas, &linhas);
clear_table();
}
else if ((command[0] == 'C') && (command[1] == '\0'))
clear_table();
else if ((command[0] == 'L') && (command[1] == '\0'))
{
scanf ("%i %i %c", &X, &Y, &COLOR);
color_pixel(X-1, Y-1, COLOR);
}
else if ((command[0] == 'V') && (command[1] == '\0'))
{
scanf("%i %i %i %c", &X, &Y1, &Y2, &COLOR);
if (Y1 < Y2)
paint_vertical(X-1, Y1-1, Y2-1, COLOR);
else
paint_vertical(X-1, Y2-1, Y1-1, COLOR);
}
else if ((command[0] == 'H') && (command[1] == '\0'))
{
scanf ("%i %i %i %c", &X1, &X2, &Y, &COLOR);
if (X1 < X2)
paint_horizontal(X1-1, X2-1, Y-1, COLOR);
else
paint_horizontal(X2-1, X1-1, Y-1, COLOR);
}
else if ((command[0] == 'K') && (command[1] == '\0'))
{
scanf ("%i %i %i %i %c", &X1, &Y1, &X2, &Y2, &COLOR);
paint_rectangle(X1-1, Y1-1, X2-1, Y2-1, COLOR);
}
else if ((command[0] == 'F') && (command[1] == '\0'))
{
scanf("%i %i %c", &X, &Y, &COLOR);
fill(X-1, Y-1, COLOR);
}
else if ((command[0] == 'S') && (command[1] == '\0'))
{
scanf("%s", fileName);
savePicture(fileName);
}
else if ((command[0] == 'X') && (command[1] == '\0'))
break;
}
return 0;
}
void clear_table(){
int i, j;
for(i = 0; i < 251; i++)
for (j = 0; j < 251; j++)
table[j] = 'O';
}
void color_pixel(int X, int Y, char COLOR){
table[Y][X] = COLOR;
}
void paint_vertical(int X, int Y1, int Y2, char COLOR){
int i;
for (i = Y1; i <= Y2; i++)
table[X] = COLOR;
}
void paint_horizontal(int X1, int X2, int Y, char COLOR){
int i;
for (i = X1; i <= X2; i++)
table[Y] = COLOR;
}
void paint_rectangle(int X1, int Y1, int X2, int Y2, char COLOR){
int i;
for (i = Y1; i <= Y2; i++)
paint_horizontal(X1, X2, i, COLOR);
}
void savePicture(char fileName[]){
int i, j;
printf("%s\n", fileName);
for (i = 0; i < linhas; i++){
for (j = 0; j < colunas; j++)
printf("%c", table[j]);
printf("\n");
}
}
/*
* A Seed Fill Algorithm
* by Paul Heckbert
* from "Graphics Gems", Academic Press, 1990
*
* user provides pixelread() and pixelwrite() routines
*/
/*
* fill.c : simple seed fill program
* Calls pixelread() to read pixels, pixelwrite() to write pixels.
*
* Paul Heckbert 13 Sept 1982, 28 Jan 1987
*/
void fill(int x, int y, char nv)
{
int l, x1, x2, dy;
char ov = table[y][x]; /* old pixel value */
Segment stack[MAX], *sp = stack; /* stack of filled segments */
if ((ov == nv) || (x<0) || (x>colunas) || (y<0) || (y>linhas))
return;
push(y, x, x, 1, sp, stack); /* needed in some cases */
sp++;
push(y+1, x, x, -1, sp, stack); /* seed segment (popped 1st) */
sp++;
while (sp>stack) {
/* pop segment off stack and fill a neighboring scan line */
sp--;
pop(&y, &x1, &x2, &dy, sp);
/*
* segment of scan line y-dy for x1<=x<=x2 was previously filled,
* now explore adjacent pixels in scan line y
*/
for (x=x1; (x>=0) && (table[y][x]==ov); x--)
table[y][x] = nv;
if (x>=x1)
goto skip;
l = x+1;
if (l<x1) {
push(y, l, x1-1, -dy, sp, stack); /* leak on left? */
sp++;
}
x = x1+1;
do {
for (; (x<=colunas) && (table[y][x]==ov); x++)
table[y][x] = nv;
push(y, l, x-1, dy, sp, stack);
sp++;
if (x>x2+1) {
push(y, x2+1, x-1, -dy, sp, stack); /* leak on right? */
sp++;
}
skip: for (x++; (x<=x2) && (table[y][x]!=ov); x++);
l = x;
} while (x<=x2);
}
}
void push(int Y, int XL, int XR, int DY, Segment* sp, Segment* stack){
if ((sp<stack+MAX) && (Y+(DY)>=0) && (Y+(DY)<=linhas)) {
sp->y = Y;
sp->xl =XL;
sp->xr = XR;
sp->dy = DY;
}
}
void pop(int* Y, int* XL, int* XR, int* DY, Segment* sp){
*Y = sp->y+(*DY = sp->dy);
*XL = sp->xl;
*XR = sp->xr;
}
[/c][/code]