/home/toolbox/public_html/solutions/7/782/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:2019-10-05
20 * Purpose: fun
21 * Problem: 782
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 #define MAX_WIDTH 100
29 #define MAX_HEIGHT 33
30 #define BLANK ' '
31 #define GOAL '*'
32 #define SET '#'
33 #define LINE '_'
34 #define NL '\n'
35 #define ZERO 0
36
37 #define maxx(a,b) ( { a>b ? a :b; } )
38
39 int numberOfTimes;
40 char map[MAX_HEIGHT][MAX_WIDTH];
41 int lineCnt;
42 int goalCol;
43 int goalRow;
44 char chr;
45 int maxWidth;
46 char divider[MAX_WIDTH];
47
48 void init()
49 {
50 /* FUNCTION init */
51 scanf("%d", &numberOfTimes);
52 } /* FUNCTION init */
53
54 void init1()
55 {
56 /* FUNCTION init1 */
57 int i;
58 int j;
59
60 for (i=0; MAX_HEIGHT>i; i++)
61 {
62 /* for i */
63 for (j=0; MAX_WIDTH>j; j++)
64 {
65 /* for j */
66 map[i][j] = BLANK;
67 } /* for j */
68 } /* for i */
69 lineCnt = 1;
70 maxWidth=0;
71 } /* FUNCTION init1 */
72
73 void dump()
74 {
75 /* FUNCTION dump */
76 int i;
77 int j;
78
79 DEBUG printf("Contour char [%c], start is at (%d, %d)\n", chr, goalCol, goalRow);
80 for (i=1; lineCnt>i; i++)
81 {
82 /* for each row */
83 DEBUG printf("|");
84 for (j=2; ZERO != map[i][j]; j++)
85 {
86 /* for each col */
87 printf("%c", map[i][j]);
88 } /* for each col */
89 DEBUG printf("|");
90 printf("\n");
91 } /* for each row */
92 } /* FUNCTION dump */
93
94 void getInput()
95 {
96 /* FUNCTION getInput */
97 int i;
98 int j;
99
100 init1();
101 chr = GOAL;
102 maxWidth = 0;
103 fgets(&map[lineCnt][2], MAX_WIDTH-1, stdin);
104 while (LINE != map[lineCnt][2])
105 {
106 /* while */
107 for (i=0; MAX_WIDTH>i; i++)
108 {
109 /* for */
110 switch (map[lineCnt][i])
111 {
112 /* switch */
113 case GOAL:
114 goalCol=i;
115 goalRow=lineCnt;
116 break;
117 case BLANK:
118 break;
119 case NL:
120 map[lineCnt][i] = BLANK;
121 break;
122 case ZERO:
123 map[lineCnt][i] = BLANK;
124 maxWidth=maxx(maxWidth, i);
125 i = MAX_WIDTH;
126 break;
127 default:
128 chr = map[lineCnt][i];
129 break;
130 } /* switch */
131 DEBUG printf("[%c]\n", map[lineCnt][i]);
132 } /* for */
133 DEBUG printf("! maxWidth = %d\n", maxWidth);
134
135 lineCnt++;
136 fgets(&map[lineCnt][2], MAX_WIDTH-1, stdin);
137 } /* while */
138 /* save the divider line to output */
139 for (i=2; ((BLANK == map[lineCnt][i]) || (LINE == map[lineCnt][i])); i++)
140 {
141 /* copy line */
142 divider[i-2] = map[lineCnt][i];
143 } /* copy line */
144 divider[i-2] = ZERO;
145 /* finished with divider */
146 maxWidth=maxWidth+2;
147 for (i=0; lineCnt>=i; i++)
148 {
149 /* make all lines same length */
150 for (j=0; ZERO!=map[i][j]; j++) ; /* skip forward until end of line */
151 while (maxWidth>j)
152 {
153 /* blank fill end of line */
154 map[i][j] = BLANK;
155 j++;
156 } /* blank fill end of line */
157 map[i][maxWidth] = ZERO;
158 } /* make all lines same length */
159 } /* FUNCTION getInput */
160
161 void clean()
162 {
163 /* FUNCTION clean */
164 int i;
165 int j;
166
167 for (i=1; lineCnt>i; i++)
168 {
169 /* for each row */
170 for (j=1; maxWidth>j; j++)
171 {
172 /* each column */
173 if ((SET == map[i][j]) &&
174 (chr != map[i-1][j]) &&
175 (chr != map[i+1][j]) &&
176 (chr != map[i][j-1]) &&
177 (chr != map[i][j+1]))
178 {
179 map[i][j] = BLANK;
180 }
181 } /* each column */
182 } /* for each row */
183 } /* FUNCTION clean */
184
185 void fill1(int row, int col)
186 {
187 /* FUNCTION fill1 */
188 DEBUG printf("map[%d][%d] = (%c)\n", row, col, map[row][col]);
189 DEBUG dump();
190 if ((0<row) && (0<col) && (maxWidth>col) && (lineCnt>row) && (BLANK == map[row][col]))
191 {
192 /* found a valid spot */
193 DEBUG printf("Found (%d, %d) [%c]\n", row, col, map[row][col]);
194 map[row][col] = SET;
195 fill1(row+1, col);
196 fill1(row-1, col);
197 fill1(row, col+1);
198 fill1(row, col-1);
199 } /* found a valid spot */
200 } /* FUNCTION fill1 */
201
202 void process()
203 {
204 /* FUNCTION process */
205 DEBUG dump();
206 map[goalRow][goalCol] = BLANK;
207 DEBUG dump();
208 fill1(goalRow, goalCol);
209 DEBUG dump();
210 clean();
211 dump();
212 printf("%s\n", divider);
213 } /* FUNCTION process */
214
215 int main()
216 {
217 /* main */
218 int i;
219
220 init();
221 for (i=0; i<numberOfTimes; i++)
222 {
223 /* while */
224 getInput();
225 process();
226 } /* while */
227
228 return EXIT_SUCCESS;
229 } /* main */
230
231