Computer Programming Contest Preparation

ToolBox - Source for: 6/657/a.c



/home/toolbox/public_html/solutions/6/657/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 20190924
   18  * Purpose: fun
   19  * Problem: 657 (floodfill)
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_LINE 75
   27 #define MAX_READ (MAX_LINE - 4)
   28 #define MAX_DIE_VALUES 7
   29 #define BACKGROUND '.'
   30 #define DIE '*'
   31 #define DIENEW '#'
   32 #define DOT 'X'
   33 #define DOTTMP 'a'
   34 #define DOTNEW 'z'
   35 
   36 int w;  /* width of input */
   37 int h;  /* height of input */
   38 char pic[MAX_LINE][MAX_LINE];
   39 int dieValues[7];
   40 int dies;
   41 
   42 void init()
   43 {
   44     /* FUNCTION init */
   45 } /* FUNCTION init */
   46 
   47 void dump()
   48 {
   49     /* FUNCTION dump */
   50     int i;
   51     int j;
   52 
   53     printf("row = %d   Column = %d\n", h, w);
   54     for (i=0; (h+2)>i; i++)
   55         {
   56             /* each row */
   57             printf("%2d: ", i);
   58             for (j=0; (w+2)>j; j++)
   59                 {
   60                     /* for each column */
   61                     printf("%c", pic[i][j]);
   62                 } /* for each column */
   63             printf("\n");
   64         } /* each row */
   65 } /* FUNCTION dump */
   66 
   67 void dumpDies()
   68 {
   69     /* FUNCTION dumpDies */
   70     int i;
   71     int j;
   72     int first = TRUE;
   73 
   74     for (i=1; MAX_DIE_VALUES>i; i++)
   75         {
   76             /* for each possible value */
   77             for (j=0; dieValues[i]>j; j++)
   78                 {
   79                     /* for each die value */
   80                     if (first)
   81                         {
   82                             /* first item on line */
   83                             printf("%d", i);
   84                             first = FALSE;
   85                         } /* first item on line */
   86                     else
   87                         {
   88                             /* all of rest need preceding blank */
   89                             printf(" %d", i);
   90                         } /* all of rest need preceding blank */
   91                 } /* for each die value */
   92         } /* for each possible value */
   93     printf("\n");
   94 } /* FUNCTION dumpDies */
   95 
   96 int getInput()
   97 {
   98     /* FUNCTION getInput */
   99     int dataReadFlag;
  100     int i;
  101 
  102     scanf(" %d %d ", &w, &h);
  103 
  104     dataReadFlag = (0 != w);
  105     if (dataReadFlag)
  106         {
  107             /* valid width and height found -- get picture */
  108             for (i=1; h>=i; i++)
  109                 {
  110                     /* for each row in the image */
  111                     fgets(&pic[i][1], MAX_READ, stdin);
  112                     pic[i][0] = BACKGROUND;  /* mark border at beginning of line */
  113                     pic[i][w+1] = BACKGROUND; /* mark border at end of line */
  114                     pic[i][w+2] = 0;
  115                 } /* for each row in the image */
  116             for (i=0; (w+2)>i; i++)
  117                 {
  118                     /* blank out each top and bottom row */
  119                     pic[0][i] = BACKGROUND;
  120                     pic[h+1][i] = BACKGROUND;
  121                 } /* blank out each top and bottom row */
  122         } /* valid width and height found -- get picture */
  123     return (dataReadFlag);
  124 } /* FUNCTION getInput */
  125 
  126 void fillDot(int x, int y)
  127 {
  128     /* FUNCTION fillDot */
  129     /* one a dot pixel is found, this will remove it and recurse through all of its neighbors */
  130     if (DOT == pic[x][y])
  131         {
  132             /* found some of the dot */
  133             DEBUG printf("fillDot: (x %d) (y %d)\n", x, y);
  134             pic[x][y] = DOTTMP;
  135             fillDot(x-1, y);
  136             fillDot(x+1, y);
  137             fillDot(x, y-1);
  138             fillDot(x, y+1);
  139         } /* found some of the dot */
  140 } /* FUNCTION fillDot */
  141 
  142 int tryAdjacent(int x, int y)
  143 {
  144     /* FUNCTION tryAdjacnet */
  145     int cnt = 0;
  146     cnt = findDots(x-1, y);
  147     cnt = cnt + findDots(x+1, y);
  148     cnt = cnt + findDots(x, y-1);
  149     cnt = cnt + findDots(x, y+1);
  150     return cnt;
  151 } /* FUNCTION tryAdjacnet */
  152 
  153 int findDots(int x, int y)
  154 {
  155     /* FUNCTION findDots */
  156     int cnt = 0;
  157 
  158     DEBUG printf("findDots: pic[%d][%d] = %c\n", x, y, pic[x][y]);
  159     switch (pic[x][y])
  160         {
  161             /* possible pixel values */
  162         case BACKGROUND:
  163         case DIENEW:
  164             break;
  165         case DOTNEW:
  166             break;
  167         case DOTTMP:
  168             pic[x][y] = DOTNEW;
  169             cnt = cnt + tryAdjacent(x,y);
  170             break;
  171         case DIE:
  172             pic[x][y] = DIENEW; /* mark it as found */
  173             cnt = cnt + tryAdjacent(x,y);
  174             break;
  175         case DOT:
  176             cnt++;
  177             fillDot(x,y);
  178             cnt = cnt + tryAdjacent(x, y);
  179             break;
  180         default:
  181             printf("invalid pixel found: pic[%d][%d] = %c\n", x, y, pic[x][y]);
  182             break;
  183         } /* possible pixel values */
  184     return cnt;
  185 } /* FUNCTION findDots */
  186 
  187 void process()
  188 {
  189     /* FUNCTION process */
  190     int i;
  191     int j;
  192     int tmp;
  193 
  194     DEBUG dump();
  195     for (i=0; MAX_DIE_VALUES>i; i++)
  196         {
  197             dieValues[i] = 0;    /* zero out counts */
  198         }
  199     for (i=1; h>=i; i++)
  200         {
  201             /* for each row */
  202             for (j=1; w>=j; j++)
  203                 {
  204                     /* for each Column */
  205                     if ((DIE == pic[i][j]) || (DOT == pic[i][j]))
  206                         {
  207                             /* found another die */
  208                             tmp = findDots(i, j);
  209                             DEBUG printf("(tmp %d)\n", tmp);
  210                             dieValues[tmp] = dieValues[tmp] + 1;
  211                         } /* found another die */
  212                 } /* for each Column */
  213         } /* for each row */
  214     DEBUG dump();
  215     dumpDies();
  216     printf("\n");
  217 } /* FUNCTION process */
  218 
  219 int main()
  220 {
  221     /* main */
  222     int moreToDo;
  223     int throw = 1;
  224 
  225     init();
  226     moreToDo = getInput();
  227     while (moreToDo)
  228         {
  229             /* while */
  230             printf("Throw %d\n", throw);
  231             throw++;
  232             process();
  233             moreToDo = getInput();
  234         } /* while */
  235 
  236     return EXIT_SUCCESS;
  237 } /* main */
  238