Computer Programming Contest Preparation

ToolBox - Source for: 112/11244/a.c



/home/toolbox/public_html/solutions/112/11244/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 <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 /*
   16  *  Author: Isaac Traxler
   17  *    Date: 2019-10-23
   18  * Purpose: fun
   19  * Problem: 11244
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_DIM 105
   27 #define DOT '.'
   28 #define STAR '*'
   29 #define ZERO 0
   30 
   31 char sky[MAX_DIM][MAX_DIM];
   32 int rowSize;
   33 int colSize;
   34 int rowOff[8] = {-1, -1, -1,  0,  0,  1,  1,  1};
   35 int colOff[8] = {-1,  0,  1, -1,  1, -1,  0,  1};
   36 
   37 void init()
   38 {
   39     /* FUNCTION init */
   40 } /* FUNCTION init */
   41 
   42 void dump()
   43 {
   44     /* FUNCTION dump */
   45     int i;
   46     int j;
   47 
   48     for (i=0; (rowSize+1)>=i; i++)
   49         {
   50             /* for each row */
   51             for (j=0; (colSize+1)>=j; j++)
   52                 {
   53                     /* for each column */
   54                     printf("%c", sky[i][j]);
   55                 } /* for each column */
   56             printf("\n");
   57         } /* for each row */
   58 } /* FUNCTION dump */
   59 
   60 int getInput()
   61 {
   62     /* FUNCTION getInput */
   63     int dataReadFlag = FALSE;
   64     int i;
   65     int j;
   66 
   67     scanf(" %d %d ", &rowSize, &colSize);
   68     if (0 != rowSize)
   69         {
   70             /* data to process */
   71             dataReadFlag = TRUE;
   72             for (j=0; (colSize+1)>=j; j++)
   73                 {
   74                     /* dot out first and last row */
   75                     sky[0][j] = DOT;
   76                     sky[rowSize+1][j] = DOT;
   77                 } /* dot out first and last row */
   78             sky[0][j] = ZERO;
   79             sky[rowSize+1][j] = ZERO;
   80             /* get sky */
   81             for (i=1; (rowSize>=i); i++)
   82                 {
   83                     /* for each row of sky */
   84                     sky[i][0] = DOT;
   85                     fgets(&sky[i][1], MAX_DIM-3, stdin);
   86                     sky[i][colSize+1] = DOT;
   87                     sky[i][colSize+2] = ZERO;
   88                 } /* for each row of sky */
   89         } /* data to process */
   90     return (dataReadFlag);
   91 } /* FUNCTION getInput */
   92 
   93 void fill(int row, int col)
   94 {
   95     /* FUNCTION fill */
   96     int i;
   97 
   98     if (STAR == sky[row][col])
   99         {
  100             /* found part of object */
  101             sky[row][col] = DOT;
  102             for (i=0; (8>i); i++)
  103                 {
  104                     /* for each of the 8 surrounding pixels */
  105                     fill(row + rowOff[i], col + colOff[i]);
  106                 } /* for each of the 8 surrounding pixels */
  107         } /* found part of object */
  108 } /* FUNCTION fill */
  109 
  110 int notClear(int row, int col)
  111 {
  112     /* FUNCTION notClear */
  113     int ret = FALSE;
  114     int i;
  115 
  116     for (i=0; (8>i); i++)
  117         {
  118             /* for each of the 8 surrounding pixels */
  119             ret = ret || (STAR == sky[row + rowOff[i]][col + colOff[i]]);
  120         } /* for each of the 8 surrounding pixels */
  121     return ret;
  122 } /* FUNCTION notClear */
  123 
  124 void process()
  125 {
  126     /* FUNCTION process */
  127     int i;
  128     int j;
  129     int cnt = 0;
  130 
  131     for (i=0; (rowSize+1)>=i; i++)
  132         {
  133             /* for each row */
  134             for (j=0; (colSize+1)>=j; j++)
  135                 {
  136                     /* for each column */
  137                     if ((STAR == sky[i][j]) && (notClear(i, j)))
  138                         {
  139                             /* found an object */
  140                             fill(i,j);
  141                         } /* found an object */
  142                 } /* for each column */
  143         } /* for each row */
  144     /* now that all objects are gone -- look for stars */
  145     for (i=0; (rowSize+1)>=i; i++)
  146         {
  147             /* for each row */
  148             for (j=0; (colSize+1)>=j; j++)
  149                 {
  150                     /* for each column */
  151                     if (STAR == sky[i][j])
  152                         {
  153                             /* found a star */
  154                             cnt++;
  155                         } /* found a star */
  156                 } /* for each column */
  157         } /* for each row */
  158     printf("%d\n", cnt);
  159 } /* FUNCTION process */
  160 
  161 int main()
  162 {
  163     /* main */
  164     int moreToDo;
  165 
  166     init();
  167     moreToDo = getInput();
  168     while (moreToDo)
  169         {
  170             /* while */
  171             process();
  172             moreToDo = getInput();
  173         } /* while */
  174 
  175     return EXIT_SUCCESS;
  176 } /* main */
  177