/home/toolbox/public_html/solutions/4/469/c.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-09-16
20 * Purpose: fun
21 * Problem: 469
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 #define MAX_CHARS 108
29 #define BLANK ' '
30 #define WATER 'W'
31 #define Water 'w'
32 #define LAND 'L'
33 #define EndOfLine 0
34 #define lastQuery -1
35
36 int numberOfTimes;
37 char map[MAX_CHARS][MAX_CHARS];
38 char line[MAX_CHARS];
39 int cnt;
40 int rowCnt;
41 int colCnt;
42 int query[2];
43 int offRow[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
44 int offCol[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
45
46 void init()
47 {
48 /* FUNCTION init */
49 int tmp;
50
51 scanf("%d ", &numberOfTimes);
52 do
53 {
54 /* skip blank line */
55 fgets(&map[rowCnt][1], MAX_CHARS-3, stdin);
56 } /* skip blank line */
57 while ((LAND != map[1][1]) && (WATER != map[1][1]));
58 map[rowCnt][strlen(map[rowCnt])-1] = 0;
59 map[1][0] = LAND;
60 DEBUG printf("init: last [%s]\n", map[1]);
61 rowCnt = 1;
62
63 } /* FUNCTION init */
64
65 void dump()
66 {
67 /* FUNCTION dump */
68 int i;
69 int j;
70
71 printf("\n");
72 for (i=0; rowCnt>=i; i++)
73 {
74 /* each row */
75 printf("%3d: ", i);
76 for (j=0; colCnt>j; j++)
77 {
78 /* each column */
79 printf("%c", map[i][j]);
80 } /* each column */
81 printf("\n");
82 } /* each row */
83 printf("\n");
84 } /* FUNCTION dump */
85
86 void getQuery()
87 {
88 /* FUNCTION getQuery */
89 int tmp;
90 int i;
91
92 DEBUG printf("getQuery [%s]\n", line);
93 tmp = sscanf(line, " %d %d ", &query[0],&query[2]);
94 if (0 < tmp)
95 {
96 /* data read */
97 map[rowCnt][1] = EndOfLine;
98 fgets(&map[rowCnt][1], MAX_CHARS-3, stdin);
99 map[rowCnt][strlen(map[rowCnt])-1] = 0;
100 tmp = strlen(&map[rowCnt][1]);
101 if (1 > tmp)
102 {
103 /* end of query hit -- should never happen */
104 map[1][0] = EndOfLine;
105 map[1][1] = EndOfLine;
106 rowCnt = 1;
107 } /* end of query hit -- should never happen */
108 } /* data read */
109 else
110 {
111 /* no more queries */
112 query[0] = lastQuery;
113 tmp = strlen(line);
114 printf("rowCnt %d [%s]:%d\n", rowCnt+1, map[rowCnt+1], tmp);
115 for (i=0; tmp>i; i++)
116 {
117 map[1][i+1] = line[i]; /* copy last input into row 1 */
118 }
119 map[1][0] = LAND;
120 map[1][tmp+1] = EndOfLine;
121 rowCnt = 1;
122 } /* no more queries */
123 } /* FUNCTION getQuery */
124
125 void getInput()
126 {
127 /* FUNCTION getInput */
128 int i;
129 int tmp;
130
131 printf("getInput: (1) %d [%s]\n", rowCnt, map[1]);
132 colCnt = strlen(map[1]);
133 tmp = colCnt;
134 map[1][colCnt] = LAND;
135 colCnt++;
136 map[1][colCnt] = EndOfLine;
137 rowCnt = 2;
138 fgets(&map[rowCnt][1], MAX_CHARS-3, stdin);
139 map[rowCnt][strlen(map[rowCnt])-1] = 0;
140 while ((LAND == map[rowCnt][1]) || (WATER == map[rowCnt][1]))
141 {
142 /* got another line of the map */
143 map[rowCnt][0] = LAND;
144 printf("getInput: (2) %d [%s]\n", rowCnt, map[rowCnt]);
145 map[rowCnt][tmp] = LAND;
146 map[rowCnt][colCnt] = EndOfLine;
147 rowCnt++;
148 fgets(&map[rowCnt][1], MAX_CHARS-3, stdin);
149 map[rowCnt][strlen(map[rowCnt])-1] = 0;
150 } /* got another line of the map */
151 tmp = strlen(&map[rowCnt][1]);
152 printf("(tmp = %d) (rowCnt = %d) [%s]\n", tmp, rowCnt, map[rowCnt]);
153 for (i=0; tmp>=i; i++)
154 {
155 line[i] = map[rowCnt][i+1]; /* put queryinto line buffer */
156 }
157 getQuery();
158 for (i=0; colCnt>i; i++)
159 {
160 /* set first and last row to LAND */
161 map[0][i] = LAND;
162 map[rowCnt][i] = LAND;
163 } /* set first and last row to LAND */
164 } /* FUNCTION getInput */
165
166 int countLake(int qr, int qc, char fnd, char rplc)
167 {
168 /* FUNCTION countLake */
169 int tot = 0;
170 int i;
171
172 DEBUG printf("countlake: (qr=%d) (qc=%d) (fnd=%c) (rplc=%c) (map=%c)\n", qr, qc, fnd, rplc, map[qr][qc]);
173 if (fnd == map[qr][qc])
174 {
175 /* found water */
176 map[qr][qc] = rplc; /* mark water found */
177 tot = 1;
178 for (i=0; 8>i; i++)
179 {
180 /* look at each cell around this water cell */
181 tot = tot + countLake(qr+offRow[i], qc+offCol[i], fnd, rplc);
182 } /* look at each cell around this water cell */
183 } /* found water */
184 return tot;
185 } /* FUNCTION countLake */
186
187 void process()
188 {
189 /* FUNCTION process */
190 int sz;
191 char x1;
192 char x2;
193
194 dump();
195 while (lastQuery != query[0])
196 {
197 /* process query */
198 DEBUG printf("Query: %d %d\n", query[0], query[1]);
199 x1 = map[query[0]][query[1]];
200 x2 = (WATER == x1) ? Water : WATER;
201 sz = countLake(query[0], query[1], x1, x2);
202 dump();
203 DEBUG printf("%d %d ", query[0], query[1]);
204 printf("%d\n", sz);
205 getQuery(rowCnt+1);
206 } /* process query */
207 } /* FUNCTION process */
208
209 int main()
210 {
211 /* main */
212 int i;
213
214 init();
215 for (i=0; i<numberOfTimes; i++)
216 {
217 /* while */
218 getInput();
219 if (0 != i)
220 {
221 printf("\n");
222 }
223 process();
224 } /* while */
225
226 return EXIT_SUCCESS;
227 } /* main */
228
229