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

trelos
New poster
Posts: 3
Joined: Thu Apr 16, 2009 12:25 am

Re: 10267 - Graphical Editor

Post by trelos » Thu Apr 16, 2009 9:40 pm

vahid sanei wrote:

Code: Select all

void K(int X1,int Y1, int X2, int Y2,char C){
  int i;

  for(i=X1;i<X2;i++){V(i,Y1,Y2,C);}
}
you should swap (x1 and x2) and( y1 and y2) here
Yes!! That certainly was a bad mistake... thank you for that! :D
vahid sanei wrote:i found another problem in your code
i think this function

Code: Select all

void F(int X, int Y, char C) {
  char oc;
  oc = B[Y][X];
  L(X,Y,C);
  if(C==oc){return;} // same colour
  if(B[Y+1][X]==oc){ F(X,Y+1,C); }
  if(B[Y][X-1]==oc){ F(X-1,Y,C); }
  if(B[Y][X+1]==oc){ F(X+1,Y,C); }
  if(B[Y-1][X]==oc){ F(X,Y-1,C); }
}
should check X -- > is >= 1 and <=m
and
Y is >=1 and <= n
I believe this is not the case. If you take a look at the way I initialize the bitmap (B) I make sure it has two more rows and two more columns. I do that so as to have the margins initialized with '\0', a character I presume I will never receive as colour input. So every time the fill command checks the B[a] == oc condition, it will always find it to be false since no colour can be equal to '\0'. Think of the '\0's as a protective wall around the bitmap.

You can find it here:

Code: Select all

void I(M,N){
  int i,j;

  c = M; r = N;
  B = (char**)malloc((N+2)*sizeof(char *));
  for(i=0;i<N+2;i++){ B[i] = (char *)malloc((M+2)*sizeof(char));} 
  for(i=0;i<N+2;i++){
    for(j=0;j<M+2;j++){
      if(i==0||i==N+1||j==0||j==M+1){B[i][j] = '\0';}
      else{ B[i][j] = '0'; }
    }
  }
}
I really can't think of a case where such a measure would be mistaken. Maybe you can...

Again thanks for the answer, but still I get that terrible RE... :(
For a ship without a destination, no wind is favorable...

paaulocezar
New poster
Posts: 5
Joined: Thu May 14, 2009 4:14 pm

Re: 10267 - Graphical Editor

Post by paaulocezar » Thu May 28, 2009 7:00 pm

well guys, after take a look in some previous posts I could fix the RTE I was receiving,
but now WA is chasing me.
Maybe somebody could find what I'm doing wrong.

Code: Select all

#include <stdio.h>
#include <stdlib.h>

void newTable( char x[][252] ){

int i, j;

for(i = 0; i<252; i++){
	for( j = 0; j<252; j++ ){
		if( i==0 || i==251 || j == 0 || j==251)
			x[i][j] = '1';
		else
			x[i][j] = 'O';
	}
}

}

void prntTable( char x[][252], int w, int h ){

int i, j;

for(i = 1; i <= h; i++){
	for( j = 1; j <= w; j++ )
		printf("%c", x[i][j] );
	printf("\n");
}

}

void pColor( char t[][252], int x, int y, char c ){

	t[y][x] = c ;

}

void vColor( char t[][252], int x, int ya, int yb, char c ){

 int i;

	if( yb < ya )
		yb ^= ya ^= yb ^= ya;

	for( i = ya; i <= yb; i++ )
		t[i][x] = c;

}

void hColor( char t[][252], int xa, int xb, int y, char c ){

	if( xb < xa )
		xb ^= xa ^= xb ^= xa;

 int i;

	for( i = xa; i <= xb; i++ )
		t[y][i] = c;

}

void kColor( char t[][252], int xa, int ya, int xb, int yb, char c ){

 int i, j;

	if( xb < xa )
		xb ^= xa ^= xb ^= xa;

	if( yb < ya )
		yb ^= ya ^= yb ^= ya;

  for(i = ya; i <= yb; i++)
	for( j = xa ; j <= xb; j++ )
		t[i][j] = c ;

}

void fColor( char t[][252], int x, int y, char c ){

  char oldColor = t[y][x];

  if( oldColor == c )
	return;

  if( t[y][x] == '1' )
	return;

  t[y][x] = c;

  if( t[y-1][x] == oldColor )		fColor( t, x, y-1, c );
  if( t[y][x-1] == oldColor )		fColor( t, x-1, y, c );
  if( t[y][x+1] == oldColor )		fColor( t, x+1, y, c );
  if( t[y+1][x] == oldColor )		fColor( t, x, y+1, c );

}

int main(){

  char cmd, param;
  char name[99];
  char table[252][252];
  int widht, height;
  int x, y, z, k;

	do{
	scanf("%c", &cmd);
	switch( cmd ){

	  case 'I':
		scanf("%d %d", &widht, &height );
		newTable( table );
		break;
	  case 'C':
		newTable( table );
		break;
	  case 'L':
		scanf("%d %d %c", &x, &y, &param);
		pColor( table, x, y, param );
		break;
	  case 'V':
		scanf("%d %d %d %c", &x, &y, &z, &param);
		vColor( table , x, y, z, param );
		break;
	  case 'H':
		scanf("%d %d %d %c", &x, &y, &z, &param);
		hColor( table , x, y, z, param );
		break;
	  case 'K':
		scanf("%d %d %d %d %c", &x, &y, &z, &k, &param);
		kColor( table, x, y, z, k, param );
		break;
	  case 'F':
		scanf("%d %d %c", &x, &y, &param);
		fColor( table ,  x, y, param );
		break;
	  case 'S':
		scanf("%s", &name);
		printf("%s\n", name );
		prntTable( table, widht, height );
		break;
	  case 'X':
		return 0;
		break;
	  default:
		break;

	}
	scanf("%c", &cmd);


	} while ( 1 == 1 );



 return 0;
}

panda_coder
New poster
Posts: 7
Joined: Mon Jun 08, 2009 4:02 am

Re: 10267 - Graphical Editor

Post by panda_coder » Tue Jun 16, 2009 10:03 pm

I checked all of the posts here, but I couldn't find any problem in my program.
I used a recursive method, and I checked the case that X1 > X2, Y1>Y2 (the description says that (x1, Y1) is the upper-left and (X2, Y2) the lower right corner, though),
I tried several test data posted here, and it seemed to work fine for me.

Could you please give me any idea to solve it out? ='(
Here's my code..

Code: Select all

import java.io.*;
import java.util.*;

public class GraphicalEditor implements Runnable {
	
	public static boolean[][] visited = null;
	public static char[][] canvasCopy = null;
	public static char[][] canvas = null;
	public static int width, height;
	public static char color, regionDefineColor;
	
	static String readLine(int maxLength) {
		byte line[] = new byte[maxLength];
		int length = 0;
		int input = -1;
		try {
			while(length < maxLength) {
				input = System.in.read();
				if((input < 0) || (input == '\n')) break;
				line[length++] += input;
			}
			if((input<0) && (length ==0)) return null;
			return new String(line, 0, length);
		}catch(IOException e) {
			return null;
		}
	}
	
	public static void main(String[] args) {
		GraphicalEditor editor = new GraphicalEditor();
		editor.run();
	}
	
	public void run() {
		String line;
		boolean[][] checkCanvas = null;
		boolean terminate = false;
		while((line = readLine(100))!=null) {
			StringTokenizer st = new StringTokenizer(line);
			char com = st.nextToken().charAt(0); /// command
			if(com == 'X') break;
			
			switch(com) {
				case 'I' : {
					width = Integer.parseInt(st.nextToken());
					height = Integer.parseInt(st.nextToken());
					canvas = new char[height][width];
					checkCanvas = new boolean[height][width];
					clearTable(canvas); /// ??? ????
					break;
				}
				
				case 'C' : {   // clear
					clearTable(canvas);
					break;
				}
				
				case 'L' : {
					int x = Integer.parseInt(st.nextToken());
					int y = Integer.parseInt(st.nextToken());
					color = st.nextToken().charAt(0);
					canvas[y-1][x-1] = color;  /// unlike an array, (width, height)
					break;
				}
				
				case 'V' : {
					int x = Integer.parseInt(st.nextToken());
					int y1 = Integer.parseInt(st.nextToken());
					int y2 = Integer.parseInt(st.nextToken());
					color = st.nextToken().charAt(0);
					if(y2 < y1) {
						int temp = y2;
						y2 = y1;
						y1 = temp;
					}
					for(int i=y1-1;i<=y2-1;i++) {
						canvas[i][x-1] = color;
					}
					break;
				}
				
				case 'H' : {
					int x1 = Integer.parseInt(st.nextToken());
					int x2 = Integer.parseInt(st.nextToken());
					int y = Integer.parseInt(st.nextToken());
					color = st.nextToken().charAt(0);
					if(x2 < x1) {
						int temp = x2;
						x2 = x1;
						x1 = temp;
					}
					for(int i=x1-1;i<=x2-1;i++) {
						canvas[y-1][i] = color;
					}
					break;
				}
				
				case 'K' : {  /// fill the rectangle 
					int x1 = Integer.parseInt(st.nextToken());
					int y1 = Integer.parseInt(st.nextToken());
					int x2 = Integer.parseInt(st.nextToken());
					int y2 = Integer.parseInt(st.nextToken());
					color = st.nextToken().charAt(0);
						
					if(x2 < x1) {
						int temp = x2;
						x2 = x1;
						x1 = temp;
					}
					
					if(y2 < y1) {
						int temp = y2;
						y2 = y1;
						y1 = temp;
					}
					
					for(int i=y1-1;i<=y2-1;i++) {
						for(int j=x1-1; j<=x2-1; j++) {
							canvas[i][j] = color;
						}
					}
					
					break;
				}
				
				case 'F' : {
					int x = Integer.parseInt(st.nextToken());
					int y = Integer.parseInt(st.nextToken());
					color = st.nextToken().charAt(0);  // color to be painted
			
					regionDefineColor = canvas[y-1][x-1];
					visited = new boolean[height][width];
					clearVisited(visited);
					canvasCopy = canvas;
					fillRegion(y-1,x-1);
					break;
				
				}
				
				case 'S' : { /// save
					String fileName = st.nextToken();
					System.out.println(fileName);
					for(int i=0; i<height; i++) {
						for(int j=0; j<width; j++)
							System.out.print(canvas[i][j]);
						System.out.println();
					}
					break;
				}
				
				case 'X' : terminate = true;
				default : break;
			}
			
			if(terminate) break;
			
			}
	}				


	public void clearTable(char[][] table) {
		for(int i=0;i<height;i++) {
			for(int k=0;k<width;k++) {
				table[i][k] = 'O';
			}
		}
	}
	
	public void clearVisited(boolean[][] tableCopy) {
		for(int i=0;i<height;i++) {
			for(int k=0;k<width;k++) {
				tableCopy[i][k] = false;
			}
		}
	}
	public void fillRegion(int x, int y) {
		
		if(visited[x][y]) return;
		else {
			visited[x][y] = true;
			canvas[x][y] = this.color;
			if(x-1>=0) {
				if(canvasCopy[x-1][y] == regionDefineColor) 
						fillRegion(x-1,y);
			}
			if(y-1>=0) {
				if(canvasCopy[x][y-1] == regionDefineColor) 
						fillRegion(x,y-1);
			}
			
			if(x+1<height) {
				if(canvasCopy[x+1][y] == regionDefineColor)
						fillRegion(x+1,y);
			}
			
			if(y+1<width) {
				if(canvasCopy[x][y+1] == regionDefineColor)
						fillRegion(x,y+1);
			}
		}
					
			
		}
	}




chili5
New poster
Posts: 1
Joined: Wed Jul 01, 2009 6:27 pm

Re: 10267 - Graphical Editor

Post by chili5 » Wed Jul 01, 2009 6:51 pm

panda_coder wrote: I used a recursive method, and I checked the case that X1 > X2, Y1>Y2 (the description says that (x1, Y1) is the upper-left and (X2, Y2) the lower right corner, though),
Just because it says (x1, y1) is the upper-left and (x2, y2) is the lower-right does not mean that x1 < x2, and y1 < y2. The lower-right could be in the upper left and the upper left could be in the lower right.

I haven't gotten this to work yet, it is driving me absolutely crazy but I'll figure out. However, since the specifications do not state that the first command is create a new image, what happens then? This question is a lot of BS.

For this command: L X Y C the pixel (X,Y) is colored in color C. However is the X and Y defined as row and column or x and y coordinates? It does make a difference. Another thing is the color defined to be exactly one letter? The specs do not say that it is one letter and thus would throw everything off if the color had a length of say 2.

From what I've seen people have defined a 2D char array as char[][] c = new char[251][251]. Why? The specs state that the column number is an integer between 1 and M such that M >= 1. We have an upper bounds on the number of rows at 250 but there is no upper bounds on the number of columns.

There is only one command per line, so that is not a problem. Now the way I see it, with these commands: V, H, K and F the entire shape of region may not be in the image. So before you fill in part of the shape, you have to check if it is the array or not.

x140l31
Learning poster
Posts: 69
Joined: Tue Jan 30, 2007 12:51 am

Re: 10267 - Graphical Editor

Post by x140l31 » Wed Jul 01, 2009 7:31 pm

chili5 wrote: Just because it says (x1, y1) is the upper-left and (x2, y2) is the lower-right does not mean that x1 < x2, and y1 < y2. The lower-right could be in the upper left and the upper left could be in the lower right.
In my AC code, I always swap X1, X2 (if X1 > X2), Y1, Y2 (if Y1 > Y2) in cases V, H and K.
chili5 wrote: I haven't gotten this to work yet, it is driving me absolutely crazy but I'll figure out. However, since the specifications do not state that the first command is create a new image, what happens then? This question is a lot of BS.
I think that all judge inputs has always an initialization. In my code I've always a map with the maximum size (250x250), so it will not say any "segmentation fault". Also the problem statement says "In case of other errors the program behaviour is unpredictable."
chili5 wrote: For this command: L X Y C the pixel (X,Y) is colored in color C. However is the X and Y defined as row and column or x and y coordinates? It does make a difference. Another thing is the color defined to be exactly one letter? The specs do not say that it is one letter and thus would throw everything off if the color had a length of say 2.
L X Y C, means the point at row Y and column X. See at example (L 2 3 A).
In my AC code, I suposse that the color is always one letter (one char).
chili5 wrote: From what I've seen people have defined a 2D char array as char[][] c = new char[251][251]. Why? The specs state that the column number is an integer between 1 and M such that M >= 1. We have an upper bounds on the number of rows at 250 but there is no upper bounds on the number of columns.
M×N (1<=M,N<=250)
chili5 wrote: There is only one command per line, so that is not a problem. Now the way I see it, with these commands: V, H, K and F the entire shape of region may not be in the image. So before you fill in part of the shape, you have to check if it is the array or not.
I didn't check it in my AC code

x140l31
Learning poster
Posts: 69
Joined: Tue Jan 30, 2007 12:51 am

Re: 10267 - Graphical Editor

Post by x140l31 » Wed Jul 01, 2009 7:40 pm

paaulocezar wrote:well guys, after take a look in some previous posts I could fix the RTE I was receiving,
but now WA is chasing me.
Maybe somebody could find what I'm doing wrong.

Code: Select all

#include <stdio.h>
#include <stdlib.h>

void newTable( char x[][252] ){

int i, j;

for(i = 0; i<252; i++){
	for( j = 0; j<252; j++ ){
		if( i==0 || i==251 || j == 0 || j==251)
			x[i][j] = '1';
		else
			x[i][j] = 'O';
	}
}

}

void prntTable( char x[][252], int w, int h ){

int i, j;

for(i = 1; i <= h; i++){
	for( j = 1; j <= w; j++ )
		printf("%c", x[i][j] );
	printf("\n");
}

}

void pColor( char t[][252], int x, int y, char c ){

	t[y][x] = c ;

}

void vColor( char t[][252], int x, int ya, int yb, char c ){

 int i;

	if( yb < ya )
		yb ^= ya ^= yb ^= ya;

	for( i = ya; i <= yb; i++ )
		t[i][x] = c;

}

void hColor( char t[][252], int xa, int xb, int y, char c ){

	if( xb < xa )
		xb ^= xa ^= xb ^= xa;

 int i;

	for( i = xa; i <= xb; i++ )
		t[y][i] = c;

}

void kColor( char t[][252], int xa, int ya, int xb, int yb, char c ){

 int i, j;

	if( xb < xa )
		xb ^= xa ^= xb ^= xa;

	if( yb < ya )
		yb ^= ya ^= yb ^= ya;

  for(i = ya; i <= yb; i++)
	for( j = xa ; j <= xb; j++ )
		t[i][j] = c ;

}

void fColor( char t[][252], int x, int y, char c ){

  char oldColor = t[y][x];

  if( oldColor == c )
	return;

  if( t[y][x] == '1' )
	return;

  t[y][x] = c;

  if( t[y-1][x] == oldColor )		fColor( t, x, y-1, c );
  if( t[y][x-1] == oldColor )		fColor( t, x-1, y, c );
  if( t[y][x+1] == oldColor )		fColor( t, x+1, y, c );
  if( t[y+1][x] == oldColor )		fColor( t, x, y+1, c );

}

int main(){

  char cmd, param;
  char name[99];
  char table[252][252];
  int widht, height;
  int x, y, z, k;

	do{
	scanf("%c", &cmd);
	switch( cmd ){

	  case 'I':
		scanf("%d %d", &widht, &height );
		newTable( table );
		break;
	  case 'C':
		newTable( table );
		break;
	  case 'L':
		scanf("%d %d %c", &x, &y, &param);
		pColor( table, x, y, param );
		break;
	  case 'V':
		scanf("%d %d %d %c", &x, &y, &z, &param);
		vColor( table , x, y, z, param );
		break;
	  case 'H':
		scanf("%d %d %d %c", &x, &y, &z, &param);
		hColor( table , x, y, z, param );
		break;
	  case 'K':
		scanf("%d %d %d %d %c", &x, &y, &z, &k, &param);
		kColor( table, x, y, z, k, param );
		break;
	  case 'F':
		scanf("%d %d %c", &x, &y, &param);
		fColor( table ,  x, y, param );
		break;
	  case 'S':
		scanf("%s", &name);
		printf("%s\n", name );
		prntTable( table, widht, height );
		break;
	  case 'X':
		return 0;
		break;
	  default:
		break;

	}
	scanf("%c", &cmd);


	} while ( 1 == 1 );



 return 0;
}
If as a command there will be a character different from I, C, L, V, H, K, F, S, X, the editor should ignore the whole line and pass to the next command.
Input

Code: Select all

I 5 6
S one.jpeg
J I 2 2
S two.jpeg
X
Correct output:

Code: Select all

one.jpeg                                                  
OOOOO                                                     
OOOOO                                                     
OOOOO                                                     
OOOOO
OOOOO
OOOOO
two.jpeg
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO
OOOOO

Obaida
A great helper
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10267 - Graphical Editor

Post by Obaida » Sun Jul 26, 2009 4:52 am

For every one getting RTE(my team name :D )

do this additional checking

Code: Select all

if(c==grid[y][x])return;
Here c is the character to be inserted and y and x are co-ordinate(be sure of your format, [y][x] or [x][y]?).
try_try_try_try_&&&_try@try.com
This may be the address of success.

vizardo
New poster
Posts: 8
Joined: Sun Mar 15, 2009 9:42 pm

Re: 10267 - Graphical Editor

Post by vizardo » Tue Jul 28, 2009 8:34 am

WA WA!! I have read almost the entire forum, but still, I can't get AC. I have tested so many test cases but it's still WA... please help! I have no idea what is wrong now!! :(

Code: Select all

import java.io.*;
import java.util.*;

public class Main{
	
	static int row = 0;
	static int col = 0;
	static char[][] table;
	
	public static void init(){
		table = new char[row][col];
		
		for (int i = 0; i < row; i++){
			for (int j = 0; j < col; j++){
				table[i][j] = 'O';
			}
		}
	}
	
	public static void clear(){
		for (int i = 0; i < row; i++){
			for (int j = 0; j < col; j++){
				table[i][j] = 'O';
			}
		}
	}
	
	public static void color(int r, int c, char ch){
		table[r-1][c-1] = ch;
	}
	
	
	public static void vertical(int r1, int r2, int c, char ch){
		if (r1 > r2){
			int temp = r1;
			r1 = r2;
			r2 = temp;
		}
		
		for (int i = r1; i <= r2; i++){
			table[i-1][c-1] = ch;
		}
	}
	
	
	public static void horizontal(int r, int c1, int c2, char ch){
		if (c1 > c2){
			int temp = c1;
			c1 = c2;
			c2 = temp;
		}
		
		for (int i = c1; i <= c2; i++){
			table[r-1][i-1] = ch;
		}
	}
	
	
	public static void rect(int r1, int c1, int r2, int c2, char ch){
		int temp = 0;
		
		if (r1 > r2){
			temp = r1;
			r1 = r2;
			r2 = temp;
		}
		if (c1 > c2){
			temp = c1;
			c1 = c2;
			c2 = temp;
		}
		
		for (int i = r1; i <= r2; i++){
			for (int j = c1; j <= c2; j++){
				table[i-1][j-1] = ch;
			}
		}
	}
	
	public static void fill (int r, int c, char ch){
		char ori = table[r-1][c-1];
		int[][] temp = new int[row][col];
		boolean isChange = true;
		
		for (int i = 0; i < row; i++){
			for (int j = 0; j < col; j++){
				temp[i][j] = 0;
			}
		}
		
		temp[r-1][c-1] = 1;
		
		
		while (isChange == true){
			isChange = false;
			
			for (int i = 0; i < row; i++){
				for (int j = 0; j < col; j++){
					if (temp[i][j] == 1){
						if (i-1 >= 0 && table[i-1][j] == ori && temp[i-1][j] == 0){
							temp[i-1][j] = 1;
							isChange = true;
						}
						
						if (j+1 < col && table[i][j+1] == ori && temp[i][j+1] == 0){
							temp[i][j+1] = 1;
							isChange = true;
						}
							
						if (i+1 < row && table[i+1][j] == ori && temp[i+1][j] == 0){
							temp[i+1][j] = 1;
							isChange = true;
						}
						
						if (j-1 >= 0 && table[i][j-1] == ori && temp[i][j-1] == 0){
							temp[i][j-1] = 1;
							isChange = true;
						}
					}
				}
			}
		}
		
		for (int i = 0; i < row; i++){
			for (int j = 0; j < col; j++){
				if (temp[i][j] == 1){
					table[i][j] = ch;
				}
			}
		}
		
		
	}
	
	
	public static void save(String name){
		System.out.println(name);
		for (int i = 0; i < row; i++){
			for (int j = 0; j < col; j++){
				System.out.print(table[i][j]);
				table[i][j] = 'O';
			}
			System.out.println("");
		}
	}
	
	
	public static void main(String[] args) throws Exception{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer token = new StringTokenizer(br.readLine());
		
		String cmd = token.nextToken();
		
		while (!cmd.equals("X")){
			int c1, c2, r1, r2;
			char ch;
			String name;
			
			switch(cmd.charAt(0)){
				case 'I':
					col = Integer.parseInt(token.nextToken());
					row = Integer.parseInt(token.nextToken());
					init();
					break;
					
				case 'C':
					clear();
					break;
					
				case 'L':
					c1 = Integer.parseInt(token.nextToken());
					r1 = Integer.parseInt(token.nextToken());
					ch = token.nextToken().charAt(0);
					color(r1,c1,ch);
					break;
				
				case 'V':
					c1 = Integer.parseInt(token.nextToken());
					r1 = Integer.parseInt(token.nextToken());
					r2 = Integer.parseInt(token.nextToken());
					ch = token.nextToken().charAt(0);
					vertical(r1,r2,c1,ch);
					break;
					
				case 'H':
					c1 = Integer.parseInt(token.nextToken());
					c2 = Integer.parseInt(token.nextToken());
					r1 = Integer.parseInt(token.nextToken());
					ch = token.nextToken().charAt(0);
					horizontal(r1,c1,c2,ch);
					break;
					
				case 'K':
					c1 = Integer.parseInt(token.nextToken());
					r1 = Integer.parseInt(token.nextToken());
					c2 = Integer.parseInt(token.nextToken());
					r2 = Integer.parseInt(token.nextToken());
					ch = token.nextToken().charAt(0);
					rect(r1,c1,r2,c2,ch);
					break;
					
				case 'F':
					c1 = Integer.parseInt(token.nextToken());
					r1 = Integer.parseInt(token.nextToken());
					ch = token.nextToken().charAt(0);
					if (r1 >= 1 && r1 <= row && c1 >= 1 && c1 <= col){
						if (table[r1-1][c1-1] != ch) fill(r1,c1,ch);
					}
					break;
					
				case 'S':
					name = token.nextToken();
					save(name);
					break;
					
				default:
					break;
			}
			
			token = new StringTokenizer(br.readLine());
			
			cmd = token.nextToken();
		}
		
		
		br.close();
		System.exit(0);
		
	}
}

barqawi
New poster
Posts: 6
Joined: Mon Aug 31, 2009 2:14 am

10267 - Graphical Editor

Post by barqawi » Fri Sep 04, 2009 10:01 pm

hey every body,

i am trying to solve graphic editor by java, i made every thing as instructions in the question it is work correctly with question input ..... but it is always giving me wrong answer with online judge.

my code work with:
- x1>x2 ... swap
- if color more than one char

can some body give me sample input and output to test my code different than input in the question



or check my code by java

Code: Select all

import java.util.LinkedList;
import java.util.Scanner;



/**
 *
 * @author bjaguar
 */
public class Main {


    public static void main(String[] args){


        Scanner sc = new Scanner(System.in);
        proces(sc);

    }//mian method

    private static void proces(Scanner sc){

        String mtx[][]=null;
        int row = 0, col = 0;
        int r1, r2, c1, c2;
        int hold;
        String color;
        String line=sc.nextLine();

        while (line.charAt(0) !='X'){

            switch(line.charAt(0)){
                case 'I':
                    col = line.charAt(2)-48;
                    row = line.charAt(4)-48;
                    mtx = new String[row][col];
                    setWhite(row, col, mtx);
                    break;
                case 'C':
                    setWhite(row, col, mtx);
                    break;
                case 'L':
                    c1 = line.charAt(2)-48-1;
                    r1 = line.charAt(4)-48-1;
                    color = line.substring(6);
                    mtx[r1][c1] = color;
                    break;
                case 'V':
                    c1 = line.charAt(2)-48-1;
                    r1 = line.charAt(4)-48-1;
                    r2 = line.charAt(6)-48-1;
                    color = line.substring(8);
                    if (r1 > r2){hold = r1;r1 = r2;r2 = hold;}//swap
                    setV(r1, r2, c1, color, mtx);
                    break;
                case 'H':
                    c1 = line.charAt(2)-48-1;
                    c2 = line.charAt(4)-48-1;
                    r1 = line.charAt(6)-48-1;
                    color = line.substring(8);
                    if (c1 > c2){hold = c1;c1 = c2;c2 = hold;}//swap
                    setH(r1, c1, c2, color, mtx);
                    break;
                case 'K':
                    c1 = line.charAt(2)-48-1;
                    r1 = line.charAt(4)-48-1;
                    c2 = line.charAt(6)-48-1;
                    r2 = line.charAt(8)-48-1;
                    color = line.substring(10);
                    if (c1 > c2){hold = c1;c1 = c2;c2 = hold;}//swap
                    if (r1 > r2){hold = r1;r1 = r2;r2 = hold;}//swap
                    drawRec(r1, c1, r2, c2, color, mtx);
                    break;
                case 'F':
                    c1 = line.charAt(2)-48-1;
                    r1 = line.charAt(4)-48-1;
                    color = line.substring(6);
                    if (!mtx[r1][c1].equals(color))
                        setRegion(r1, c1, row, col, color, mtx);
                    break;
                case 'S':
                    System.out.println(line.substring(2));
                    for (int i=0; i<row; i++){
                        for (int j=0; j<col; j++)
                            System.out.print(mtx[i][j]);
                        System.out.println();
                    }// loop to print mtx
                    break;
            }//switch
            line=sc.nextLine();

        }//while not end
    }//method to read from the system

    private static void drawRec(int r1, int c1, int r2, int c2, String color, String mtx[][]){

        for (int i=r1; i<=r2;i++){
            for (int j=c1; j<=c2; j++){
                mtx[i][j] = color;
            }//loop on cols
        }//loop on rows
    }//method to draw rectangle
    
    private static void setRegion(int r1, int c1, int row, int col, String color, String mtx[][]){

        int check[][] = new int[row][col];//0-uncheck(open) 1-checked(close)
        String oldColor = mtx[r1][c1];
        int i,j, count=0;
        int pos[];
        LinkedList<int[]> open = new LinkedList<int[]>();

        mtx[r1][c1] = color;
        check[r1][c1] = 1;
        open.add(new int[]{r1,c1});
        while (!open.isEmpty()){


            pos = open.removeFirst();
            i = pos[0];j = pos[1];            
            addToRegion(i, j, oldColor, color, row, col, check, mtx, open);            
        }//loop on all positions in the region


    }//method to set the F region

    private static void addToRegion(int i, int j, String oldColor, String color, int row, int col,
            int check[][], String mtx[][], LinkedList<int[]> open){
          int goTo, number;

          
        goTo = i;
        if (j!=0 && check[goTo][j-1]!=1 && mtx[goTo][j-1].equals(oldColor)){
            mtx[goTo][j-1] = color;
            check[goTo][j-1]= 1;
            open.add(new int[]{goTo,j-1});
            
        }//if left
        if (j!=col-1 && check[goTo][j+1]!=1 && mtx[goTo][j+1].equals(oldColor) ){
            mtx[goTo][j+1] = color;
            check[goTo][j+1] = 1;
            open.add(new int[]{goTo,j+1});           
        }//if right

        // row up
        if (i!=0){
            goTo = i-1;
            // the same col
            if(check[goTo][j]!=1 && mtx[goTo][j].equals(oldColor)){
                mtx[goTo][j] = color;
                check[goTo][j] = 1;
                open.add(new int[]{goTo,j});
            }//smae col
            if (j!=0 && check[goTo][j-1]!=1 && mtx[goTo][j-1].equals(oldColor)){
                 mtx[goTo][j-1] = color;
                 check[goTo][j-1] = 1;
                 open.add(new int[]{goTo,j-1});
            }//if left col
            if (j!=col-1 && check[goTo][j+1]!=1 && mtx[goTo][j+1].equals(oldColor)){
                 mtx[goTo][j+1] = color;
                 check[goTo][j+1] = 1;
                 open.add(new int[]{goTo,j+1});
            }//if right col
        }//if there is upper row

        // row down
        if (i!=row-1){
            goTo = i+1;
            // the same col
            if(check[goTo][j]!=1 && mtx[goTo][j].equals(oldColor)){
                mtx[goTo][j] = color;
                check[goTo][j] = 1;
                open.add(new int[]{goTo,j});
            }//smae col
            if (j!=0 && check[goTo][j-1]!=1 && mtx[goTo][j-1].equals(oldColor)){
                 mtx[goTo][j-1] = color;
                 check[goTo][j-1] = 1;
                 open.add(new int[]{goTo,j-1});
            }//if left col
            if (j!=col-1 && check[goTo][j+1]!=1 && mtx[goTo][j+1].equals(oldColor)){
                 mtx[goTo][j+1] = color;
                 check[goTo][j+1] = 1;
                 open.add(new int[]{goTo,j+1});
            }//if right col
        }// if there is down row

    }//set index to region

    

    private static void setH(int row, int c1,int c2, String color, String mtx[][]){

        for (int col=c1; col<=c2; col++)
            mtx[row][col] = color;
    }//method to set the h

    private static void setV(int r1,int r2, int col, String color, String mtx[][]){

        for (int row=r1; row<=r2; row++)
            mtx[row][col] = color;
    }//method to set the v

    private static void setWhite(int row,int col, String mtx[][]){
        for (int i=0; i<row; i++)
            for (int j=0; j<col; j++)
                mtx[i][j] = "O";
    }//make all matrix white


}//class

hope you help me please

barqawi
New poster
Posts: 6
Joined: Mon Aug 31, 2009 2:14 am

Re: 10267 - Graphical Editor

Post by barqawi » Fri Sep 04, 2009 11:56 pm

about the region for x ,y << see the *

it is like this:
1*3
*5*
7*9

or like this:
***
*5*
***

barqawi
New poster
Posts: 6
Joined: Mon Aug 31, 2009 2:14 am

Re: 10267 - Graphical Editor

Post by barqawi » Sat Sep 05, 2009 2:20 am

ok ok i know my wrongs in the code

one about checking neighbors and one when reading input

if there is another errors i will tell u

barqawi
New poster
Posts: 6
Joined: Mon Aug 31, 2009 2:14 am

Re: 10267 - Graphical Editor

Post by barqawi » Sat Sep 05, 2009 2:49 am

i solved ... thaks god ... thaks for you i read instuctions here in other post ... it was helpful

this is my code in java ... hope it is help you

Code: Select all


import java.util.LinkedList;
import java.util.Scanner;



/**
 *
 * @author bjaguar
 */
public class Main {

/*
 .......
   ...n...
   ..nRn..
   ...n...
   .......
 */
    public static void main(String[] args){


        Scanner sc = new Scanner(System.in);
        proces(sc);

    }//mian method

    private static void proces(Scanner sc){

        char mtx[][]=null;
        int row = 0, col = 0;
        int r1, r2, c1, c2;
        int hold;
        char color;
        boolean loop = true;
        char cas;

        while (loop){
            cas = sc.next().charAt(0);
            switch(cas){
                case 'I':
                    col = sc.nextInt();
                    row = sc.nextInt();
                    mtx = new char[row][col];
                    setWhite(row, col, mtx);
                    break;
                case 'C':
                    setWhite(row, col, mtx);
                    break;
                case 'L':
                    c1 = sc.nextInt()-1;
                    r1 = sc.nextInt()-1;
                    color = sc.next().charAt(0);
                    if ( c1<col && r1<row  && c1>=0 && r1>=0)
                    mtx[r1][c1] = color;
                    break;
                case 'V':
                    c1 = sc.nextInt()-1;
                    r1 = sc.nextInt()-1;
                    r2 = sc.nextInt()-1;
                    color = sc.next().charAt(0);
                    if (r1 > r2){hold = r1;r1 = r2;r2 = hold;}//swap
                    if ( c1<col && r1<row  && c1>=0 && r1>=0 && r2<row && r2>=0)
                    setV(r1, r2, c1, color, mtx);
                    break;
                case 'H':
                    c1 = sc.nextInt()-1;
                    c2 = sc.nextInt()-1;
                    r1 = sc.nextInt()-1;
                    color = sc.next().charAt(0);
                    if (c1 > c2){hold = c1;c1 = c2;c2 = hold;}//swap
                    if ( c1<col && r1<row  && c1>=0 && r1>=0 && c2<col && c1>=0)
                    setH(r1, c1, c2, color, mtx);
                    break;
                case 'K':
                    c1 = sc.nextInt()-1;
                    r1 = sc.nextInt()-1;
                    c2 = sc.nextInt()-1;
                    r2 = sc.nextInt()-1;
                    color = sc.next().charAt(0);
                    if (c1 > c2){hold = c1;c1 = c2;c2 = hold;}//swap
                    if (r1 > r2){hold = r1;r1 = r2;r2 = hold;}//swap
                    if ( c1<col && r1<row  && c1>=0 && r1>=0 && c2<col && c1>=0 && r2<row && r2>=0)
                    drawRec(r1, c1, r2, c2, color, mtx);
                    break;
                case 'F':
                    c1 = sc.nextInt()-1;
                    r1 = sc.nextInt()-1;
                    color = sc.next().charAt(0);
                    if ( c1<col && r1<row  && c1>=0 && r1>=0 && mtx[r1][c1] != color)
                        setRegion(r1, c1, row, col, color, mtx);
                    break;
                case 'S':
                    System.out.println(sc.next());
                    for (int i=0; i<row; i++){
                        for (int j=0; j<col; j++)
                            System.out.print(mtx[i][j]);
                        System.out.println();
                    }// loop to print mtx
                    break;
                case 'X':
                    loop = false;
                    break;
            }//switch            

        }//while not end
    }//method to read from the system

    private static void drawRec(int r1, int c1, int r2, int c2, char color, char mtx[][]){
        for (int i=r1; i<=r2;i++){
            for (int j=c1; j<=c2; j++){
                mtx[i][j] = color;
            }//loop on cols
        }//loop on rows
    }//method to draw rectangle
    
    private static void setRegion(int r1, int c1, int row, int col, char color, char mtx[][]){

        int check[][] = new int[row][col];//0-uncheck(open) 1-checked(close)
        char oldColor = mtx[r1][c1];
        int i, j;
        int pos[];
        LinkedList<int[]> open = new LinkedList<int[]>();

        mtx[r1][c1] = color;
        check[r1][c1] = 1;
        open.add(new int[]{r1,c1});
        
        while (!open.isEmpty()){

            pos = open.removeFirst();
            i = pos[0];j = pos[1];            
            addToRegion(i, j, oldColor, color, row, col, check, mtx, open);            
        }//loop on all positions in the region
    }//method to set the F region

    private static void addToRegion(int i, int j, char oldColor, char color, int row, int col,
            int check[][], char mtx[][], LinkedList<int[]> open){
          
        int goTo;

        goTo = i;
        if (j!=0 && check[goTo][j-1]!=1 && mtx[goTo][j-1] == oldColor){
            mtx[goTo][j-1] = color;
            check[goTo][j-1]= 1;
            open.add(new int[]{goTo,j-1});
            
        }//if left
        if (j!=col-1 && check[goTo][j+1]!=1 && mtx[goTo][j+1] == oldColor ){
            mtx[goTo][j+1] = color;
            check[goTo][j+1] = 1;
            open.add(new int[]{goTo,j+1});           
        }//if right

        // row up
        if (i!=0){
            goTo = i-1;            
            if(check[goTo][j]!=1 && mtx[goTo][j] == oldColor){
                mtx[goTo][j] = color;
                check[goTo][j] = 1;
                open.add(new int[]{goTo,j});
            }//smae col            
        }//if there is upper row

        // row down
        if (i!=row-1){
            goTo = i+1;            
            if(check[goTo][j]!=1 && mtx[goTo][j] == oldColor){
                mtx[goTo][j] = color;
                check[goTo][j] = 1;
                open.add(new int[]{goTo,j});
            }//smae col
            
        }// if there is down row

    }//set index to region

    

    private static void setH(int row, int c1,int c2, char color, char mtx[][]){

        for (int col=c1; col<=c2; col++)
            mtx[row][col] = color;
    }//method to set the h

    private static void setV(int r1,int r2, int col, char color, char mtx[][]){

        for (int row=r1; row<=r2; row++)
            mtx[row][col] = color;
    }//method to set the v

    private static void setWhite(int row,int col, char mtx[][]){
        for (int i=0; i<row; i++)
            for (int j=0; j<col; j++)
                mtx[i][j] = 'O';
    }//make all matrix white


}//class


i there is something u doent anderstand in my code ... just ask me

imonhf
New poster
Posts: 1
Joined: Tue Jun 23, 2009 5:45 pm

CE

Post by imonhf » Sat Apr 24, 2010 12:04 pm

Why my code is showing compilation error!
pls help
It shows no problem in code blocks!!!


Code: Select all


// ACM 10267
//Written by imon
//24-04-10
#include<stdio.h>
#include<math.h>
char fld[255][255], c1, c2;

void func(int x, int y)
{
    if(fld[x][y]==c2)
    {
        fld[x][y]=c1;
        func(x-1, y-1);
        func(x-1, y);
        func(x-1, y+1);
        func(x, y-1);
        func(x, y+1);
        func(x+1, y-1);
        func(x+1, y);
        func(x+1, y+1);
    }
}
int main()
{
    //freopen("input.txt","r",stdin);
    char ch, c, tmp, str[50];
    int x, x1, x2, y, y1, y2, m, n, i, j;

    while(scanf("%c", &ch)&&ch!='X')
    {
        switch(ch)
        {
            case 'I':
                scanf("%d%d", &n, &m);
                for(i=0; i<=m+1; i++)
                    for(j=0; j<=n+1; j++)
                    {
                        fld[i][j]='0';
                    }
                break;

            case 'C':
                for(i=0; i<=m+1; i++)
                    for(j=0; j<=n+1; j++)
                    {
                        fld[i][j]='0';
                    }
                break;

            case 'L':
                scanf("%d%d%c%c", &x, &y, &tmp, &c);
                fld[y][x] = c;
                break;

            case 'V':
                scanf("%d%d%d%c%c", &x, &y1, &y2, &tmp, &c);
                if(y1<y2)
                    for(i=y1; i<=y2; i++)
                    {
                        fld[i][x]=c;
                    }
                else
                    for(i=y2; i<=y1; i++)
                    {
                        fld[i][x]=c;
                    }
                break;

            case 'H':
                scanf("%d%d%d%c%c", &x1, &x2, &y, &tmp, &c);
                if(x1<x2)
                    for(i=x1; i<=x2; i++)
                    {
                        fld[y][i]=c;
                    }
                else
                    for(i=x2; i<=x1; i++)
                    {
                        fld[y][i]=c;
                    }
                break;

            case 'K':
                scanf("%d%d%d%d%c%c", &x1, &y1, &x2, &y2, &tmp, &c);
                if(x1>x2)
                    for(i=x1, j=y1; i<=x2; i++, j++)
                    {
                        fld[j][i]=c;
                    }
                    break;

            case 'F':
                scanf("%d%d%c%c", &x, &y, &tmp, &c1);
                c2 = fld[y][x];
                func(y, x);
                break;

            case 'S':
                scanf("%s", str);
                printf("%s\n", str);
                for(i=1; i<=m; i++)
                {
                    for(j=1; j<=n; j++)
                    {
                        printf("%c", fld[i][j]);
                    }
                    printf("\n");
                }
                break;
        }
    }
    return 0;
}



User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

Re: 10267 - Graphical Editor

Post by sohel » Sat Apr 24, 2010 12:18 pm

When you get a 'compile error' verdict, you should be able to see the reason for CE by clicking on the link.

ethdem
New poster
Posts: 1
Joined: Wed May 19, 2010 4:41 pm

Re: 10267 - Graphical Editor

Post by ethdem » Wed May 19, 2010 4:45 pm

I'm getting WA after several tries. I've checked my fill functions and every other, my test data works fine, I compensate for x1 > x2 or y1 > y2, my test cases seem to work as well. Still no AC. Anyone see a problem?

Code: Select all

#include <iostream>
#include <string>

#define TABLE_MIN 1
#define TABLE_MAX 251

using namespace std;

int tablex, tabley;

char colortable[TABLE_MAX][TABLE_MAX];

void clear() {
	for (int i = 0; i <= tablex ; i++) {
		for (int j = 0; j <= tabley; j++) {
			colortable[i][j] = 'O';
		}
	}
}

void setcolor( int x, int y, char c ) {
	colortable[x][y] = c;
}

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

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

void drawrect( 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 = x1; i <= x2; i++ ) {
		for( int j = y1; j <= y2; j++) {
			colortable[i][j] = c;
		}
	}

}


void fill( int x, int y, char c1, char c2 ) {
	colortable[x][y] = c1;
	for( int i = x-1; i <= x+1; i += 2 ) {
		if( i >= TABLE_MIN && y >= TABLE_MIN && i <= tablex  && y <= tabley && colortable[i][y] == c2 ) {
			fill( i, y, c1, c2 );
		}
	}
		for( int j = y-1; j <= y+1; j += 2 ) {
			if( x >= TABLE_MIN && j >= TABLE_MIN && x <= tablex && j <= tabley && colortable[x][j] == c2 ) {
				fill( x, j, c1, c2 );
			}
		}
	}


void fillwrapper( int x, int y, char c ) {
	char currcol = colortable[x][y];
	if (c == currcol)
		return;
	fill( x, y, c, currcol );
}

void save( string filename, int x, int y ) {
		filename.resize(12);
	cout << filename << endl;
	for(int i = TABLE_MIN; i <= y; i++) {
		for(int j = TABLE_MIN; j <= x; j++) {
			printf( "%c", colortable[j][i] );
		}
		printf( "\n" );
	}
}


int main(int arg, char **argv) {
	string filename;
	char colorarg;
	tablex = 0;
	tabley = 0;
	for(;;) {
		char cmd = 0;
		int x1 = 0, x2 = 0, y1 = 0, y2 = 0;
		cin >> cmd;
		switch(cmd) {
			case 'X':
				return 0;
			case 'I':
				cin >> tablex >> tabley;
			case 'C':
				clear();
				break;
			case 'L':
				cin >> x1 >> y1 >> colorarg;
				setcolor( x1, y1, colorarg );
				break;
			case 'V':
				cin >> x1 >> y1 >> y2 >> colorarg;
				vline( x1, y1, y2, colorarg );
				break;
			case 'H':
				cin >> x1 >> x2 >> y1 >> colorarg;
				hline( y1, x1, x2, colorarg );
				break;
			case 'K':
				cin >> x1 >> y1 >> x2 >> y2 >> colorarg;
				drawrect( x1, x2, y1, y2, colorarg );
				break;
			case 'F':
				cin >> x1 >> y1 >> colorarg;
				fillwrapper( x1, y1, colorarg );
				break;
			case 'S':
				cin >> filename;
				save( filename, tablex, tabley );
				break;
			default:
				cin.ignore(255,'\n');
				break;
		}
	}

	return 0;
}

Post Reply

Return to “Volume 102 (10200-10299)”