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