Computer Programming Contest Preparation

ToolBox - Source for: 5/572/a.c



/home/toolbox/public_html/solutions/5/572/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: 2015-03-12
   18  * Purpose:
   19  * Problem: 572
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_SIZE 132
   27 #define ABSENCE 0
   28 #define OIL -1
   29 
   30 int m;
   31 int n;
   32 int grid [MAX_SIZE][MAX_SIZE];
   33 int maxFill;
   34 
   35 void init()
   36 {
   37     /* FUNCTION init */
   38 } /* FUNCTION init */
   39 
   40 void dump()
   41 {
   42     /* FUNCTION dump */
   43 } /* FUNCTION dump */
   44 
   45 int getInput()
   46 {
   47     /* FUNCTION getInput */
   48     int dataReadFlag;
   49     char line[MAX_SIZE];
   50     int i;
   51     int j;
   52 
   53     scanf(" %d %d ", &m, &n);
   54     if (0 == m)
   55         {
   56             /* end of file */
   57             dataReadFlag = FALSE;
   58         } /* end of file */
   59     else
   60         {
   61             /* okay -- need to load a grid */
   62             dataReadFlag = TRUE;
   63             for (i=0; i<m; i++)
   64                 {
   65                     /* for -- each row */
   66                     scanf(" %s ", line);
   67                     for (j=0; j<n; j++)
   68                         {
   69                             /* for -- each col */
   70                             if ('*' == line[j])
   71                                 {
   72                                     /* no oil */
   73                                     grid[i][j] = ABSENCE;
   74                                 } /* no oil */
   75                             else
   76                                 {
   77                                     /* oil */
   78                                     grid[i][j] = OIL;
   79                                 } /* oil */
   80                         } /* for -- each col */
   81                 } /* for -- each row */
   82         } /* okay -- need to load a grid */
   83     return (dataReadFlag);
   84 } /* FUNCTION getInput */
   85 
   86 void fill(int r, int c, int fil)
   87 {
   88     /* FUNCTION fill */
   89     if ((0<=r) && (r<m) && (0<=c) && (c<n) && (OIL == grid[r][c]))
   90         {
   91             /* oil here */
   92             grid[r][c] = fil;
   93             fill(r-1, c-1, fil);
   94             fill(r-1, c, fil);
   95             fill(r-1, c+1, fil);
   96             fill(r, c+1, fil);
   97             fill(r+1, c+1, fil);
   98             fill(r+1, c, fil);
   99             fill(r+1, c-1, fil);
  100             fill(r, c-1, fil);
  101         } /* oil here */
  102 } /* FUNCTION fill */
  103 
  104 void process()
  105 {
  106     /* FUNCTION process */
  107     int i;
  108     int j;
  109 
  110     maxFill = 0;
  111     for (i=0; i<m; i++)
  112         {
  113             /* for -- each row */
  114             for (j=0; j<n; j++)
  115                 {
  116                     /* for -- each col */
  117                     if (OIL == grid[i][j])
  118                         {
  119                             /* found a new deposit */
  120                             maxFill++;
  121                             fill(i, j, maxFill);
  122                         } /* found a new deposit */
  123                 } /* for -- each col */
  124         } /* for -- each row */
  125     printf("%d\n", maxFill);
  126 } /* FUNCTION process */
  127 
  128 int main()
  129 {
  130     /* main */
  131     int moreToDo;
  132 
  133     init();
  134     moreToDo = getInput();
  135     while (moreToDo)
  136         {
  137             /* while */
  138             process();
  139             moreToDo = getInput();
  140         } /* while */
  141 
  142     return EXIT_SUCCESS;
  143 } /* main */
  144