Computer Programming Contest Preparation

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



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