Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/102/10267/b.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 fill(int x, int y, char buffer[][MAXHEIGHT], int M, int N, char save)
  153 {
  154     // FUNCTION BEGIN fillArea
  155     if ((x>0) && (x<=M) && (y>0) && (y<=N) && (save == buffer[y][x]))
  156         {
  157             // if
  158             buffer[y][x] = '.';
  159             fill(x, y+1, buffer, M, N, save);
  160             fill(x, y-1, buffer, M, N, save);
  161             fill(x+1, y, buffer, M, N, save);
  162             fill(x-1, y, buffer, M, N, save);
  163         } // if
  164 } // FUNCTION END fillArea
  165 
  166 void fillArea(int x, int y, char buffer[][MAXHEIGHT], int M, int N, char color)
  167 {
  168     // FUNCTION BEGIN fillArea
  169     int i;
  170     int j;
  171     int changes;
  172     char save;
  173     int cnt = 0;
  174     save = buffer[y][x];
  175     fill(x, y, buffer, M, N, save);
  176     // reset the '.' to desired color
  177     for (j=1; j<=M; j++)
  178         for (i=1; i<=N; i++)
  179             if ('.' == buffer[i][j]) buffer[i][j] = color;
  180     cnt++;
  181 //    printf("Iterations: %d\n", cnt);
  182 } // FUNCTION END fillArea
  183 
  184 int process(COMMAND_STRUCT c, char  buffer[][MAXHEIGHT], int *M, int *N)
  185 {
  186     // BEGIN FUNCTION process
  187     int i,j;
  188 //   dump(c);
  189     switch (c.cmd)
  190         {
  191         // switch
  192         case 'I':
  193 //       for (i=0; i<MAXHEIGHT; i++)
  194 //          for (j=0; j<MAXWIDTH; j++)
  195 //             buffer[j][i] = '*';
  196             *M = c.x1;
  197             *N = c.y1;
  198             fillRect(1, 1, *M, *N, buffer, 'O');
  199             break;
  200         case 'C':
  201             fillRect(1, 1, *M, *N, buffer, 'O');
  202             break;
  203         case 'L':
  204             buffer[c.y1][c.x1] = c.color;
  205             break;
  206         case 'F':
  207             fillArea(c.x1, c.y1, buffer, *M, *N, c.color);
  208             break;
  209         case 'V':
  210             fillVert(c.x1, c.y1, c.y2, buffer, c.color);
  211             break;
  212         case 'H':
  213             fillHorz(c.x1, c.x2, c.y1, buffer, c.color);
  214             break;
  215         case 'K':
  216             fillRect(c.x1, c.y1, c.x2, c.y2, buffer, c.color);
  217             break;
  218         case 'S':
  219             dumpBuffer(buffer, *M, *N, c.name);
  220             break;
  221         } // switch
  222 } // END FUNCTION process
  223 
  224 int main ()
  225 {
  226     // main
  227     int moreToDo;
  228     COMMAND_STRUCT c;
  229     char buffer[MAXWIDTH][MAXHEIGHT];
  230     int M;
  231     int N;
  232 
  233     // init();
  234     moreToDo = getInput(&c);
  235     while (moreToDo)
  236         {
  237             // while
  238             process(c, buffer, &M, &N);
  239             moreToDo = getInput(&c);
  240         } // while
  241 
  242     return 1;
  243 } // main
  244