10267 - Graphical Editor

All about problems in Volume 102. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10267 - Graphical Editor

Post by brianfry713 » Tue Sep 17, 2013 12:25 am

Input:

Code: Select all

I 10 1
S a.bmp
H 2 6 1 M
S b.bmp
V 2 1 1 G
S c.bmp
F 4 1 P
S d.bmp
K 5 1 6 1 D
S e.bmp
F 8 1 W
S f.bmp
H 9 4 1 G
S g.bmp
L 3 1 J
S h.bmp
X
AC output:

Code: Select all

a.bmp
OOOOOOOOOO
b.bmp
OMMMMMOOOO
c.bmp
OGMMMMOOOO
d.bmp
OGPPPPOOOO
e.bmp
OGPPDDOOOO
f.bmp
OGPPDDWWWW
g.bmp
OGPGGGGGGW
h.bmp
OGJGGGGGGW
Check input and AC output for thousands of problems on uDebug!

kevintomlee
New poster
Posts: 1
Joined: Thu Sep 26, 2013 7:53 am

Re: 10267 - Graphical Editor

Post by kevintomlee » Thu Sep 26, 2013 7:56 am

Hi there.
I have passed all the test cases in the forum and still got wrong answers.
I don't know where to go from here....
Can anyong please take a look at my code please?
Thank you.

Code: Select all

/*
	new GE
 */
 
 
#include <stdio.h>
#include <stdlib.h>

void create(int mCol, int nRow, char*** matrix);
void clear(int mCol, int nRow, char** matrix);
void colorXY(int x, int y, char c, char** matrix);
void fill(int mCol, int nRow, int x, int y, char** matrix, char oldc, char newc);
void printMatrix(int mCol, int nRow, char** matrix);
void swap(int *a, int *b);

int main() {
	
	char** matrix;
	size_t size;
	int mCol = 0;
	int nRow = 0;
	int quit = 0;
	char* command = NULL;
	char ch;
	
	while (!quit && getline(&command, &size, stdin)!=EOF) {
		switch(command[0]) {
		case 'I' : {
			sscanf(command, "%c %d %d", &ch, &mCol, &nRow);
			create(mCol, nRow, &matrix);
			clear(mCol, nRow, matrix);
			break;
		}
		case 'C' : {
			clear(mCol, nRow, matrix);
			break;
		}
		case 'L' : {
			int x, y;
			char c;
			sscanf(command, "%c %d %d %c", &ch, &x, &y, &c);
			colorXY(x, y, c, matrix);
			break;
		}
		case 'V' : {
			int x, y1, y2;
			char c;
			sscanf(command, "%c %d %d %d %c", &ch, &x, &y1, &y2, &c);
			if (y1>y2) swap(&y1, &y2);
			int i;
			for (i=y1;i<=y2;i++) {
				colorXY(x, i, c, matrix);
			}
			break;
		}
		case 'H' : {
			int x1, x2, y;
			char c;
			sscanf(command, "%c %d %d %d %c", &ch, &x1, &x2, &y, &c);
			if (x1>x2) swap(&x1, &x2);
			int i;
			for (i=x1;i<=x2;i++) {
				colorXY(i, y, c, matrix);
			}
			break;
		}
		case 'K' : {
			int x1, x2, y1, y2;
			char c;
			sscanf(command, "%c %d %d %d %d %c", &ch, &x1, &y1, &x2, &y2, &c);
			if (x1>x2) swap(&x1, &x2);
			if (y1>y2) swap(&y1, &y2);
			int i, j;
			for (i=x1;i<=x2;i++) {
				for (j=y1;j<=y2;j++) {
					colorXY(i, j, c, matrix);
				}
			}
			break;
		}
		case 'F' : {
			int x, y;
			char c;
			sscanf(command, "%c %d %d %c", &ch, &x, &y, &c);
			fill(mCol, nRow, x, y, matrix, matrix[x-1][y-1], c);
			break;
		}
		case 'S' : {
			char name[20];
			sscanf(command, "%c %s", &ch, name);
			printf("%s\n", name);
			printMatrix(mCol, nRow, matrix);
			break;
		}
		}
	}
	return 0;
}

void create(int mCol, int nRow, char*** matrix) {
	*matrix = (char **)malloc(mCol * sizeof(char *));
	int i;
	for (i=0;i<mCol;i++) {
		(*matrix)[i] = (char *)malloc(nRow * sizeof(char));
	}
}

void clear(int mCol, int nRow, char** matrix) {
	int i,j;
	for (i=0;i<mCol;i++) {
		for (j=0;j<nRow;j++) {
			matrix[i][j] = 'O';
		}
	}
}

void colorXY(int x, int y, char c, char** matrix) {
	matrix[x-1][y-1] = c;
}

void fill(int mCol, int nRow, int x, int y, char** matrix, char oldc, char newc) {
	if (oldc==newc) return;
	if (x<1 || x>mCol || y<1 || y>nRow) return;
	
	colorXY(x, y, newc, matrix);
	/* share up side */
	if (y>1 && matrix[x-1][y-2]==oldc) {
		//colorXY(x, y, newc, matrix);
		fill(mCol, nRow, x, y-1, matrix, oldc, newc);
	}
	/* share bottom side */
	if (y<nRow && matrix[x-1][y]==oldc) {
		//colorXY(x, y, newc, matrix);
		fill(mCol, nRow, x, y+1, matrix, oldc, newc);
	}
	/* share left side */
	if (x>1 && matrix[x-2][y-1]==oldc) {
		//colorXY(x, y, newc, matrix);
		fill(mCol, nRow, x-1, y, matrix, oldc, newc);
	}
	/* share right side */
	if (x<mCol && matrix[x][y-1]==oldc) {
		//colorXY(x, y, newc, matrix);
		fill(mCol, nRow, x+1, y, matrix, oldc, newc);
	}
}

void printMatrix(int mCol, int nRow, char** matrix) {
	int i,j;
	for (i=0;i<nRow;i++) {
		for (j=0;j<mCol;j++) {
			printf("%c", matrix[j][i]);
		}
		printf("\n");
	}
}

void swap(int *a, int *b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10267 - Graphical Editor

Post by brianfry713 » Fri Sep 27, 2013 11:57 pm

X Terminates the session.
Check input and AC output for thousands of problems on uDebug!

TryCatchMe
New poster
Posts: 15
Joined: Fri May 30, 2014 12:09 am

Re: 10267 - Graphical Editor

Post by TryCatchMe » Fri Jun 20, 2014 11:51 am

brianfry,
Is it too much to tell you that I love you, lol? You are extremely helpful for so many of these problems. Anyways, I got 3 WA when I tried to submit the problem. Originally, read the input char by char using getchar() in the C standard library, assuming that there was one space between fields and no trailing whitespace. Once I created a more dynamic line read function that could handle any amount of whitespace in between and trailing each line I got AC. Also, to those who say you can't get AC with recursive floodfill, you are not correct. Like others have mentioned, you must check if the new color you want to fill is the same as the old color. Of course, if you don't do this check you will get infinite recursion => RTE. Here are some notes I would pass along to someone who was irritated with this problem:

1) don't assume I/O will be in the exact format as in the description ( i.e. extra whitespace between commands, etc... ) . Of course, this is an assumption that should be made for every UVA problem.
2) you don't need an array larger than size 12 for the filename
3) when using recursive flood fill, check if color you want to fill equals color you already have
4) be careful with how "awkward" the I/O is... meaning I found it somewhat un-natural to have the columns listed before the rows and with the format of some of the commands.
5) All judge data is valid. ( if you're getting WA it is not because of the judge data being wrong )
6) Like others have mentioned, in commands H, V and K the judge data is NOT necessarily ordered. Ex: V X Y1 Y2 C does NOT imply that Y1 < Y2.
Good luck everyone!

alcaide_redb
New poster
Posts: 3
Joined: Fri Feb 22, 2013 2:04 pm

Re: 10267 - Graphical Editor

Post by alcaide_redb » Wed Jul 02, 2014 4:53 am

Code: Select all

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;

int length,height;
char image[251][251];

void floodFill(int x,int y,char oldcol, char repcol)
{
	if((x < 1) ||( x > length)) return;
	if((y < 1) || ( y > height)) return;
	if(image[y][x]==repcol) return;

	
	if(image[y][x] == oldcol)
	{
		image[y][x] = repcol;
		floodFill(x+1,y,oldcol,repcol);
		floodFill(x-1,y,oldcol,repcol);
		floodFill(x,y+1,oldcol,repcol);
		floodFill(x,y-1,oldcol,repcol);
	}

}


int main()
{
	char choice;

	while(cin>>choice)
	{	
		int x,y,x1,x2,y1,y2,m,n;
		x=y=x1=x2=y1=y2=m=n=0;
		char color;
		string name;
		if(choice=='I')
		{
			cin>>m>>n;
		
			length = m;	
			height = n;	
			for(int i=1;i<=height;i++){
				for(int j=1;j<=length;j++)
				image[i][j]='0';
			}
		}	
		else if(choice=='C')
		{
			for(int i=1;i<=height;i++)
				for(int j=1;j<=length;j++)
				image[i][j]='0';
		}
		else if(choice == 'L')
		{
			cin>>x>>y>>color;
			image[y][x] = color;
		}
		else if(choice == 'V')
		{	
			cin>>x>>y1>>y2>>color;
			if(y1>y2) swap(y1,y2);

			for(int i=y1;i<=y2;i++)
			image[i][x] = color;
		}
		else if(choice =='H')
		{
			cin>>x1>>x2>>y>>color;
			if(x1>x2) swap(x1,x2);
			for(int i=x1;i<=x2;i++)
			image[y][i] = color;	
		}
		else if(choice =='K')
		{
			cin>>x1>>y1>>x2>>y2>>color;
			if(x1>x2) swap(x1,x2);
			if(y1>y2) swap(y1,y2);
			for(int i=y1;i<=y2;i++){
				for(int j=x1;j<=x2;j++)
				image[i][j]=color;
			}
		}
		else if(choice=='S')
		{
			cin>>name;
			cout<<name<<endl;
			for(int i=1;i<=height;i++){
				for(int j=1;j<=length;j++)
				cout<<image[i][j];
			cout<<endl;
			}
		}


		else if(choice == 'F')
		{
			cin>>x>>y>>color;
			char oldcol = image[y][x];
			char repcol = color;
			floodFill(x,y,oldcol,repcol);
		}
		else if(choice=='X')
			break;
		else
			cin.ignore(10000000,'\n');




	}
	return 0;
}	
I don't know what's wrong with my code....it seems that I've covered all cases.I don't know where to go on from here.

kier.guevara
New poster
Posts: 30
Joined: Thu Jul 19, 2012 11:24 pm

Re: 10267 - Graphical Editor

Post by kier.guevara » Thu Jul 03, 2014 3:40 pm

@alcaideredb

Try changing the number '0' to letter 'O' when creating a new image and clearing an image .

alcaide_redb
New poster
Posts: 3
Joined: Fri Feb 22, 2013 2:04 pm

Re: 10267 - Graphical Editor

Post by alcaide_redb » Thu Jul 03, 2014 6:01 pm

Thanks @kier.guevara XD

fresher96
New poster
Posts: 25
Joined: Wed Sep 03, 2014 8:50 am

Re: 10267 - Graphical Editor

Post by fresher96 » Wed Sep 03, 2014 9:00 am

heyyy !!!
any one please can help i'm getting a runtime error and i have no idea why
i don't think i clearly understood the errors !
and i am really so depressed :cry:
any help with my code in c++



///////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

char **img;
int n,m;

void swap(int &a , int &b)
{
int t = a;
a = b;
b = t;
}

void check(int x, int y)
{
if (x < 1 || x > m || y < 1 || y > n)
exit(0);
}

void I()
{
img = new char *[n+1];
for(int i = 1; i<=n; i++)
img = new char [m+1];
for(int i = 1; i<=n; i++)
for(int j = 1; j<=m; j++)
img[j]='O';
}

void C()
{
for(int i = 1; i<=n; i++)
for(int j = 1; j<=m; j++)
img[j]='O';
}

void L(int x, int y, char c)
{
img[y][x] = c;
}

void V(int x,int y1,int y2,char c)
{
if (y1 > y2)
swap(y1 , y2);
for(int i = y1; i<= y2; i++)
{
img[x] = c;
}
}

void H(int y, int x1,int x2,char c)
{
if (x1 > x2)
swap(x1,x2);
for(int i = x1; i<=x2; i++)
{
img[y] = c;
}
}

void K(int x1,int x2,int y1, int y2,char c)
{
if (y1 > y2)
swap(y1 , y2);
if(x1 > x2)
swap(x1 , x2);
for(int i = y1; i<=y2; i++)
{
for(int j = x1; j <= x2; j++)
{
img[j] = c;
}
}
}

void S(string name)
{
cout<<name<<endl;
for(int i = 1; i<=n; i++)
{
for(int j = 1; j<=m; j++)
cout<<img[j];
cout<<endl;
}
}

void F(int x,int y,char c)
{
char colour = img[y][x];
img[y][x] = c;

if (y-1 >= 1 && img[y-1][x] == colour )
{
F(x,y-1,c);
}
if (x-1 >= 1 && img[y][x-1] == colour)
{
F(x-1,y,c);
}
if (y+1 <= n && img[y+1][x] == colour)
{
F(x,y+1,c);
}
if (x+1 <= m && img[y][x+1] == colour )
{
F(x+1,y,c);
}
if (x-1 >= 1 && y-1 >= 1 && img[y-1][x-1] == colour)
{
F(x-1,y-1,c);
}
if (x+1 <= m && y+1 <= n && img[y+1][x+1] == colour)
{
F(x+1,y+1,c);
}
if (y+1 <= n && x-1 >= 1 && img[y+1][x-1] == colour)
{
F(x-1,y+1,c);
}
if (y-1 >= 1 && x+1 <= m && img[y-1][x+1] == colour)
{
F(x+1,y-1,c);
}
}



int main()
{
char command;
int x,y;
int x1,x2,y1,y2;
char c;
string name;
m = n = 10;
I();
while(cin>>command , command != 'X')
{
switch(command)
{
case 'I':
cin>>m>>n;
I();
break;

case 'C':
C();
break;

case 'L':
cin>>x>>y>>c;
check(x , y);
L(x,y,c);
break;

case 'V':
char c;
cin>>x>>y1>>y2>>c;
check(x , y1);
check(x , y2);
V(x,y1,y2,c);
break;

case 'H':
cin>>x1>>x2>>y>>c;
check(x1 , y);
check(x2 , y);
H(y,x1,x2,c);
break;

case 'K':
cin>>x1>>y1>>x2>>y2>>c;
check(x1 , y1);
check(x2 , y2);
K(x1,x2,y1,y2,c);
break;

case 'F':
cin>>x>>y>>c;
check(x , y);
F(x,y,c);
break;

case 'S':
cin>>name;
S(name);
break;

default:
getline(cin , name , '\n');
break;
}
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
thanks in advanced

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 10267 - Graphical Editor

Post by lighted » Wed Sep 03, 2014 6:40 pm

Code: Select all

Use code tags
Try to read posts in thread before posting. There are so many people who said about this bug.
titid_gede wrote:i think your got RTE with your old fillregion function, since you dont check if old color == new color. it cause stack overflow. you can check first, if old color != new color then call function fillregion.
Input

Code: Select all

I 5 6
L 2 3 A
S one.bmp
G 2 3 J
F 3 3 J
F 3 3 J
V 2 3 4 W
H 3 4 2 Z
S two.bmp
X
Acc Output

Code: Select all

one.bmp
OOOOO
OOOOO
OAOOO
OOOOO
OOOOO
OOOOO
two.bmp
JJJJJ
JJZZJ
JWJJJ
JWJJJ
JJJJJ
JJJJJ
Your Ouput

Code: Select all

one.bmp
OOOOO
OOOOO
OAOOO
OOOOO
OOOOO
OOOOO
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

fresher96
New poster
Posts: 25
Joined: Wed Sep 03, 2014 8:50 am

Re: 10267 - Graphical Editor

Post by fresher96 » Thu Sep 04, 2014 9:11 am

I'm sorry it's my first time here ... my bad
thanks a lot for your time :D
now i can die in peace :-?
thank you again

xiexinxinlove
New poster
Posts: 6
Joined: Thu Jul 10, 2014 2:19 pm

Re: 10267 - Graphical Editor

Post by xiexinxinlove » Sat Dec 06, 2014 8:54 pm

Thanks for your advices!
I get WA 8 times :cry: , and now I know the reason. :D

Post Reply

Return to “Volume 102 (10200-10299)”