Computer Programming Contest Preparation

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



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