Computer Programming Contest Preparation

ToolBox - Source for: 103/10336/b.c



/home/toolbox/public_html/solutions/103/10336/b.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 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2018-03-18
   20  * Purpose: fun
   21  * Problem: 10136 - Rank the Languages
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define LETTERS 26
   29 #define MAX_DIM 1002
   30 #define MISSING '#'
   31 
   32 char grid[MAX_DIM][MAX_DIM];
   33 int  cnt[LETTERS];
   34 int  used[LETTERS];
   35 int  u[2] = { 1, 1};
   36 int  Roff[4] = { 0, 1,  0, -1};
   37 int  Coff[4] = { 1, 0, -1,  0};
   38 
   39 int numberOfTimes;
   40 int h;
   41 int w;
   42 
   43 void init()
   44 {
   45     /* FUNCTION init */
   46     scanf("%d ", &numberOfTimes);
   47 } /* FUNCTION init */
   48 
   49 void reset()
   50 {
   51     /* FUNCTION reset */
   52     int i;
   53 
   54     for (i=0; LETTERS>i; i++)
   55         {
   56             /* for each possible letter */
   57             cnt[i] = 0;
   58             used[i] = 0;
   59         } /* for each possible letter */
   60 } /* FUNCTION reset */
   61 
   62 void dump()
   63 {
   64     /* FUNCTION dump */
   65 } /* FUNCTION dump */
   66 
   67 void getInput()
   68 {
   69     /* FUNCTION getInput */
   70     int i;
   71     int j;
   72     int tmp;
   73 
   74     reset();
   75     scanf(" %d %d ", &h, &w);
   76     for (i=1; h>=i; i++)
   77         {
   78             /* for each row */
   79             scanf(" %s ", &grid[h][1]);
   80             grid[h][0] = MISSING;
   81             grid[h][w+1] = MISSING;
   82             for (j=1; w>=j; j++)
   83                 {
   84                     /* each column */
   85                     tmp = grid[i][j]-'a';
   86                     used[tmp] = u[used[tmp]];  /* mark each used letter */
   87                 } /* each column */
   88         } /* for each row */
   89     for (j=0; (h+2)>j; j++)
   90         {
   91             /* load the 2 guard rows */
   92             grid[0][j] = MISSING;
   93             grid[h+1][j] = MISSING;
   94         } /* load the 2 guard rows */
   95 } /* FUNCTION getInput */
   96 
   97 void fill(int r, int c, char lt)
   98 {
   99     /* FUNCTION fill */
  100     int i;
  101 
  102     grid[r][c] = MISSING;
  103     for (i=0; 4>i; i++)
  104         {
  105             /* for each neighbor */
  106             if (lt == grid[r + Roff[i]][c + Coff[i]])
  107                 {
  108                     /* match found */
  109                     fill(r + Roff[i], c + Coff[i], lt);
  110                 } /* match found */
  111         } /* for each neighbor */
  112 } /* FUNCTION fill */
  113 
  114 void process()
  115 {
  116     /* FUNCTION process */
  117     int i;
  118     int j;
  119     int l;
  120     char lt;
  121 
  122     for (l=0; LETTERS>l; l++)
  123         {
  124             /* for each possible letter */
  125             if (0 != used[l])
  126                 {
  127                     /* letter was used */
  128                     lt = 'a' + l;
  129                     for (i=1; h>=i; i++)
  130                         {
  131                             /* for each row */
  132                             for (j=1; w>=j; j++)
  133                                 {
  134                                     /* for each column */
  135                                     if (lt == grid[i][j])
  136                                         {
  137                                             /* letter match */
  138                                             fill(i, j, lt);
  139                                             cnt[l] = cnt[l] + 1;
  140                                         } /* letter match */
  141                                 } /* for each column */
  142                         } /* for each row */
  143                 } /* letter was used */
  144         } /* for each possible letter */
  145     for (i=0; LETTERS>i; i++)
  146         {
  147             /* for each possible letter */
  148             if (0 != used[i])
  149                 {
  150                     /* dump this letter */
  151                     lt = i + 'a';
  152                     printf("%c: %d\n", lt, cnt[i]);
  153                 } /* dump this letter */
  154         } /* for each possible letter */
  155 } /* FUNCTION process */
  156 
  157 int main()
  158 {
  159     /* main */
  160     int i;
  161 
  162     init();
  163     for (i=1; i<=numberOfTimes; i++)
  164         {
  165             /* while */
  166             printf("World #%d\n", i);
  167             getInput();
  168             process();
  169         } /* while */
  170 
  171     return EXIT_SUCCESS;
  172 } /* main */
  173 
  174