Computer Programming Contest Preparation

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



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