Computer Programming Contest Preparation

ToolBox - Source for: 111/11110/close.c



/home/toolbox/public_html/solutions/111/11110/close.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: 2019-10-23
   18  * Purpose: fun
   19  * Problem: 11110
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_DIM 105
   27 #define UNFILLED 0
   28 #define FILLED 111
   29 #define IGNORE 112
   30 #define R 0
   31 #define C 1
   32 
   33 int size;
   34 int tbl[MAX_DIM][MAX_DIM];
   35 int start[MAX_DIM][2];
   36 
   37 void init()
   38 {
   39     /* FUNCTION init */
   40     int i;
   41     int j;
   42 
   43     for (i=0; (1+size)>=i; i++)
   44         {
   45             /* all of the outside of the matrix */
   46             tbl[0][i] = IGNORE;
   47             tbl[1+size][i] = IGNORE;
   48             tbl[i][0] = IGNORE;
   49             tbl[i][1+size] = IGNORE;
   50         } /* all of the outside of the matrix */
   51     for (i=1; size>=i; i++)
   52         {
   53             /* for each row */
   54             for (j=1; size>=j; j++)
   55                 {
   56                     /* for each column */
   57                     tbl[i][j] = UNFILLED;
   58                 } /* for each column */
   59         } /* for each row */
   60 } /* FUNCTION init */
   61 
   62 void dump()
   63 {
   64     /* FUNCTION dump */
   65     int i;
   66     int j;
   67 
   68     for (i=0; (1+size)>=i; i++)
   69         {
   70             /* for each row */
   71             for (j=0; (1+size)>=j; j++)
   72                 {
   73                     /* for each column */
   74                     printf("%3d ", tbl[i][j]);
   75                 } /* for each column */
   76             printf("\n");
   77         } /* for each row */
   78 } /* FUNCTION dump */
   79 
   80 int getInput()
   81 {
   82     /* FUNCTION getInput */
   83     int dataReadFlag = FALSE;
   84     int i;
   85     int j;
   86     int r;
   87     int c;
   88 
   89     scanf(" %d ", &size);
   90     if (0 < size)
   91         {
   92             /* We have data to proces */
   93             dataReadFlag = TRUE;
   94             init();
   95             for (i=1; size>i; i++)
   96                 {
   97                     /* loop for each partition */
   98                     scanf(" %d %d ", &r, &c);
   99                     tbl[r][c] = i;
  100                     start[i][R] = r;
  101                     start[i][C] = c;
  102                     for (j=1; size>j; j++)
  103                         {
  104                             /* loop for each pair of coordinates */
  105                             scanf(" %d %d ", &r, &c);
  106                             tbl[r][c] = i;
  107                         } /* loop for each pair of coordinates */
  108                 } /* loop for each partition */
  109         } /* We have data to proces */
  110     return (dataReadFlag);
  111 } /* FUNCTION getInput */
  112 
  113 void findZero()
  114 {
  115     /* FUNCTION findZero */
  116     int i;
  117     int j;
  118 
  119     for (i=1; size>=i; i++)
  120         {
  121             /* for each row */
  122             for (j=1; size>=j; j++)
  123                 {
  124                     /* for each column */
  125                     if (UNFILLED == tbl[i][j])
  126                         {
  127                             /* found one of the implied partition */
  128                             start[0][R] = i;
  129                             start[0][C] = j;
  130                             i = size + 1;
  131                             j = size + 1;
  132                         } /* found one of the implied partition */
  133                 } /* for each column */
  134         } /* for each row */
  135 } /* FUNCTION findZero */
  136 
  137 void fill(int row, int col, int part)
  138 {
  139     /* FUNCTION fill */
  140     if (part == tbl[row][col])
  141         {
  142             /* still in partition */
  143             tbl[row][col] = FILLED;
  144             fill(row - 1, col, part);
  145             fill(row + 1, col, part);
  146             fill(row, col - 1, part);
  147             fill(row, col + 1, part);
  148         } /* still in partition */
  149 } /* FUNCTION fill */
  150 
  151 int anyLeft()
  152 {
  153     /* FUNCTION anyLeft */
  154     int i;
  155     int j;
  156     int found = FALSE;
  157 
  158     for (i=1; size>=i; i++)
  159         {
  160             /* for each row */
  161             for (j=1; size>=j; j++)
  162                 {
  163                     /* for each column */
  164                     if (FILLED != tbl[i][j])
  165                         {
  166                             /* found one */
  167                             found = TRUE;
  168                             i = size + 1;
  169                             j = size + 1;
  170                         } /* found one */
  171                 } /* for each column */
  172         } /* for each row */
  173     return found;
  174 } /* FUNCTION anyLeft */
  175 
  176 void process()
  177 {
  178     /* FUNCTION process */
  179     int i;
  180 
  181     if (1 == size)
  182         {
  183             /* trivial case */
  184             printf("good\n");
  185         } /* trivial case */
  186     else
  187         {
  188             /* normal cases */
  189             DEBUG dump();
  190             findZero();
  191             for (i=0; size>i; i++)
  192                 {
  193                     /* fill each partition starting at first known member */
  194 
  195                     DEBUG printf("fill %d: %d,%d = %d\n", i, start[i][R], start[i][C], tbl[start[i][R]][start[i][C]]);
  196                     fill(start[i][R], start[i][C], tbl[start[i][R]][start[i][C]]);
  197                 } /* fill each partition starting at first known member */
  198             /* if any remaining unfilled spots, then this is wrong */
  199             printf("Answer\n");
  200             if (anyLeft())
  201                 {
  202                     /* wrong */
  203                     printf("wrong\n");
  204                 } /* wrong */
  205             else
  206                 {
  207                     /* good */
  208                     printf("good\n");
  209                 } /* good */
  210         } /* normal cases */
  211 } /* FUNCTION process */
  212 
  213 int main()
  214 {
  215     /* main */
  216     int moreToDo;
  217 
  218     init();
  219     moreToDo = getInput();
  220     while (moreToDo)
  221         {
  222             /* while */
  223             process();
  224             moreToDo = getInput();
  225         } /* while */
  226 
  227     return EXIT_SUCCESS;
  228 } /* main */
  229