Computer Programming Contest Preparation

ToolBox - Source for: 7/782/close.c



/home/toolbox/public_html/solutions/7/782/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 <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-05
   20  * Purpose: fun
   21  * Problem: 782
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAX_WIDTH 100
   29 #define MAX_HEIGHT 33
   30 #define BLANK ' '
   31 #define GOAL '*'
   32 #define SET '#'
   33 #define LINE '_'
   34 #define NL '\n'
   35 #define ZERO 0
   36 
   37 #define maxx(a,b) ( { a>b ? a :b; } )
   38 
   39 int numberOfTimes;
   40 char map[MAX_HEIGHT][MAX_WIDTH];
   41 int lineCnt;
   42 int goalCol;
   43 int goalRow;
   44 char chr;
   45 int maxWidth;
   46 char divider[MAX_WIDTH];
   47 
   48 void init()
   49 {
   50     /* FUNCTION init */
   51     fgets(divider, MAX_WIDTH-1, stdin);
   52     sscanf(divider, " %d ", &numberOfTimes);
   53 } /* FUNCTION init */
   54 
   55 void init1()
   56 {
   57     /* FUNCTION init1 */
   58     int i;
   59     int j;
   60 
   61     for (i=0; MAX_HEIGHT>i; i++)
   62         {
   63             /* for i */
   64             for (j=0; MAX_WIDTH>j; j++)
   65                 {
   66                     /* for j */
   67                     map[i][j] = BLANK;
   68                 } /* for j */
   69         } /* for i */
   70     lineCnt = 1;
   71     maxWidth=0;
   72 } /* FUNCTION init1 */
   73 
   74 void dump()
   75 {
   76     /* FUNCTION dump */
   77     int i;
   78     int j;
   79 
   80     DEBUG printf("Contour char [%c], start is at (%d, %d)\n", chr, goalCol, goalRow);
   81     for (i=1; lineCnt>i; i++)
   82         {
   83             /* for each row */
   84             DEBUG printf("|");
   85             for (j=2; ZERO != map[i][j]; j++)
   86                 {
   87                     /* for each col */
   88                     printf("%c", map[i][j]);
   89                 } /* for each col */
   90             DEBUG printf("|");
   91             printf("\n");
   92         } /* for each row */
   93 } /* FUNCTION dump */
   94 
   95 void getInput()
   96 {
   97     /* FUNCTION getInput */
   98     int i;
   99     int j;
  100 
  101     init1();
  102     chr = GOAL;
  103     maxWidth = 0;
  104     fgets(&map[lineCnt][2], MAX_WIDTH-1, stdin);
  105     while (LINE != map[lineCnt][2])
  106         {
  107             /* while */
  108             for (i=0; MAX_WIDTH>i; i++)
  109                 {
  110                     /* for */
  111                     switch (map[lineCnt][i])
  112                         {
  113                             /* switch */
  114                         case GOAL:
  115                             goalCol=i;
  116                             goalRow=lineCnt;
  117                             break;
  118                         case BLANK:
  119                             break;
  120                         case NL:
  121                             map[lineCnt][i] = BLANK;
  122                             break;
  123                         case ZERO:
  124                             map[lineCnt][i] = BLANK;
  125                             maxWidth=maxx(maxWidth, i);
  126                             i = MAX_WIDTH;
  127                             break;
  128                         default:
  129                             chr = map[lineCnt][i];
  130                             break;
  131                         } /* switch */
  132                     DEBUG printf("[%c]\n", map[lineCnt][i]);
  133                 } /* for */
  134             DEBUG printf("!  maxWidth = %d\n", maxWidth);
  135 
  136             lineCnt++;
  137             fgets(&map[lineCnt][2], MAX_WIDTH-1, stdin);
  138         } /* while */
  139     /* save the divider line to output */
  140     for (i=2; ((BLANK == map[lineCnt][i]) || (LINE == map[lineCnt][i])); i++)
  141         {
  142             /* copy line */
  143             divider[i-2] = map[lineCnt][i];
  144         } /* copy line */
  145     divider[i-2] = ZERO;
  146     /* finished with divider */
  147     maxWidth=maxWidth+2;
  148     for (i=0; lineCnt>=i; i++)
  149         {
  150             /* make all lines same length */
  151             for (j=0; ZERO!=map[i][j]; j++) ; /* skip forward until end of line */
  152             while (maxWidth>j)
  153                 {
  154                     /* blank fill end of line */
  155                     map[i][j] = BLANK;
  156                     j++;
  157                 } /* blank fill end of line */
  158             map[i][maxWidth] = ZERO;
  159         } /* make all lines same length */
  160 } /* FUNCTION getInput */
  161 
  162 void clean()
  163 {
  164     /* FUNCTION clean */
  165     int i;
  166     int j;
  167 
  168     for (i=1; lineCnt>i; i++)
  169         {
  170             /* for each row */
  171             for (j=1; maxWidth>j; j++)
  172                 {
  173                     /* each column */
  174                     if ((SET == map[i][j]) &&
  175                             (chr != map[i-1][j]) &&
  176                             (chr != map[i+1][j]) &&
  177                             (chr != map[i][j-1]) &&
  178                             (chr != map[i][j+1]))
  179                         {
  180                             map[i][j] = BLANK;
  181                         }
  182                 } /* each column */
  183         } /* for each row */
  184 } /* FUNCTION clean */
  185 
  186 void fill1(int row, int col)
  187 {
  188     /* FUNCTION fill1 */
  189     DEBUG printf("map[%d][%d] = (%c)\n", row, col, map[row][col]);
  190     DEBUG dump();
  191     if ((0<row) && (0<col) && (maxWidth>col) && (lineCnt>row) && (BLANK == map[row][col]))
  192         {
  193             /* found a valid spot */
  194             DEBUG printf("Found (%d, %d) [%c]\n", row, col, map[row][col]);
  195             map[row][col] = SET;
  196             fill1(row+1, col);
  197             fill1(row-1, col);
  198             fill1(row, col+1);
  199             fill1(row, col-1);
  200         } /* found a valid spot */
  201 } /* FUNCTION fill1 */
  202 
  203 void process()
  204 {
  205     /* FUNCTION process */
  206     DEBUG dump();
  207     map[goalRow][goalCol] = BLANK;
  208     DEBUG dump();
  209     fill1(goalRow, goalCol);
  210     DEBUG dump();
  211     clean();
  212     dump();
  213     printf("%s\n", divider);
  214 } /* FUNCTION process */
  215 
  216 int main()
  217 {
  218     /* main */
  219     int i;
  220 
  221     init();
  222     for (i=0; i<numberOfTimes; i++)
  223         {
  224             /* while */
  225             getInput();
  226             process();
  227         } /* while */
  228 
  229     return EXIT_SUCCESS;
  230 } /* main */
  231 
  232