Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/7/784/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: 2018-03-13
   20  * Purpose: fun
   21  * Problem: 784 - Maze Exploration
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAX_LINE 100
   29 #define MAX_ROW 32
   30 #define START '*'
   31 #define MARK '#'
   32 #define EMPTY ' '
   33 
   34 int numberOfTimes;
   35 char line[MAX_LINE];
   36 char maze[MAX_ROW][MAX_LINE];
   37 int rows;
   38 int Sr;
   39 int Sc;
   40 
   41 void init()
   42 {
   43     /* FUNCTION init */
   44     scanf("%d ", &numberOfTimes);
   45 } /* FUNCTION init */
   46 
   47 void dump()
   48 {
   49     /* FUNCTION dump */
   50 } /* FUNCTION dump */
   51 
   52 void getInput()
   53 {
   54     /* FUNCTION getInput */
   55     int i;
   56 
   57     rows = 0;
   58     fgets(maze[rows], MAX_LINE - 1, stdin);
   59     while ('_' != maze[rows][0])
   60         {
   61             /* read in each line of the maze */
   62             /* look for '*' */
   63             for (i=1; strlen(maze[rows])>i; i++)
   64                 {
   65                     /* for */
   66                     if (START == maze[rows][i])
   67                         {
   68                             /* found start location */
   69                             Sr = rows;
   70                             Sc = i;
   71                             maze[Sr][Sc] = MARK;
   72                         } /* found start location */
   73                 } /* for */
   74             rows++;
   75             fgets(maze[rows], MAX_LINE - 1, stdin);
   76         } /* read in each line of the maze */
   77 } /* FUNCTION getInput */
   78 
   79 int mark(int x, int y)
   80 {
   81     /* FUNCTION mark */
   82     int tot = 0;
   83     if (EMPTY == maze[x+1][y])
   84         {
   85             tot++;
   86             maze[x+1][y] = MARK;
   87         }
   88     if (EMPTY == maze[x-1][y])
   89         {
   90             tot++;
   91             maze[x-1][y] = MARK;
   92         }
   93     if (EMPTY == maze[x][y+1])
   94         {
   95             tot++;
   96             maze[x][y+1] = MARK;
   97         }
   98     if (EMPTY == maze[x][y-1])
   99         {
  100             tot++;
  101             maze[x][y-1] = MARK;
  102         }
  103     return tot;
  104 } /* FUNCTION mark */
  105 
  106 void process()
  107 {
  108     /* FUNCTION process */
  109     int cnt = 1;
  110     int lngth;
  111     int r;
  112     int c;
  113 
  114     while (0 < cnt)
  115         {
  116             /* keep marking while we can */
  117             cnt = 0;
  118             for (r=1; rows>r; r++)
  119                 {
  120                     /* for each row */
  121                     lngth = strlen(maze[r]);
  122                     for (c=1; lngth>c; c++)
  123                         {
  124                             /* for each column */
  125                             if (MARK == maze[r][c])
  126                                 {
  127                                     /* possible nexus for new flooding */
  128                                     cnt = cnt + mark(r, c);
  129                                 } /* possible nexus for new flooding */
  130                         } /* for each column */
  131                 } /* for each row */
  132         } /* keep marking while we can */
  133     /* show answer */
  134     for (r=0; rows>=r; r++)
  135         {
  136             /* display each row */
  137             printf("%s", maze[r]);
  138         } /* display each row */
  139 } /* FUNCTION process */
  140 
  141 int main()
  142 {
  143     /* main */
  144     int i;
  145 
  146     init();
  147     for (i=0; i<numberOfTimes; i++)
  148         {
  149             /* while */
  150             getInput();
  151             process();
  152         } /* while */
  153 
  154     return EXIT_SUCCESS;
  155 } /* main */
  156 
  157