10267 - Graphical Editor

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

Moderator: Board moderators

Re: 10267 - Graphical Editor

Postby 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;
}
paaulocezar
New poster
 
Posts: 5
Joined: Thu May 14, 2009 4:14 pm

Re: 10267 - Graphical Editor

Postby 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);
         }
      }
               
         
      }
   }



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

Re: 10267 - Graphical Editor

Postby 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.
chili5
New poster
 
Posts: 1
Joined: Wed Jul 01, 2009 6:27 pm

Re: 10267 - Graphical Editor

Postby 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

Postby 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
x140l31
Learning poster
 
Posts: 69
Joined: Tue Jan 30, 2007 12:51 am

Re: 10267 - Graphical Editor

Postby 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.
Obaida
A great helper
 
Posts: 380
Joined: Wed Jan 16, 2008 6:51 am
Location: (BUBT) Dhaka,Bagladesh.

Re: 10267 - Graphical Editor

Postby 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);
      
   }
}
vizardo
New poster
 
Posts: 8
Joined: Sun Mar 15, 2009 9:42 pm

Re: 10267 - Graphical Editor

Postby chackal_sjc » Thu Jun 10, 2010 11:28 pm

Dude,

I'm getting WA but all my inputs are correct :cry: ... does someone have a really good input to test?

Thank you
UNIFEI - Universidade Federal de Itajubá, Brazil
chackal_sjc
New poster
 
Posts: 2
Joined: Wed Jun 09, 2010 5:53 am

Re: 10267 - Graphical Editor

Postby qweasdf_123 » Sat Dec 04, 2010 9:37 pm

Hi everybody,i always get WA.But my code is perfect and works fine.Please,help me.Where is my fault.I' be very very pleased.

public class Main{
// 110105/10267/Graphical Editor
public static void main(String[] args) throws IOException{
new Main().run();
}
private PrintWriter out;
private Scanner in;
private char[][] a=new char[250][250];
private int n=0,m=0, i=0,i1=0,i2=0, j=0,j1=0,j2=0;
private char color='O';

void run() throws IOException{
// in = new Scanner(new File("src/input.txt"));
// out = new PrintWriter(new File("src/output.txt"));

in = new Scanner(System.in);
out = new PrintWriter(System.out,true);

solve();

in.close();
out.close();
}
void solve() throws IOException{
int cnt=0;
while(in.hasNextLine()){
String cmdLine=in.nextLine().trim();
String params[]=cmdLine.split(" ");
char cmd=cmdLine.charAt(0);

switch(cmd){
case 'I':
m=Integer.parseInt(params[1]);
n=Integer.parseInt(params[2]);
a=new char[n][m];
for(int t=0;t<n;t++)
for(int k=0;k<m;k++)
a[t][k]='O';
break;
case 'C':
// c
for(int t=0;t<n;t++)
for(int k=0;k<m;k++)
a[t][k]='O';
break;
case 'L':
// l x y c
j=Integer.parseInt(params[1])-1;
i=Integer.parseInt(params[2])-1;
color=params[3].charAt(0);
a[i][j]=color;
break;
case 'V':
// v x y1 y2 c
j=Integer.parseInt(params[1])-1;
i1=Integer.parseInt(params[2])-1;
i2=Integer.parseInt(params[3])-1;
color=params[4].charAt(0);
for(int t=Math.min(i1,i2);t<=Math.max(i1,i2);t++)
a[t][j]=color;
break;
case 'H':
// h x1 x2 y c
j1=Integer.parseInt(params[1])-1;
j2=Integer.parseInt(params[2])-1;
i=Integer.parseInt(params[3])-1;
color=params[4].charAt(0);
for(int k=Math.min(j1,j2);k<=Math.max(j1,j2);k++)
a[i][k]=color;
break;
case 'K':
// k x1 y1 x2 y2 c
j1=Integer.parseInt(params[1])-1;
i1=Integer.parseInt(params[2])-1;
j2=Integer.parseInt(params[3])-1;
i2=Integer.parseInt(params[4])-1;
color=params[5].charAt(0);
for(int t=Math.min(i1,i2);t<=Math.max(i1,i2);t++)
for(int k=Math.min(j1,j2);k<=Math.max(j1,j2);k++)
a[t][k]=color;
break;
case 'F':
// f x y c
i=Integer.parseInt(params[1])-1;
j=Integer.parseInt(params[2])-1;
color=params[3].charAt(0);
char oldColor=a[i][j];
if(color==oldColor)
break;

int curr=0;
Vector<Pixel> q=new Vector<Pixel>();// queue
boolean[][] used=new boolean[n][m];
used[i][j]=true;
q.add(new Pixel(i,j));
while(curr<q.size()){
i=q.get(curr).i;
j=q.get(curr).j;
for(int t=i-1;t<=i+1;t++)
for(int k=j-1;k<=j+1;k++)
if(t<n && t>=0 && k<m && k>=0 && (t==i||k==j) && !used[t][k] && a[t][k]==oldColor){
used[t][k]=true;
q.add(new Pixel(t,k));
}
a[i][j]=color;
curr++;
}
break;
case 'S':
// s name
cnt++;
String fileName=params[1];
if(cnt>1)
out.println();
out.println(fileName);
for(int t=0;t<n;t++){
for(int k=0;k<m;k++)
out.print(a[t][k]);
if(t<n-1)
out.println();
}
break;
case 'X':
return;
default:
break;
}
}
}
class Pixel{
int i, j;
Pixel(int i,int j){
this.i=i;
this.j=j;
}
}
}
qweasdf_123
New poster
 
Posts: 2
Joined: Sun Nov 28, 2010 11:03 am

Re: 10267 - Graphical Editor

Postby darka » Fri Dec 16, 2011 10:39 am

I 've looked through the whole discussion and checked that I've made none of the errors you guys mentioned, but I still get Runtime error :(. Can someone help find my error ? Thanks in advance :D
Here is my code: ( I used a First In First Out queue for the floodfill )
Code: Select all
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;


long i,m,n,j,x,x2,y,y2,qend,qstart,queue[10000000][2];
char oldcolour,colour,comword,board[300][300];
string filename,temp;

const int addx[4]={0,0,1,-1};
const int addy[4]={1,-1,0,0};

void qin(int x3,int y3){     

     qend++;
     queue[qend][0]=x3;
     queue[qend][1]=y3;     
   
}
void  qout(){
     
      qstart++;
      x=queue[qstart][0];
      y=queue[qstart][1];
     
}
int main (){
    m=0;
    n=0;
   
    while ( (cin >> comword)&&(comword != 'X') ){
          switch (comword){
          case 'I':
               
               cin >> m >> n;
               for (i=1;i<=m;i++)
                   for (j=1;j<=n;j++)
                       board[i][j] = 'O';

               // Draw a "$" rectangle outside the picture
               for (i=0;i<=m+1;i++) { board[i][0]='$'; board[i][n+1]='$'; }
               for (i=0;i<=n+1;i++) { board[0][i]='$'; board[m+1][i]='$'; }
             
               getline(cin,temp);
               break;
          case 'C':
               for (i=1;i<=m;i++)
                   for (j=1;j<=n;j++)
                       board[i][j] = 'O';
               getline(cin,temp);
               break;
          case 'L':
               cin >> x >> y >> colour;
               board[x][y]=colour;

               getline(cin,temp);
               break;
          case 'V':
               cin >> x >> y >> y2 >> colour;
               if (y > y2) { y=y+y2;y2=y-y2;y=y-y2;}
               
               for (i=y;i<=y2;i++) board[x][i] = colour;

               getline(cin,temp);
               break;
          case 'H':
               cin >> x >> x2 >> y >> colour;
               if (x > x2) { x=x+x2;x2=x-x2;x=x-x2;}
               
               for (i=x;i<=x2;i++) board[i][y] = colour;

               getline(cin,temp);
               break;
          case 'K':
               cin >> x >> y >> x2 >> y2 >> colour;
               if (y > y2) { y=y+y2;y2=y-y2;y=y-y2;}
               if (x > x2) { x=x+x2;x2=x-x2;x=x-x2;}
               
               for (i=x;i<=x2;i++)
                   for (j=y;j<=y2;j++)
                       board[i][j]=colour;

               getline(cin,temp);
               break;
          case 'F':
               cin >> x >> y >> colour;
             
               qend=0;               
               qstart=0;
               qin(x,y);
                             
               oldcolour=board[x][y];
               board[x][y]=colour;
               
               do{
                 
                  qout();
                  for (i=0;i<=3;i++)
                      if ( board[x+addx[i]][y+addy[i]]==oldcolour )
                      {
                         qin(x+addx[i], y+addy[i]);
                         board[x+addx[i]][y+addy[i]] = colour;                                                                                                         
                      }               
               }
               while (qstart<qend);
                             
               getline(cin,temp);
               break;
          case 'S':
               
               cin >> filename;
                                             
               cout << filename << endl;
               for (i=1 ;i<=n;i++)
                   {
                        for(j=1;j<=m;j++)
                                         cout << board[j][i];
                        cout << endl;
                   }
               getline(cin,filename);
               break;
          default:
               getline(cin,temp);
               break;
          }
               
    }

}
darka
New poster
 
Posts: 1
Joined: Fri Dec 16, 2011 10:24 am

Re: 10267 - Graphical Editor

Postby brianfry713 » Thu Jan 19, 2012 2:31 am

darka, see what happens with an F command matching the pixel's current color.
brianfry713
Guru
 
Posts: 1761
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

10267 - Graphical Editor: WA

Postby adityaarun1 » Mon Jan 21, 2013 4:38 pm

I have been trying this problem and it works correctly for test input set given in question. Can anyone please point me out where I am going wrong. Thanks.. :)

Code: Select all
#include<stdio.h>

#define MAX 250

void funcF(int, int, char, char);

char disp[MAX][MAX];
int M,N;   /*M->rows N->coloumns*/

int main()
{
   char cmd[5],C,name[1000];
   int X,Y,X1,X2,Y1,Y2;
   int i,j;
   while(1)
   {
      scanf("%s",cmd);
      if(cmd[0]=='X')
         return 0;
      else if(cmd[0]=='I')
      {
         scanf("%d %d",&M,&N);
         for(i=0;i<N;i++)
         {
            for(j=0;j<M;j++)
               disp[i][j]='0';
         }
      }
      else if(cmd[0]=='C')
      {
         for(i=0;i<N;i++)
         {
            for(j=0;j<M;j++)
               disp[i][j]='0';
         }
      }
      else if(cmd[0]=='L')
      {
         scanf("%d %d %c",&X,&Y,&C);
         disp[Y-1][X-1]=C;
      }
      else if(cmd[0]=='F')
      {
         scanf("%d %d %c",&X,&Y,&C);
         char tmp=disp[Y-1][X-1];
         funcF(X,Y,C,tmp);
         /*if(tmp==C)
            continue;
         for(i=0;i<N;i++)
         {
            for(j=0;j<M;j++)
            {
               if(disp[i][j]==tmp)
                  disp[i][j]=C;
            }
         }*/
      }
      else if(cmd[0]=='V')
      {
         scanf("%d %d %d %c",&X,&Y1,&Y2,&C);
         if(Y1>Y2)
         {
            int tmp=Y1;
            Y1=Y2;
            Y2=tmp;
         }
         for(i=(Y1-1);i<Y2;i++)
            disp[i][X-1]=C;
      }
      else if(cmd[0]=='H')
      {
         scanf("%d %d %d %c",&X1,&X2,&Y,&C);
         if(X1>X2)
         {
            int tmp=X1;
            X1=X2;
            X2=tmp;
         }
         for(i=(X1-1);i<X2;i++)
            disp[Y-1][i]=C;
      }
      else if(cmd[0]=='K')
      {
         scanf("%d %d %d %d %c",&X1,&Y1,&X2,&Y2,&C);
         if(Y1>Y2)
         {
            int tmp=Y1;
            Y1=Y2;
            Y2=tmp;
         }
         if(X1>X2)
         {
            int tmp=X1;
            X1=X2;
            X2=tmp;
         }
         for(i=(Y1-1);i<Y2;i++)
         {
            for(j=(X1-1);j<X2;j++)
               disp[i][j]=C;
         }
      }
      else if(cmd[0]=='S')
      {
         scanf("%s",name);
         printf("%s\n",name);
         for(i=0;i<N;i++)
         {
            for(j=0;j<M;j++)
               printf("%c",disp[i][j]);
            printf("\n");
         }
      }
      else
      {
         char garbage[1000];
         gets(garbage);
      }
   }
   return 0;
}

void funcF(int X, int Y, char C, char old_C)
{
   if(X<0 || X>M || Y<0 || Y>N || disp[Y-1][X-1]!=old_C || C==old_C)
      return;
   
   disp[Y-1][X-1]=C;
   
   funcF(X,Y-1,C,old_C);   /*up*/
   funcF(X,Y+1,C,old_C);   /*down*/
   funcF(X-1,Y,C,old_C);   /*left*/
   funcF(X+1,Y,C,old_C);   /*right*/
}

adityaarun1
New poster
 
Posts: 3
Joined: Mon Jan 21, 2013 4:22 pm

Re: 10267 - Graphical Editor

Postby brianfry713 » Mon Jan 21, 2013 11:32 pm

white is the letter O not the number zero.
brianfry713
Guru
 
Posts: 1761
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 10267 - Graphical Editor

Postby Fedaykin » Tue Jan 29, 2013 2:09 am

7 WA before AC :oops: . The very last thing I fixed was I had assumed incorrectly that 'X' would be the last input, ie if 'X' was inputted there wouldn't be anything after it, but that was wrong. hope that helps someone
Fedaykin
New poster
 
Posts: 6
Joined: Sun Dec 09, 2012 9:27 pm

Previous

Return to Volume CII

Who is online

Users browsing this forum: No registered users and 0 guests