Computer Programming Contest Preparation

ToolBox - Source for: 3/352/a.c



/home/toolbox/public_html/solutions/3/352/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: 2018-03-13
   18  * Purpose: fun
   19  * Problem: 352 - War Eagles
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_DIM 30
   27 #define ZERO '0'
   28 #define ONE  '1'
   29 
   30 int sz;
   31 char tbl[MAX_DIM][MAX_DIM];
   32 int xoff[8] = { 1,  1,  0, -1, -1, -1,  0,  1};
   33 int yoff[8] = { 0,  1,  1,  1,  0, -1, -1, -1};
   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     int i;
   50     int tmp;
   51 
   52     dataReadFlag = (0 <= scanf(" %d ", &sz));
   53     if (dataReadFlag)
   54         {
   55             /* load table */
   56             tmp = sz + 1;
   57             tbl[0][0] = ZERO;
   58             tbl[0][tmp] = ZERO;
   59             tbl[tmp][0] = ZERO;
   60             tbl[tmp][tmp] = ZERO;
   61             for (i=1; tmp>i; i++)
   62                 {
   63                     /* for each row */
   64                     scanf("%s", &tbl[i][1]);
   65                     tbl[i][0] = ZERO;
   66                     tbl[i][tmp] = ZERO;
   67                     tbl[0][i] = ZERO;
   68                     tbl[tmp][0] = ZERO;
   69                 } /* for each row */
   70         } /* load table */
   71     return (dataReadFlag);
   72 } /* FUNCTION getInput */
   73 
   74 void fill(int r, int c)
   75 {
   76     /* FUNCITON fill */
   77     int i;
   78 
   79     tbl[r][c] = ZERO;
   80     for (i=0; 8>i; i++)
   81         {
   82             /* for each possible neighbor */
   83             if (ONE == tbl[r + xoff[i]][c + yoff[i]])
   84                 {
   85                     /* found another */
   86                     fill(r+xoff[i], c+yoff[i]);
   87                 } /* found another */
   88         } /* for each possible neighbor */
   89 
   90 } /* FUNCITON fill */
   91 
   92 void process()
   93 {
   94     /* FUNCTION process */
   95     int cnt = 0;
   96     int i;
   97     int j;
   98 
   99     for (i=1; sz>=i; i++)
  100         {
  101             /* for each row */
  102             for (j=1; sz>=j; j++)
  103                 {
  104                     /* for each column */
  105                     if (ONE == tbl[i][j])
  106                         {
  107                             /* found another eagle */
  108                             cnt++;
  109                             fill(i, j);
  110                         } /* found another eagle */
  111                 } /* for each column */
  112         } /* for each row */
  113     printf("%d war eagles.\n", cnt);
  114 } /* FUNCTION process */
  115 
  116 int main()
  117 {
  118     /* main */
  119     int moreToDo;
  120     int images = 1;
  121 
  122     init();
  123     moreToDo = getInput();
  124     while (moreToDo)
  125         {
  126             /* while */
  127             printf("Image number %d contains ", images);
  128             process();
  129             moreToDo = getInput();
  130             images++;
  131         } /* while */
  132 
  133     return EXIT_SUCCESS;
  134 } /* main */
  135