/home/toolbox/public_html/solutions/7/785/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-22
20 * Purpose: fun
21 * Problem: 785
22 */
23
24
25 #define MAX_WIDTH 100
26 #define MAX_HEIGHT 34
27 #define BLANK ' '
28 #define LINE '_'
29 #define NL '\n'
30 #define ZERO 0
31
32 #define maxx(a,b) ( { a>b ? a :b; } )
33
34 char map[MAX_HEIGHT][MAX_WIDTH];
35 int lineCnt;
36
37 char contour;
38 char goal;
39 int maxWidth;
40
41 void dump()
42 {
43 /* FUNCTION dump */
44 int i;
45 int j;
46
47 for (i=0; lineCnt>=i; i++)
48 {
49 /* for each row */
50 for (j=0; ZERO != map[i][j]; j++)
51 {
52 /* for each col */
53 printf("%c", map[i][j]);
54 } /* for each col */
55 printf("\n");
56 } /* for each row */
57 } /* FUNCTION dump */
58
59 int getInput()
60 {
61 /* FUNCTION getInput */
62 int i;
63 int j;
64 int tmp;
65 int dataReadFlag;
66
67 lineCnt = 0;
68 maxWidth = 0;
69 contour = BLANK;
70
71 dataReadFlag = (NULL != fgets(map[lineCnt], MAX_WIDTH-1, stdin));
72 if (dataReadFlag)
73 {
74 /* go ahead and load input case */
75 while (LINE != map[lineCnt][0])
76 {
77 /* while */
78 tmp = strlen(map[lineCnt]) - 1;
79 map[lineCnt][tmp] = ZERO;
80 DEBUG printf("%d:%s:\n", lineCnt, map[lineCnt]);
81 maxWidth = maxx(maxWidth, tmp);
82 /* find contour character */
83 for (i=0; (BLANK == contour) && (tmp>i); i++)
84 {
85 /* for */
86 if (BLANK != map[lineCnt][i])
87 {
88 /* found first contour char */
89 contour = map[lineCnt][i];
90 } /* found first contour char */
91 } /* for */
92 lineCnt++;
93 fgets(map[lineCnt], MAX_WIDTH-1, stdin);
94 } /* while */
95 /* make every line same length */
96 for (i=0; lineCnt>i; i++)
97 {
98 /* for each line */
99 tmp = strlen(map[i]);
100 for (j=tmp; maxWidth>j; j++)
101 {
102 /* for each new char */
103 map[i][j] = BLANK;
104 } /* for each new char */
105 map[i][j] = ZERO;
106 } /* for each line */
107 } /* go ahead and load input case */
108 return dataReadFlag;
109 } /* FUNCTION getInput */
110
111 void trim()
112 {
113 /* FUNCTION trim */
114 int i;
115 int j;
116 int tmp;
117
118 for (i=0; lineCnt>=i; i++)
119 {
120 /* for each row */
121 j=strlen(map[i]) - 1;
122 while ((0<=j) && (BLANK ==map[i][j]))
123 {
124 /* while line ends in blank */
125 map[i][j] = ZERO;
126 j--;
127 } /* while line ends in blank */
128 } /* for each row */
129 } /* FUNCTION trim */
130
131 int check1(int row, int col)
132 {
133 /* FUNCTION check1 */
134 int ret = FALSE;
135
136 if (0<row)
137 {
138 ret = (BLANK == map[row-1][col]);
139 }
140 if (lineCnt>(row+1))
141 {
142 ret = ret || (BLANK == map[row+1][col]);
143 }
144 if (0<col)
145 {
146 ret = ret || (BLANK == map[row][col-1]);
147 }
148 if (maxWidth>(col+1))
149 {
150 ret = ret || (BLANK == map[row][col+1]);
151 }
152 return ret;
153 } /* FUNCTION check1 */
154
155 int check(int row, int col)
156 {
157 /* FUNCTION check */
158 int ret;
159
160 ret = ((0<row) && (0<col) && (maxWidth>col) && (lineCnt>row) && (BLANK == map[row][col]));
161 return ret;
162 } /* FUNCTION check */
163
164 void fill(int row, int col, char mrk)
165 {
166 /* FUNCTION fill */
167
168 DEBUG printf("(row %d) (col %d) (map %c) (mark %c)\n", row, col, map[row][col], mrk);
169 if ((0<row) && (0<col) && (maxWidth>col) && (lineCnt>row) && (mrk == map[row][col]))
170 {
171 /* found a valid spot */
172 map[row][col] = mrk;
173 if (check(row+1, col))
174 {
175 map[row+1][col] = mrk;
176 fill(row+1, col, mrk);
177 }
178 if (check(row-1, col))
179 {
180 map[row-1][col] = mrk;
181 fill(row-1, col, mrk);
182 }
183 if (check(row, col+1))
184 {
185 map[row][col+1] = mrk;
186 fill(row, col+1, mrk);
187 }
188 if (check(row, col-1))
189 {
190 map[row][col-1] = mrk;
191 fill(row, col-1, mrk);
192 }
193 } /* found a valid spot */
194 } /* FUNCTION fill */
195
196 void process()
197 {
198 /* FUNCTION process */
199 int i;
200 int j;
201
202 DEBUG dump();
203 for (i=1; lineCnt>i; i++)
204 {
205 /* for each line */
206 for (j=1; maxWidth>j; j++)
207 {
208 /* for each column */
209 if ((BLANK != map[i][j]) && (contour != map[i][j]))
210 {
211 /* found a mark char */
212 fill(i, j, map[i][j]);
213 } /* found a mark char */
214 } /* for each column */
215 } /* for each line */
216 trim();
217 dump();
218 } /* FUNCTION process */
219
220 int main()
221 {
222 /* main */
223 int moreToDo;
224
225 moreToDo = getInput();
226 while (moreToDo)
227 {
228 /* while */
229 process();
230 moreToDo = getInput();
231 } /* while */
232
233 return EXIT_SUCCESS;
234 } /* main */
235
236