/home/toolbox/public_html/solutions/103/10336/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 /* 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[i][1]);
80 grid[i][0] = MISSING;
81 grid[i][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 int mx;
122
123 for (i=1; h>=i; i++)
124 {
125 /* for each row */
126 for (j=1; w>=j; j++)
127 {
128 /* for each column */
129 if (MISSING != grid[i][j])
130 {
131 /* found a language */
132 lt = grid[i][j];
133 l = lt - 'a';
134 DEBUG printf("grid[%d][%d] = %d l = %d\n", i, j, grid[i][j], l);
135 fill(i, j, lt);
136 cnt[l] = cnt[l] + 1;
137 } /* found a language */
138 } /* for each column */
139 } /* for each row */
140 mx = cnt[0];
141 for (i=1; LETTERS>i; i++)
142 {
143 /* for each possible letter */
144 mx = (mx < cnt[i]) ? cnt[i] : mx;
145 } /* for each possible letter */
146 while (0 < mx)
147 {
148 /* while */
149 for (i=0; LETTERS>i; i++)
150 {
151 /* for each letter */
152 if (mx == cnt[i])
153 {
154 printf("%c: %d\n", (i + 'a'), cnt[i]);
155 }
156 } /* for each letter */
157 mx--;
158 } /* while */
159 } /* FUNCTION process */
160
161 int main()
162 {
163 /* main */
164 int i;
165
166 init();
167 for (i=1; i<=numberOfTimes; i++)
168 {
169 /* while */
170 printf("World #%d\n", i);
171 getInput();
172 process();
173 } /* while */
174
175 return EXIT_SUCCESS;
176 } /* main */
177
178