Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/101/10189/a.c
    1 //
    2 // trivial solution
    3 //
    4 
    5 #include <stdio.h>
    6 
    7 #define TRUE  (1 == 1)
    8 #define FALSE (1 != 1)
    9 #define MININT (-999999)
   10 #define DIM   102
   11 #define STAR '*'
   12 
   13 
   14 int getInput(int *xdim, int *ydim, int Arr[][DIM], int *starCount,  int starX[], int starY[])
   15 {
   16     // BEGIN FUNCTION getInput
   17     int moreToDo;
   18     int i;
   19     int j;
   20     char ignore;
   21 
   22     scanf("%d %d ", ydim, xdim);
   23     moreToDo = ((0 != *xdim) && (0 != *ydim));
   24 //    printf("x %d  y%d\n", *xdim, *ydim);
   25     *starCount = 0;
   26 //    printf("moreToDo=[%d]\n",moreToDo);
   27     if (moreToDo)
   28         {
   29             // if
   30             // iniitialize the 4 corners
   31             Arr[0][0] = 0;
   32             Arr[0][(*ydim)+1] = 0;
   33             Arr[(*xdim+1)][0] = 0;
   34             Arr[(*xdim+1)][(*ydim)+1] = 0;
   35             for (i=1; i <= *ydim; i++)
   36                 {
   37                     // for - i
   38                     // initialize vertical border
   39                     Arr[i][0] = 0;
   40                     Arr[i][(*xdim)+1] = 0;
   41                     for (j=1; j <= *xdim; j++)
   42                         {
   43                             // for - j
   44                             // initialize horizontal border
   45                             Arr[0][j] = 0;
   46                             Arr[(*ydim)+1][j] = 0;
   47                             ignore = getc(stdin);
   48 //                printf("[%c] - %d\n", ignore, ignore);
   49                             // if (STAR == ignore)
   50                             if (42 == ignore)
   51                                 {
   52                                     // if
   53 //                    printf("* ");
   54                                     Arr[i][j] = MININT;
   55                                     starX[*starCount] = i;
   56                                     starY[(*starCount)++] = j;
   57                                 } // if
   58                             else
   59                                 {
   60                                     // else
   61 //                   printf(". ");
   62                                     Arr[i][j] = 0;
   63                                 } // else
   64                         } // for - j
   65 //            printf("\n");
   66                     getc(stdin);
   67                 } // for - i
   68         } // if
   69 //    printf("leaving\n");
   70     return moreToDo;
   71 } // END FUNCTION getInput
   72 
   73 void dump(int xdim, int ydim, int Arr[][DIM], int starCount,  int starX[], int starY[], int fieldCount)
   74 {
   75     // BEGIN FUNCTION dump
   76     int moreToDo;
   77     int i;
   78     int j;
   79 
   80     printf("Field #%1d:\n", fieldCount);
   81     for (i=1; i <= (ydim+0); i++)
   82         {
   83             // for - i
   84             for (j=1; j <= (xdim+0); j++)
   85                 {
   86                     // for - j
   87                     if (0 > Arr[i][j])
   88                         {
   89                             // if
   90                             printf("*");
   91                         } // if
   92                     else
   93                         {
   94                             // else
   95                             printf("%d", Arr[i][j]);
   96                         } // else
   97                 } // for - j
   98             printf("\n");
   99         } // for - i
  100     printf("\n");
  101 //    printf("Location of stars\n");
  102 //    printf("\n");
  103 //    for (i=0; i< starCount; i++)
  104 //     { // for - i
  105 //        printf ("[%d,%d]\n",starX[i], starY[i]);
  106 //     } // for - i
  107 } // END FUNCTION dump
  108 
  109 void process(int xdim, int ydim, int Arr[][DIM], int starCount,  int starX[], int starY[])
  110 {
  111     // BEGIN FUNCTION process
  112     int i;
  113     int j;
  114     int I[8] = {-1,  0,  1,  -1,  1, -1,  0,  1};
  115     int J[8] = {-1,  -1, -1,  0,  0,  1,  1,  1};
  116     int k;
  117 
  118     for (i=0; i < starCount; i++)
  119         {
  120             // for - i
  121             for (k=0; k < 8; k++)
  122                 {
  123                     // for - j
  124                     Arr [starX[i]+I[k]] [starY[i]+J[k]] ++;
  125                 } // for - k
  126         } // for - i
  127 } // END FUNCTION process
  128 
  129 int main ()
  130 {
  131     // main
  132     // xdim - number of columns
  133     // ydim - nmmber of rows
  134     // Arr - array to hold numeric version of field
  135     // starCount - number of mines found
  136     // starX - x location of the each star
  137     // starY - y location of each star
  138     int moreToDo;  // tell me when to stop
  139     int xdim;
  140     int ydim;
  141     int Arr[DIM][DIM];
  142     int starCount;
  143     int starX[DIM*DIM];
  144     int starY[DIM*DIM];
  145     int fieldCount = 1;
  146 
  147 
  148 //    init();
  149     moreToDo = getInput(&xdim, &ydim, Arr, &starCount, starX, starY);
  150     while (moreToDo)
  151         {
  152             // while
  153             process(xdim, ydim, Arr, starCount, starX, starY);
  154             dump(xdim, ydim, Arr, starCount, starX, starY, fieldCount);
  155             fieldCount++;
  156             moreToDo = getInput(&xdim, &ydim, Arr, &starCount, starX, starY);
  157         } // while
  158 
  159     return 1;
  160 } // main
  161