Computer Programming Contest Preparation

ToolBox - Source for: 102/10267/j.c



/home/toolbox/public_html/solutions/102/10267/j.c
    1 #include <stdio.h>
    2 
    3 /* @JUDGE_ID: 47124AK  10267  C  */
    4 
    5 
    6 #define TRUE  (1 == 1)
    7 #define FALSE (1 != 1)
    8 #define NAMEMAX 25
    9 #define MAXWIDTH 305
   10 #define MAXHEIGHT 305
   11 #define SIZE 255
   12 
   13 typedef struct COMMAND_STRUCT
   14 {
   15     /* struct COMMAND_STRUCT */
   16     char cmd;
   17     int  x1;
   18     int  x2;
   19     int  y1;
   20     int  y2;
   21     char color;
   22     char name[NAMEMAX];
   23 } /* struct COMMAND_STRUCT */
   24 COMMAND_STRUCT;
   25 
   26 void dump(COMMAND_STRUCT c)
   27 {
   28     /* FUNCTION BEGIN dump */
   29     printf("%c %d %d %d %d %c - [%s]\n", c.cmd, c.x1, c.x2, c.y1, c.y2, c.color, c.name);
   30 } /* FUNCTION END dump */
   31 
   32 int getInput(COMMAND_STRUCT *c)
   33 {
   34     /* BEGIN FUNCTION getInput */
   35     char x;
   36 
   37     scanf("%c ", &x);
   38     (*c).cmd = x;
   39     switch (x)
   40         {
   41         /* switch */
   42         case 'C':
   43             (*c).color='O';
   44         case 'X':
   45             break;
   46         case 'I':
   47             /* get Max X (max cols) */
   48             scanf("%d ",&(*c).x1);
   49             /* get Max Y (max rows) */
   50             scanf("%d ",&(*c).y1);
   51             (*c).color='O';
   52             break;
   53         case 'L':
   54         case 'F':
   55             /* get x1 */
   56             scanf("%d ",&(*c).x1);
   57             /* get y1 */
   58             scanf("%d ",&(*c).y1);
   59             /* get color */
   60             scanf("%c ",&(*c).color);
   61             break;
   62         case 'V':
   63             /* get x1 */
   64             scanf("%d ",&(*c).x1);
   65             /* get y1 */
   66             scanf("%d ",&(*c).y1);
   67             /* get y2 */
   68             scanf("%d ",&(*c).y2);
   69             /* get color */
   70             scanf("%c ",&(*c).color);
   71             break;
   72         case 'H':
   73             /* get x1 */
   74             scanf("%d ",&(*c).x1);
   75             /* get x2 */
   76             scanf("%d ",&(*c).x2);
   77             /* get y1 */
   78             scanf("%d ",&(*c).y1);
   79             /* get color */
   80             scanf("%c ",&(*c).color);
   81             break;
   82         case 'K':
   83             /* get x1 */
   84             scanf("%d ",&(*c).x1);
   85             /* get y1 */
   86             scanf("%d ",&(*c).y1);
   87             /* get x2 */
   88             scanf("%d ",&(*c).x2);
   89             /* get y2 */
   90             scanf("%d ",&(*c).y2);
   91             /* get color */
   92             scanf("%c ",&(*c).color);
   93             break;
   94         case 'S':
   95             /* get name */
   96             scanf("%s ",(*c).name);
   97             break;
   98         default :
   99             (*c).cmd = 'Z';
  100             while ('\n' != getc(stdin));
  101         } /* switch */
  102     return ('X' != x);
  103 } /* END FUNCTION getInput */
  104 
  105 void fillHorz(int x1, int x2, int y1, char buffer[][MAXHEIGHT], char color)
  106 {
  107     /* FUNCTION BEGIN fillVert */
  108     int j;
  109 
  110     for (j=x1; j<=x2; j++)
  111         buffer[j][y1] = color;
  112 } /* FUNCTION END fillVert */
  113 
  114 void fillVert(int x1, int y1, int y2, char buffer[][MAXHEIGHT], char color)
  115 {
  116     /* FUNCTION BEGIN fillVert */
  117     int i;
  118 
  119     for (i=y1; i<=y2; i++)
  120         buffer[x1][i] = color;
  121 } /* FUNCTION END fillVert */
  122 
  123 void fillRect(int x1, int y1, int x2, int y2, char buffer[][MAXHEIGHT], char color)
  124 {
  125     /* FUNCTION BEGIN fillRect */
  126     int j;
  127 
  128     for (j=x1; j<=x2; j++)
  129         fillVert(j, y1, y2, buffer, color);
  130 } /* FUNCTION END fillRect */
  131 
  132 void dumpBuffer(char buffer[][MAXHEIGHT], int M, int N, char name[])
  133 {
  134     /* FUNCTION BEGIN dumpBuffer */
  135     int i;
  136     int j;
  137 
  138     printf("%s\n", name);
  139     for (j=1; j<=N; j++)
  140         {
  141             /* for j */
  142             for (i=1; i<=M; i++)
  143                 printf("%c", buffer[i][j]);
  144             printf("\n");
  145         } /* for j */
  146 } /* FUNCTION BEGIN dumpBuffer */
  147 
  148 void fill(int x, int y, char buffer[][MAXHEIGHT], int M, int N, char save)
  149 {
  150     /* FUNCTION BEGIN fillArea */
  151     if ((x>0) && (x<=M) && (y>0) && (y<=N) && (save == buffer[x][y]))
  152         {
  153             /* if */
  154             buffer[x][y] = '.';
  155             fill(x, y+1, buffer, M, N, save);
  156             fill(x, y-1, buffer, M, N, save);
  157             fill(x+1, y, buffer, M, N, save);
  158             fill(x-1, y, buffer, M, N, save);
  159         } /* if */
  160 } /* FUNCTION END fillArea */
  161 
  162 void fillArea(int x, int y, char buffer[][MAXHEIGHT], int M, int N, char color)
  163 {
  164     /* FUNCTION BEGIN fillArea */
  165     int i;
  166     int j;
  167     int changes;
  168     char save;
  169     int cnt = 0;
  170     save = buffer[x][y];
  171     fill(x, y, buffer, M, N, save);
  172     /* reset the '.' to desired color */
  173     for (j=1; j<=M; j++)
  174         for (i=1; i<=N; i++)
  175             if ('.' == buffer[j][i]) buffer[j][i] = color;
  176     cnt++;
  177     /*    printf("Iterations: %d\n", cnt); */
  178 } /* FUNCTION END fillArea */
  179 
  180 int process(COMMAND_STRUCT c, char  buffer[][MAXHEIGHT], int *M, int *N)
  181 {
  182     /* BEGIN FUNCTION process */
  183     int i,j;
  184     /*   dump(c); */
  185     switch (c.cmd)
  186         {
  187         /* switch */
  188         case 'I':
  189             /*       for (i=0; i<MAXHEIGHT; i++) */
  190             /*          for (j=0; j<MAXWIDTH; j++) */
  191             /*             buffer[j][i] = '*'; */
  192             *M = c.x1;
  193             *N = c.y1;
  194             fillRect(1, 1, *M, *N, buffer, 'O');
  195             break;
  196         case 'C':
  197             fillRect(1, 1, *M, *N, buffer, 'O');
  198             break;
  199         case 'L':
  200             buffer[c.x1][c.y1] = c.color;
  201             break;
  202         case 'F':
  203             fillArea(c.x1, c.y1, buffer, *M, *N, c.color);
  204             break;
  205         case 'V':
  206             fillVert(c.x1, c.y1, c.y2, buffer, c.color);
  207             break;
  208         case 'H':
  209             fillHorz(c.x1, c.x2, c.y1, buffer, c.color);
  210             break;
  211         case 'K':
  212             fillRect(c.x1, c.y1, c.x2, c.y2, buffer, c.color);
  213             break;
  214         case 'S':
  215             dumpBuffer(buffer, *M, *N, c.name);
  216             break;
  217         } /* switch */
  218 } /* END FUNCTION process */
  219 
  220 int main ()
  221 {
  222     /* main */
  223     int moreToDo;
  224     COMMAND_STRUCT c;
  225     char buffer[MAXWIDTH][MAXHEIGHT];
  226     int M;
  227     int N;
  228 
  229     /* init(); */
  230     moreToDo = getInput(&c);
  231     while (moreToDo)
  232         {
  233             /* while */
  234             process(c, buffer, &M, &N);
  235             moreToDo = getInput(&c);
  236         } /* while */
  237 
  238     return 1;
  239 } /* main */
  240