/home/toolbox/public_html/solutions/7/782/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: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
47 void init()
48 {
49 /* FUNCTION init */
50 fgets(map[0], MAX_WIDTH-1, stdin);
51 sscanf(map[0], " %d ", &numberOfTimes);
52 } /* FUNCTION init */
53
54 void dump()
55 {
56 /* FUNCTION dump */
57 int i;
58 int j;
59
60 DEBUG printf("Contour char [%c], start is at (%d, %d)\n", chr, goalCol, goalRow);
61 for (i=0; lineCnt>=i; i++)
62 {
63 /* for each row */
64 DEBUG printf("|");
65 for (j=0; ZERO != map[i][j]; j++)
66 {
67 /* for each col */
68 printf("%c", map[i][j]);
69 } /* for each col */
70 DEBUG printf("|");
71 printf("\n");
72 } /* for each row */
73 } /* FUNCTION dump */
74
75 void getInput()
76 {
77 /* FUNCTION getInput */
78 int i;
79 int j;
80 int tmp;
81
82 lineCnt = 0;
83 maxWidth = 0;
84 goalCol = -1;
85 fgets(map[lineCnt], MAX_WIDTH-1, stdin);
86 tmp = strlen(map[lineCnt]);
87 map[lineCnt][tmp-1] = BLANK;
88 map[lineCnt][tmp] = ZERO;
89 while (LINE != map[lineCnt][0])
90 {
91 /* while */
92 for (j=0; (-1 == goalCol) && (tmp>j); j++)
93 {
94 /* look for starting point */
95 if (GOAL == map[lineCnt][j])
96 {
97 /* found it */
98 goalRow = lineCnt;
99 goalCol = j;
100 } /* found it */
101 } /* look for starting point */
102 maxWidth = maxx(maxWidth, tmp);
103 lineCnt++;
104 fgets(map[lineCnt], MAX_WIDTH-1, stdin);
105 tmp = strlen(map[lineCnt]);
106 map[lineCnt][tmp-1] = BLANK;
107 map[lineCnt][tmp] = ZERO;
108 } /* while */
109 /* make every line same length */
110 chr = GOAL;
111 for (i=0; lineCnt>i; i++)
112 {
113 /* for each line */
114 tmp = strlen(map[i]);
115 for (j=0; (GOAL == chr) && (tmp>j); j++)
116 {
117 /* hunt for contour character */
118 if (BLANK != map[i][j])
119 {
120 chr = map[i][j];
121 }
122 } /* hunt for contour character */
123 for (j=tmp; maxWidth>j; j++)
124 {
125 /* for each new char */
126 map[i][j] = BLANK;
127 } /* for each new char */
128 map[i][j] = ZERO;
129 } /* for each line */
130 DEBUG printf("getInput: chr = %c (%d) goal(%d, %d) = %c\n", chr, chr, goalRow, goalCol, GOAL);
131 } /* FUNCTION getInput */
132
133 int isChr(int row, int col)
134 {
135 /* FUNCTION isChr */
136 int retVal = FALSE;
137
138 if ((0<=row) && (0<=col) && (maxWidth>col) && (lineCnt>row))
139 {
140 /* space is valid to check */
141 retVal = (chr == map[row][col]);
142 } /* space is valid to check */
143 return retVal;
144 } /* FUNCTION isChr */
145
146 int findChr(int row, int col)
147 {
148 /* FUNCTION findChr */
149 int retVal;
150
151 retVal = (isChr(row-1, col)) ||
152 (isChr(row+1, col)) ||
153 (isChr(row, col-1)) ||
154 (isChr(row, col+1));
155 return retVal;
156 } /* FUNCTION findChr */
157
158 void clean()
159 {
160 /* FUNCTION clean */
161 int i;
162 int j;
163
164 for (i=0; lineCnt>i; i++)
165 {
166 /* for each row */
167 for (j=0; maxWidth>j; j++)
168 {
169 /* each column */
170 if ((SET == map[i][j]) && (! findChr(i, j)))
171 {
172 /* no adjacnet contour == turn off */
173 map[i][j] = BLANK;
174 } /* no adjacnet contour == turn off */
175 } /* each column */
176 } /* for each row */
177 DEBUG printf("clean: chr = %c\n", chr);
178 } /* FUNCTION clean */
179
180 void trim()
181 {
182 /* FUNCTION trim */
183 int i;
184 int j;
185 int tmp;
186
187 for (i=0; lineCnt>=i; i++)
188 {
189 /* for each row */
190 j=strlen(map[i]) - 1;
191 while ((0<=j) && (BLANK ==map[i][j]))
192 {
193 /* while line ends in blank */
194 map[i][j] = ZERO;
195 j--;
196 } /* while line ends in blank */
197 } /* for each row */
198 } /* FUNCTION trim */
199
200 void fill(int row, int col)
201 {
202 /* FUNCTION fill */
203 DEBUG printf("map[%d][%d] %c\n", row, col, chr);
204 DEBUG printf("map[%d][%d] = (%c)\n", row, col, map[row][col]);
205 DEBUG dump();
206 if ((0<=row) && (0<=col) && (maxWidth>col) && (lineCnt>row) && (BLANK == map[row][col]))
207 {
208 /* found a valid spot */
209 DEBUG printf("Found (%d, %d) [%c]\n", row, col, map[row][col]);
210 map[row][col] = SET;
211 fill(row+1, col);
212 fill(row-1, col);
213 fill(row, col+1);
214 fill(row, col-1);
215 } /* found a valid spot */
216 } /* FUNCTION fill */
217
218 void process()
219 {
220 /* FUNCTION process */
221 DEBUG dump();
222 map[goalRow][goalCol] = BLANK;
223 DEBUG dump();
224 fill(goalRow, goalCol);
225 DEBUG dump();
226 clean();
227 DEBUG dump();
228 trim();
229 dump();
230 } /* FUNCTION process */
231
232 int main()
233 {
234 /* main */
235 int i;
236
237 init();
238 for (i=0; i<numberOfTimes; i++)
239 {
240 /* while */
241 getInput();
242 process();
243 } /* while */
244
245 return EXIT_SUCCESS;
246 } /* main */
247
248