Computer Programming Contest Preparation

ToolBox - Source for: 7/722/a.c



/home/toolbox/public_html/solutions/7/722/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 <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 #define MAX_ROWS 101
   16 #define MAX_COLS 101
   17 #define LAND 1
   18 #define WATER 0
   19 #define FILL 3
   20 
   21 /* fprintf(stderr, "functionName: message", varslist); */
   22 
   23 /*
   24  *  Author: Isaac Traxler
   25  *    Date: 2014-10-26
   26  * Purpose: UVA hack-a-thon
   27  * Problem: 722 - Lakes
   28  */
   29 
   30 /*
   31  * This template reads data a specified number of times.
   32  */
   33 
   34 int numberOfTimes;
   35 int m; /* number of rows */
   36 int n; /* number of columns */
   37 int world[MAX_ROWS][MAX_COLS];
   38 int wr; /* row of water location */
   39 int wc; /* column of water location */
   40 
   41 void init()
   42 {
   43     /* FUNCTION init */
   44     scanf("%d ", &numberOfTimes);
   45 } /* FUNCTION init */
   46 
   47 void dump()
   48 {
   49     /* FUNCTION dump */
   50     int r;
   51     int c;
   52 
   53     printf("World is %d x %d\n", m, n);
   54     for (r=0; r<(m+2); r++)
   55         {
   56             /* for each row */
   57             for (c=0; c<(n+2); c++)
   58                 {
   59                     /* for each col */
   60                     printf("%d", world[r][c]);
   61                 } /* for each col */
   62             printf("\n");
   63         } /* for each row */
   64     printf(" water is at: (%d,%d)\n\n", wr, wc);
   65 } /* FUNCTION dump */
   66 
   67 void getInput()
   68 {
   69     /* FUNCTION getInput */
   70     char line[MAX_COLS];
   71     int r;
   72     int c;
   73 
   74     scanf(" %d %d ", &wr, &wc);
   75     DEBUG printf("water (%d, %d)\n", wr, wc);
   76     fgets(line, MAX_COLS, stdin);
   77     line[strlen(line)-1] = 0;
   78     DEBUG printf("line = [%s]\n", line);
   79     n = strlen(line);
   80     m = 0;
   81     while (0 < strlen(line))
   82         {
   83             /* found another line of input */
   84             m++;
   85             world[m][0] = LAND;
   86             for (c=0; c<n; c++)
   87                 {
   88                     /* process each pixel of the row */
   89                     world[m][c+1] = line[c] - '0';
   90                 } /* process each pixel of the row */
   91             world[m][n+1] = LAND;
   92             fgets(line, MAX_COLS, stdin);
   93             line[strlen(line)-1] = 0;
   94             if (feof(stdin))
   95                 {
   96                     line[0] = 0;
   97                 }
   98             DEBUG printf("line = [%s]\n", line);
   99         } /* found another line of input */
  100 
  101     for (c=0; c<n+2; c++)
  102         {
  103             /* do top and bottom border */
  104             world[0][c] = LAND;
  105             world[m+1][c] = LAND;
  106         } /* do top and bottom border */
  107 
  108 } /* FUNCTION getInput */
  109 
  110 void process()
  111 {
  112     /* FUNCTION process */
  113     int r;
  114     int c;
  115     int cnt = 1;
  116     int waterFound = TRUE;
  117 
  118     DEBUG dump();
  119     world[wr][wc] = FILL;
  120     while (waterFound)
  121         {
  122             /* do look for new water */
  123             waterFound = FALSE;
  124             for (r=1; r<(m+1); r++)
  125                 {
  126                     /* for each row */
  127                     for (c=1; c<(n+1); c++)
  128                         {
  129                             /* for each col */
  130                             if (FILL == world[r][c])
  131                                 {
  132                                     /* found a fill area */
  133                                     if (WATER == world[r-1][c] )
  134                                         {
  135                                             /* found water on previous row */
  136                                             waterFound = TRUE;
  137                                             world[r-1][c] = FILL;
  138                                             cnt++;
  139                                         } /* found water on previous row */
  140                                     if (WATER == world[r+1][c] )
  141                                         {
  142                                             /* found water on next row */
  143                                             waterFound = TRUE;
  144                                             world[r+1][c] = FILL;
  145                                             cnt++;
  146                                         } /* found water on next row */
  147                                     if (WATER == world[r][c-1] )
  148                                         {
  149                                             /* found water on previous column */
  150                                             waterFound = TRUE;
  151                                             world[r][c-1] = FILL;
  152                                             cnt++;
  153                                         } /* found water on previous column */
  154                                     if (WATER == world[r][c+1] )
  155                                         {
  156                                             /* found water on next column */
  157                                             waterFound = TRUE;
  158                                             world[r][c+1] = FILL;
  159                                             cnt++;
  160                                         } /* found water on next column */
  161                                 } /* found a fill area */
  162                         } /* for each col */
  163                 } /* for each row */
  164         } /* do look for new water */
  165     DEBUG dump();
  166     printf("%d\n", cnt);
  167 
  168 } /* FUNCTION process */
  169 
  170 int main ()
  171 {
  172     /* main */
  173     int i;
  174 
  175     init();
  176     for (i=0; i<numberOfTimes; i++)
  177         {
  178             /* while */
  179             getInput();
  180             process();
  181             if ((numberOfTimes-1) != i)
  182                 {
  183                     printf("\n");
  184                 }
  185         } /* while */
  186 
  187     return EXIT_SUCCESS;
  188 } /* main */
  189 
  190