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