Computer Programming Contest Preparation

ToolBox - Source for: 7/785/a.c



/home/toolbox/public_html/solutions/7/785/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:2019-10-22
   20  * Purpose: fun
   21  * Problem: 785
   22  */
   23 
   24 
   25 #define MAX_WIDTH 100
   26 #define MAX_HEIGHT 34
   27 #define BLANK ' '
   28 #define LINE '_'
   29 #define NL '\n'
   30 #define ZERO 0
   31 
   32 #define maxx(a,b) ( { a>b ? a :b; } )
   33 
   34 char map[MAX_HEIGHT][MAX_WIDTH];
   35 int lineCnt;
   36 
   37 char contour;
   38 char goal;
   39 
   40 void dump()
   41 {
   42     /* FUNCTION dump */
   43     int i;
   44     int j;
   45 
   46     for (i=0; lineCnt>=i; i++)
   47         {
   48             /* for each row */
   49             for (j=0; ZERO != map[i][j]; j++)
   50                 {
   51                     /* for each col */
   52                     printf("%c", map[i][j]);
   53                 } /* for each col */
   54             printf("\n");
   55         } /* for each row */
   56 } /* FUNCTION dump */
   57 
   58 int getInput()
   59 {
   60     /* FUNCTION getInput */
   61     int i;
   62     int j;
   63     int tmp;
   64     int dataReadFlag;
   65 
   66     lineCnt = 0;
   67     contour = BLANK;
   68 
   69     dataReadFlag = (NULL != fgets(map[lineCnt], MAX_WIDTH-1, stdin));
   70     if (dataReadFlag)
   71         {
   72             /* go ahead and load input case */
   73             while (LINE != map[lineCnt][0])
   74                 {
   75                     /* while */
   76                     tmp = strlen(map[lineCnt]) - 1;
   77                     map[lineCnt][tmp] = ZERO;
   78                     DEBUG printf("%d:%s:\n", lineCnt, map[lineCnt]);
   79                     /* find contour character */
   80                     for (i=0; (BLANK == contour) && (tmp>i); i++)
   81                         {
   82                             /* for */
   83                             if (BLANK != map[lineCnt][i])
   84                                 {
   85                                     /* found first contour char */
   86                                     contour = map[lineCnt][i];
   87                                 } /* found first contour char */
   88                         } /* for */
   89                     lineCnt++;
   90                     fgets(map[lineCnt], MAX_WIDTH-1, stdin);
   91                 } /* while */
   92             tmp = strlen(map[lineCnt]) - 1;
   93             map[lineCnt][tmp] = ZERO;
   94         } /* go ahead and load input case */
   95     return dataReadFlag;
   96 } /* FUNCTION getInput */
   97 
   98 int check(int row, int col)
   99 {
  100     /* FUNCTION check */
  101     int ret;
  102     int sln;
  103 
  104     sln = strlen(map[row]);
  105     ret = ((0<row) && (0<col) && (sln>col) && (lineCnt>row) && (BLANK == map[row][col]));
  106     return ret;
  107 } /* FUNCTION check */
  108 
  109 void fill(int row, int col, char mrk)
  110 {
  111     /* FUNCTION fill */
  112     int sln;
  113 
  114     DEBUG printf("(row %d) (col %d)  (map %c) (mark %c)\n", row, col, map[row][col], mrk);
  115     sln = strlen(map[row]);
  116     if ((0<row) && (0<col) && (sln>col) && (lineCnt>row) && (mrk == map[row][col]))
  117         {
  118             /* found a valid spot */
  119             map[row][col] = mrk;
  120             if (check(row+1, col))
  121                 {
  122                     map[row+1][col] = mrk;
  123                     fill(row+1, col, mrk);
  124                 }
  125             if (check(row-1, col))
  126                 {
  127                     map[row-1][col] = mrk;
  128                     fill(row-1, col, mrk);
  129                 }
  130             if (check(row, col+1))
  131                 {
  132                     map[row][col+1] = mrk;
  133                     fill(row, col+1, mrk);
  134                 }
  135             if (check(row, col-1))
  136                 {
  137                     map[row][col-1] = mrk;
  138                     fill(row, col-1, mrk);
  139                 }
  140         } /* found a valid spot */
  141 } /* FUNCTION fill */
  142 
  143 void process()
  144 {
  145     /* FUNCTION process */
  146     int i;
  147     int j;
  148     int sln;
  149 
  150     DEBUG dump();
  151     for (i=1; lineCnt>i; i++)
  152         {
  153             /* for each line */
  154             sln = strlen(map[i]);
  155             for (j=1; sln>j; j++)
  156                 {
  157                     /* for each column */
  158                     if ((BLANK != map[i][j]) && (contour != map[i][j]))
  159                         {
  160                             /* found a mark char */
  161                             fill(i, j, map[i][j]);
  162                         } /* found a mark char */
  163                 } /* for each column */
  164         } /* for each line */
  165     dump();
  166 } /* FUNCTION process */
  167 
  168 int main()
  169 {
  170     /* main */
  171     int moreToDo;
  172 
  173     moreToDo = getInput();
  174     while (moreToDo)
  175         {
  176             /* while */
  177             process();
  178             moreToDo = getInput();
  179         } /* while */
  180 
  181     return EXIT_SUCCESS;
  182 } /* main */
  183 
  184