Computer Programming Contest Preparation

ToolBox - Source for: 101/10189/b.c



/home/toolbox/public_html/solutions/101/10189/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 #define MAX_SIZE 102
   16 #define BOMB '*'
   17 #define ZERO '0'
   18 
   19 /*
   20  *  Author: Isaac Traxler
   21  *    Date: 2012-07-18
   22  * Purpose: fun
   23  * Problem: 10189 - Minesweeper
   24  */
   25 
   26 /*
   27  * This template reads data until a terminating value is reached.
   28  */
   29 
   30 int fieldNumber=0;
   31 int rows;
   32 int cols;
   33 char field[MAX_SIZE][MAX_SIZE];
   34 int roff[8]= { 1,  1,  1,  0,  0, -1, -1, -1};
   35 int coff[8]= {-1,  0,  1, -1,  1, -1,  0,  1};
   36 int disp[20]= {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   37                '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'
   38               };
   39 
   40 void init()
   41 {
   42     /* FUNCTION init */
   43     int r;
   44     int c;
   45 
   46     for (r=0; r<=rows; r++)
   47         {
   48             /* for each row */
   49             for (c=0; c<=cols; c++)
   50                 {
   51                     /* for each column */
   52                     field[r][c] = 0;
   53                 } /* for each column */
   54         } /* for each row */
   55 } /* FUNCTION init */
   56 
   57 int getInput()
   58 {
   59     /* FUNCTION getInput */
   60     int dataReadFlag;
   61 
   62     scanf(" %d %d ", &rows, &cols);
   63     dataReadFlag = (0!=rows) || (0!=cols);
   64     rows++;
   65     cols++;
   66     fieldNumber++;
   67     return (dataReadFlag);
   68 } /* FUNCTION getInput */
   69 
   70 void dropBomb(int r, int c)
   71 {
   72     /* FUNCTION dropBomb */
   73     int i;
   74 
   75     for (i=0; i<8; i++)
   76         {
   77             /* loop through the 8 squares around the bomb */
   78             field[r+roff[i]][c+coff[i]]++;
   79         } /* loop through the 8 squares around the bomb */
   80 
   81 } /* FUNCTION dropBomb */
   82 
   83 void loadField()
   84 {
   85     /* FUNCTION loadField */
   86     int r;
   87     int c;
   88     char dummy;
   89 
   90     /* zero out top and bottom rows */
   91 
   92     /* load data */
   93     for (r=1; r<rows; r++)
   94         {
   95             /* for each row */
   96             for (c=1; c<cols; c++)
   97                 {
   98                     /* for each column */
   99                     scanf("%c", &dummy);
  100                     if (BOMB == dummy)
  101                         {
  102                             /* bomb found */
  103                             field[r][c] = 10;
  104                             dropBomb(r, c);
  105                         } /* bomb found */
  106                 } /* for each column */
  107             scanf("%c", &dummy); /* eat end of line char */
  108         } /* for each row */
  109 } /* FUNCTION loadField */
  110 
  111 void dump()
  112 {
  113     /* FUNCTION dump */
  114     int r;
  115     int c;
  116 
  117     printf("Field #%d:\n", fieldNumber);
  118     for (r=1; r<rows; r++)
  119         {
  120             /* for each row */
  121             for (c=1; c<cols; c++)
  122                 {
  123                     /* for each column */
  124                     printf("%c", disp[field[r][c]]);
  125                 } /* for each column */
  126             printf("\n");
  127         } /* for each row */
  128 } /* FUNCTION dump */
  129 
  130 void process()
  131 {
  132     /* FUNCTION process */
  133     init();
  134     loadField();
  135     dump();
  136 } /* FUNCTION process */
  137 
  138 int main ()
  139 {
  140     /* main */
  141     int moreToDo;
  142     int i = 0;
  143 
  144     init();
  145     moreToDo = getInput();
  146     while (moreToDo)
  147         {
  148             /* while */
  149             if (0 == i)
  150                 {
  151                     i++;
  152                 }
  153             else
  154                 {
  155                     printf("\n");
  156                 }
  157             process();
  158             moreToDo = getInput();
  159         } /* while */
  160 
  161     return EXIT_SUCCESS;
  162 } /* main */
  163