Computer Programming Contest Preparation

ToolBox - Source for: 109/10946/a.c



/home/toolbox/public_html/solutions/109/10946/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2018-03-18
   20  * Purpose: fun
   21  * Problem: 10946 - You want What Filled?
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define LETTERS 28
   29 #define MAX_HOLES ((50 * 50) + 2)
   30 #define MAX_DIM 53
   31 #define LAND '.'
   32 #define MISSING LAND
   33 #define FIRST ('A' - 1)
   34 
   35 typedef struct HOLE_STRUCT
   36 {
   37     /* HOLE_STRUCT */
   38     char label;
   39     int cnt;
   40 } /* HOLE_STRUCT */
   41 HOLE_STRUCT;
   42 
   43 char grid[MAX_DIM][MAX_DIM];
   44 HOLE_STRUCT  holes[MAX_HOLES];
   45 int  numHoles;
   46 int  Roff[4] = { 0, 1,  0, -1};
   47 int  Coff[4] = { 1, 0, -1,  0};
   48 
   49 int x;
   50 int y;
   51 
   52 void init()
   53 {
   54     /* FUNCTION init */
   55 } /* FUNCTION init */
   56 
   57 void dump()
   58 {
   59     /* FUNCTION dump */
   60 } /* FUNCTION dump */
   61 
   62 int getInput()
   63 {
   64     /* FUNCTION getInput */
   65     int i;
   66     int j;
   67     int tmp;
   68     int moreToDo;
   69 
   70     scanf(" %d %d ", &x, &y);
   71     moreToDo = ((0 != x) || (0 != y));
   72     if (moreToDo)
   73         {
   74             /* more data -- load grid */
   75             numHoles = 0;
   76             for (i=1; x>=i; i++)
   77                 {
   78                     /* for each row */
   79                     scanf(" %s ", &grid[i][1]);
   80                     grid[i][0] = MISSING;
   81                     grid[i][y+1] = MISSING;
   82                 } /* for each row */
   83             for (j=0; (x+2)>j; j++)
   84                 {
   85                     /* load the 2 guard rows */
   86                     grid[0][j] = MISSING;
   87                     grid[x+1][j] = MISSING;
   88                 } /* load the 2 guard rows */
   89         } /* more data -- load grid */
   90     return moreToDo;
   91 } /* FUNCTION getInput */
   92 
   93 int compareHoles(HOLE_STRUCT *a, HOLE_STRUCT *b)
   94 {
   95     /* FUNCTION compareHoles */
   96     int toReturn;
   97 
   98     if (a->cnt == b->cnt)
   99         {
  100             /* if same count -- then resolve via letter */
  101             toReturn = (a->label - b->label);
  102         } /* if same count -- then resolve via letter */
  103     else
  104         {
  105             /* settle it by count */
  106             toReturn = (b->cnt - a->cnt);
  107         } /* settle it by count */
  108     return toReturn;
  109 } /* FUNCTION compareHoles */
  110 
  111 int fill(int r, int c, char lt)
  112 {
  113     /* FUNCTION fill */
  114     int i;
  115     int cnt = 1;
  116 
  117     grid[r][c] = MISSING;
  118     for (i=0; 4>i; i++)
  119         {
  120             /* for each neighbor */
  121             if (lt == grid[r + Roff[i]][c + Coff[i]])
  122                 {
  123                     /* match found */
  124                     cnt = cnt + fill(r + Roff[i], c + Coff[i], lt);
  125                 } /* match found */
  126         } /* for each neighbor */
  127     return cnt;
  128 } /* FUNCTION fill */
  129 
  130 void process()
  131 {
  132     /* FUNCTION process */
  133     int i;
  134     int j;
  135     int l;
  136     char lt;
  137     int mx;
  138     int tmp;
  139 
  140     for (i=1; x>=i; i++)
  141         {
  142             /* for each row */
  143             for (j=1; y>=j; j++)
  144                 {
  145                     /* for each column */
  146                     if (MISSING != grid[i][j])
  147                         {
  148                             /* found a hole */
  149                             lt = grid[i][j];
  150                             holes[numHoles].label = lt;
  151                             holes[numHoles].cnt = fill(i, j, lt);
  152                             numHoles++;
  153                         } /* found a hole */
  154                 } /* for each column */
  155         } /* for each row */
  156     /* sort by count and then letter */
  157     qsort(holes, numHoles, sizeof(HOLE_STRUCT), compareHoles);
  158     /* dump results */
  159     for (i=0; numHoles>i; i++)
  160         {
  161             /* while */
  162             printf("%c %d\n", holes[i].label, holes[i].cnt);
  163         } /* while */
  164 } /* FUNCTION process */
  165 
  166 int main()
  167 {
  168     /* main */
  169     int i = 1;
  170     int moreToDo;
  171 
  172     init();
  173     moreToDo = getInput();
  174     while (moreToDo)
  175         {
  176             /* while */
  177             printf("Problem %d:\n", i++);
  178             process();
  179             moreToDo = getInput();
  180         } /* while */
  181 
  182     return EXIT_SUCCESS;
  183 } /* main */
  184 
  185