/home/toolbox/public_html/solutions/7/776/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 #define MAX_LINE 2048
16 #define MAX_DIM 1024
17 #define MISSING ' '
18
19 /*
20 * Author: Isaac Traxler
21 * Date: 2019-09-09
22 * Purpose: fun
23 * Problem: 776
24 */
25
26 /*
27 * This template reads lines of data at a time until end of file.
28 */
29
30 char line[MAX_LINE];
31 char in[MAX_DIM][MAX_DIM];
32 int out[MAX_DIM][MAX_DIM];
33 int rows;
34 int cols;
35 int EOF_FLAG = FALSE;
36 int max;
37
38 void init()
39 {
40 /* FUNCTION init */
41 } /* FUNCTION init */
42
43 void dump()
44 {
45 /* FUNCTION dump */
46 int i;
47 int j;
48
49 printf("rows = %d cols=%d\n", rows, cols);
50 for (i=1; rows>i; i++)
51 {
52 /* for each row */
53 printf("%3d: ", i);
54 for (j=1; cols>j; j++)
55 {
56 /* for each column */
57 printf("%c ", in[i][j]);
58 } /* for each column */
59 printf("\n");
60 } /* for each row */
61 } /* FUNCTION dump */
62
63 int largest(int a, int b)
64 {
65 /* FUNCTION largest */
66 int ret;
67
68 ret = (a > b) ? a : b;
69 return ret;
70 } /* FUNCTION largest */
71
72 int maxWidth(int x)
73 {
74 /* FUNCTION maxWidth */
75 int ret = 0;
76
77 while (0 < x)
78 {
79 /* while */
80 ret++;
81 x = x /10;
82 } /* while */
83 return ret;
84 } /* FUNCTION maxWidth */
85
86 void dumpAns(int wd)
87 {
88 /* FUNCTION dumpAns */
89 int i;
90 int j;
91
92 for (j=1; cols>j; j++)
93 {
94 /* for each column */
95 for (i=1; rows>i; i++)
96 {
97 /* for each row */
98 out[0][j] = largest(out[0][j], out[i][j]);
99 } /* for each row */
100 out[0][j] = maxWidth(out[0][j]);
101 } /* for each column */
102
103 for (i=1; rows>i; i++)
104 {
105 /* for each row */
106 printf("%*d", out[0][1], out[i][1]);
107 for (j=2; cols>j; j++)
108 {
109 /* for each column */
110 printf(" %*d", out[0][j], out[i][j]);
111 } /* for each column */
112 printf("\n");
113 } /* for each row */
114 printf("%\n");
115 } /* FUNCTION dumpAns */
116
117 int getInput()
118 {
119 /* FUNCTION getInput */
120 int dataReadFlag;
121 int i;
122 int j;
123
124 fgets(line, MAX_LINE, stdin);
125 if (feof(stdin))
126 {
127 /* EOF */
128 dataReadFlag = FALSE;
129 EOF_FLAG = TRUE;
130 } /* EOF */
131 else
132 {
133 /* something to read */
134 dataReadFlag = TRUE;
135 cols = strlen(line)/2 + 1;
136 rows = 1;
137 while ('%' != line[0])
138 {
139 /* load next line */
140 if (feof(stdin))
141 {
142 /* EOF */
143 EOF_FLAG = TRUE;
144 line[0] = '%';
145 } /* EOF */
146 else
147 {
148 /* another line of the matrix */
149 DEBUG printf("line [%s]", line);
150 for (i=1,j=0; i<cols; i++, j=j+2)
151 {
152 /* pricess line */
153 in[rows][i] = line[j];
154 out[rows][i] = 0;
155 } /* pricess line */
156 rows++;
157 } /* another line of the matrix */
158 fgets(line, MAX_LINE, stdin);
159 } /* load next line */
160 /* sent missing boundary */
161 for (i=0; i<=rows; i++)
162 {
163 /* reset first and last of each row */
164 in[i][0] = MISSING;
165 in[i][cols+1]= MISSING;
166 } /* reset first and last of each row*/
167 for (i=1; i<cols; i++)
168 {
169 /* reset top and bottom bondary */
170 in[0][i] = MISSING;
171 in[rows][i] = MISSING;
172 } /* reset top and bottom bondary */
173 } /* something to read */
174 return (dataReadFlag);
175 } /* FUNCTION getInput */
176
177 void fill(int r, int c, int x, char chr)
178 {
179 /* FUNCTION fill */
180 DEBUG printf("fill (r=%d) (c=%d) (x=%d) (chr=%c)\n", r, c, x, chr);
181 out[r][c] = x;
182 in[r][c] = MISSING;
183 if (chr == in[r-1][c-1])
184 {
185 fill(r-1, c-1, x, chr);
186 }
187 if (chr == in[r-1][c ])
188 {
189 fill(r-1, c , x, chr);
190 }
191 if (chr == in[r-1][c+1])
192 {
193 fill(r-1, c+1, x, chr);
194 }
195 if (chr == in[r ][c-1])
196 {
197 fill(r , c-1, x, chr);
198 }
199 if (chr == in[r ][c+1])
200 {
201 fill(r , c+1, x, chr);
202 }
203 if (chr == in[r+1][c-1])
204 {
205 fill(r+1, c-1, x, chr);
206 }
207 if (chr == in[r+1][c ])
208 {
209 fill(r+1, c , x, chr);
210 }
211 if (chr == in[r+1][c+1])
212 {
213 fill(r+1, c+1, x, chr);
214 }
215 } /* FUNCTION fill */
216
217 void process()
218 {
219 /* FUNCTION process */
220 int i;
221 int j;
222 int wd;
223
224 DEBUG dump();
225 max=0;
226 for (i=1; rows>i; i++)
227 {
228 /* for each row */
229 for (j=1; cols>j; j++)
230 {
231 /* for each column */
232 if (0 == out[i][j])
233 {
234 /* new family */
235 max++;
236 fill(i, j, max, in[i][j]);
237 } /* new family */
238 } /* for each column */
239 } /* for each row */
240 dumpAns(wd);
241 } /* FUNCTION process */
242
243 int main()
244 {
245 /* main */
246 int moreToDo;
247
248 init();
249 moreToDo = getInput();
250 while (moreToDo)
251 {
252 /* while */
253 process();
254 moreToDo = getInput();
255 } /* while */
256
257 return EXIT_SUCCESS;
258 } /* main */
259