Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/101/10189/judged.c
    1 #include <stdio.h>
    2 #include <stdlib.h>
    3 #include <string.h>
    4 
    5 /*
    6  *  Author: Matthew Eastman <meastman@cct.lsu.edu>
    7  *    Date: 2006-09-05
    8  * Purpose: Contest practice
    9  * Problem: 10189 - Minesweeper <http://isaac.lsu.edu/udv/v101/10189.html>
   10  */
   11 
   12 #define TRUE  1
   13 #define FALSE 0
   14 
   15 #define MAX_SIZE 105
   16 
   17 char table[MAX_SIZE][MAX_SIZE];
   18 int width;
   19 int height;
   20 int offY[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
   21 int offX[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
   22 
   23 void dumpTable()
   24 {
   25     int i;
   26 
   27     for (i = 0; i <= height + 1; i++)
   28         {
   29             printf("\"%s\"\n", table[i]);
   30         }
   31 
   32     printf("--------\n");
   33 }
   34 
   35 int getInput()
   36 {
   37     int status = FALSE;
   38     int y;
   39 
   40     memset((void*) table, ' ', sizeof(table));
   41 
   42     scanf(" %d %d ", &height, &width);
   43 
   44     if ((0 < height) && (0 < width))
   45         {
   46             status = TRUE;
   47 
   48             for (y = 1; y <= height; y++)
   49                 {
   50                     scanf(" %s ", table[y] + 1);
   51                     /* fgets(table[y] + 1, MAX_SIZE - 1, stdin); */
   52                     table[y][width + 1] = ' ';
   53                     table[y][width + 2] = '\0';
   54                 }
   55 
   56             table[0][width + 2] = '\0';
   57             table[y][width + 2] = '\0';
   58         }
   59 
   60     return status;
   61 }
   62 
   63 int neighbors(int y, int x)
   64 {
   65     int i;
   66     int count = 0;
   67 
   68     for (i = 0; i < 8; i++)
   69         {
   70             if ('*' == table[y + offY[i]][x + offX[i]])
   71                 count++;
   72 
   73             /*
   74                     if (table[y + i / 3 - 1][x + i % 3 - 1] == '*')
   75                         count++;
   76             */
   77         }
   78 
   79     return count;
   80 }
   81 
   82 void solveProblem()
   83 {
   84     int x;
   85     int y;
   86 
   87     for (y = 1; y <= height; y++)
   88         {
   89             for (x = 1; x <= width; x++)
   90                 {
   91                     if (table[y][x] == '*')
   92                         printf("*");
   93                     else
   94                         printf("%d", neighbors(y, x));
   95                 }
   96             printf("\n");
   97         }
   98 }
   99 
  100 int main()
  101 {
  102     int i = 1;
  103 
  104     while (getInput())
  105         {
  106             if (1 < i)
  107                 printf("\n");
  108             printf("Field #%d:\n", i);
  109             solveProblem();
  110             /* dumpTable(); */
  111             i++;
  112         }
  113 
  114     return EXIT_SUCCESS;
  115 }
  116 
  117