Computer Programming Contest Preparation

ToolBox - Source for: 102/10279/a.c



/home/toolbox/public_html/solutions/102/10279/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-12
   20  * Purpose: fun
   21  * Problem: 10279
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAX_SIZE 15
   29 #define EMPTY 0
   30 #define MINE -1
   31 #define UNKNOWN -2
   32 #define TOUCHED_MINE -4
   33 
   34 int numberOfTimes;
   35 int touched;
   36 /* mines will have a border row and column of UNKNOWN all the way around */
   37 int mines[MAX_SIZE][MAX_SIZE];
   38 int xoff[8] = { -1, -1, -1,  0,  0,  1,  1,  1 };
   39 int yoff[8] = { -1,  0,  1, -1,  1, -1,  0,  1 };
   40 int size;
   41 
   42 void init()
   43 {
   44     /* FUNCTION init */
   45     int i;
   46 
   47     /* mark boundary spots as unknown */
   48     for (i=0; MAX_SIZE>i; i++)
   49         {
   50             /* for each place */
   51             mines[i][0] = UNKNOWN;
   52             mines[0][i] = UNKNOWN;
   53         } /* for each place */
   54     scanf("%d ", &numberOfTimes);
   55 } /* FUNCTION init */
   56 
   57 void dump()
   58 {
   59     /* FUNCTION dump */
   60     int i,j;
   61     printf("size = %d\n", size);
   62     for (i=0; MAX_SIZE>i; i++)
   63         {
   64             /* for each row */
   65             for (j=0; MAX_SIZE>j; j++)
   66                 {
   67                     /* for each column */
   68                     printf("%3d ", mines[i][j]);
   69                 } /* for each column */
   70             printf("\n");
   71         } /* for each row */
   72 } /* FUNCTION dump */
   73 
   74 void getInput()
   75 {
   76     /* FUNCTION getInput */
   77     int i;
   78     int j;
   79     char line[MAX_SIZE];
   80 
   81     scanf(" %d ", &size);
   82     touched = FALSE;
   83     /* get mine locations */
   84     mines[size+1][size+1] = UNKNOWN;
   85     for (i=1; size>=i; i++)
   86         {
   87             /* for */
   88             /* since minefield may be less than max, set outer edges to UNKNOWN */
   89             mines[size+1][i] = UNKNOWN;
   90             mines[i][size+1] = UNKNOWN;
   91             scanf(" %s ", line);
   92             DEBUG printf("line = (%s)\n", line);
   93             for (j=0; size>j; j++)
   94                 {
   95                     /* for each char */
   96                     switch (line[j])
   97                         {
   98                         /* switch */
   99                         case '.':
  100                             mines[i][j+1] = UNKNOWN;
  101                             break;
  102                         case '*':
  103                             mines[i][j+1] = MINE;
  104                             break;
  105                         } /* switch */
  106                 } /* for each char */
  107         } /* for */
  108     DEBUG dump();
  109     /* get explored locations */
  110     for (i=1; size>=i; i++)
  111         {
  112             /* for */
  113             scanf(" %s ", line);
  114             for (j=0; size>j; j++)
  115                 {
  116                     /* for each char */
  117                     if ('x' == line[j])
  118                         {
  119                             /* explored */
  120                             if (MINE == mines[i][j+1])
  121                                 {
  122                                     /* touched a mine */
  123                                     touched = TRUE;
  124                                     /*
  125                                     mines[i][j+1] = TOUCHED_MINE;
  126                                     */
  127                                 } /* touched a mine */
  128                             else
  129                                 {
  130                                     /* space is clear */
  131                                     mines[i][j+1] = EMPTY;
  132                                 } /* space is clear */
  133                         } /* explored */
  134                 } /* for each char */
  135         } /* for */
  136     DEBUG dump();
  137 } /* FUNCTION getInput */
  138 
  139 void process()
  140 {
  141     /* FUNCTION process */
  142     int i;
  143     int j;
  144     int k;
  145     int tot;
  146 
  147     for (i=1; size>=i; i++)
  148         {
  149             /* for each row */
  150             for (j=1; size>=j; j++)
  151                 {
  152                     /* for each column */
  153                     if (EMPTY == mines[i][j])
  154                         {
  155                             /* update count in empty */
  156                             tot = 0;
  157                             for (k=0; 8>k; k++)
  158                                 {
  159                                     /* for each of the 8 surrounding spots */
  160                                     if (MINE == mines[i+xoff[k]][j+yoff[k]])
  161                                         {
  162                                             /* found a mine - increment count */
  163                                             tot = tot + 1;
  164                                         } /* found a mine - increment count */
  165                                 } /* for each of the 8 surrounding spots */
  166                             mines[i][j] = tot;
  167                         } /* update count in empty */
  168                 } /* for each column */
  169         } /* for each row */
  170     /* output result */
  171     for (i=1; size>=i; i++)
  172         {
  173             /* for each row */
  174             for (j=1; size>=j; j++)
  175                 {
  176                     /* for each column */
  177                     if (0 > mines[i][j])
  178                         {
  179                             /* unknown or a mine */
  180                             if ((MINE == mines[i][j]) && (touched))
  181                                 {
  182                                     /* a mine was touched and this is a mine */
  183                                     printf("*");
  184                                 } /* a mine was touched and this is a mine */
  185                             else
  186                                 {
  187                                     /* no mine touched or this is not a mine */
  188                                     printf(".");
  189                                 } /* no mine touched or this is not a mine */
  190                         } /* unknown or a mine */
  191                     else
  192                         {
  193                             /* number so print */
  194                             printf("%1d", mines[i][j]);
  195                         } /* number so print */
  196                 } /* for each column */
  197             printf("\n");
  198         } /* for each row */
  199 } /* FUNCTION process */
  200 
  201 int main()
  202 {
  203     /* main */
  204     int i;
  205 
  206     init();
  207     for (i=0; i<numberOfTimes; i++)
  208         {
  209             /* while */
  210             getInput();
  211             if (0 < i)
  212                 {
  213                     printf("\n");
  214                 }
  215             process();
  216         } /* while */
  217 
  218     return EXIT_SUCCESS;
  219 } /* main */
  220 
  221