Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/111/11110/b.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     if (0 < size)
   97         {
   98             /* We have data to proces */
   99             dataReadFlag = TRUE;
  100             init();
  101             for (i=1; size>i; i++)
  102                 {
  103                     /* loop for each partition */
  104                     scanf("%d %d%c", &r, &c, &chr);
  105                     if (NL == chr)
  106                         {
  107                             /* nothing on line */
  108                             start[i][R] = 0;
  109                             start[i][C] = 0;
  110                         } /* nothing on line */
  111                     else
  112                         {
  113                             /* data on line */
  114                             tbl[r][c] = i;
  115                             start[i][R] = r;
  116                             start[i][C] = c;
  117                             while (NL != chr)
  118                                 {
  119                                     /* work way across line */
  120                                     scanf("%d %d%c", &r, &c, &chr);
  121                                     tbl[r][c] = i;
  122                                     start[i][R] = r;
  123                                     start[i][C] = c;
  124                                 } /* work way across line */
  125                         } /* data on line */
  126                 } /* loop for each partition */
  127         } /* We have data to proces */
  128     return (dataReadFlag);
  129 } /* FUNCTION getInput */
  130 
  131 void findZero()
  132 {
  133     /* FUNCTION findZero */
  134     int i;
  135     int j;
  136 
  137     for (i=1; size>=i; i++)
  138         {
  139             /* for each row */
  140             for (j=1; size>=j; j++)
  141                 {
  142                     /* for each column */
  143                     if (UNFILLED == tbl[i][j])
  144                         {
  145                             /* found one of the implied partition */
  146                             start[0][R] = i;
  147                             start[0][C] = j;
  148                             i = size + 1;
  149                             j = size + 1;
  150                         } /* found one of the implied partition */
  151                 } /* for each column */
  152         } /* for each row */
  153 } /* FUNCTION findZero */
  154 
  155 int fill(int row, int col, int part)
  156 {
  157     /* FUNCTION fill */
  158     int ret = 0;
  159 
  160     if (part == tbl[row][col])
  161         {
  162             /* still in partition */
  163             ret = 1;
  164             tbl[row][col] = FILLED;
  165             ret = ret + fill(row - 1, col, part);
  166             ret = ret + fill(row + 1, col, part);
  167             ret = ret + fill(row, col - 1, part);
  168             ret = ret + fill(row, col + 1, part);
  169         } /* still in partition */
  170     return ret;
  171 } /* FUNCTION fill */
  172 
  173 int anyLeft()
  174 {
  175     /* FUNCTION anyLeft */
  176     int i;
  177     int j;
  178     int found = FALSE;
  179 
  180     for (i=1; size>=i; i++)
  181         {
  182             /* for each row */
  183             for (j=1; size>=j; j++)
  184                 {
  185                     /* for each column */
  186                     if (FILLED != tbl[i][j])
  187                         {
  188                             /* found one */
  189                             found = TRUE;
  190                             i = size + 1;
  191                             j = size + 1;
  192                         } /* found one */
  193                 } /* for each column */
  194         } /* for each row */
  195     return found;
  196 } /* FUNCTION anyLeft */
  197 
  198 void process()
  199 {
  200     /* FUNCTION process */
  201     int i;
  202     int valid = TRUE;
  203 
  204     if (1 == size)
  205         {
  206             /* trivial case */
  207             printf("good\n");
  208         } /* trivial case */
  209     else
  210         {
  211             /* normal cases */
  212             DEBUG dump();
  213             findZero();
  214             for (i=0; (vaild) && (size>i); i++)
  215                 {
  216                     /* fill each partition starting at first known member */
  217                     valid = (0 < fill(start[i][R], start[i][C], i));
  218                 } /* fill each partition starting at first known member */
  219             /* if any remaining unfilled spots, then this is wrong */
  220             printf("Answer\n");
  221             if ((!valid) || (anyLeft()))
  222                 {
  223                     /* wrong */
  224                     printf("wrong\n");
  225                 } /* wrong */
  226             else
  227                 {
  228                     /* good */
  229                     printf("good\n");
  230                 } /* good */
  231         } /* normal cases */
  232 } /* FUNCTION process */
  233 
  234 int main()
  235 {
  236     /* main */
  237     int moreToDo;
  238 
  239     init();
  240     moreToDo = getInput();
  241     while (moreToDo)
  242         {
  243             /* while */
  244             process();
  245             moreToDo = getInput();
  246         } /* while */
  247 
  248     return EXIT_SUCCESS;
  249 } /* main */
  250