/home/toolbox/public_html/solutions/4/469/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: 09/17/2019
20 * Purpose: fun
21 * Problem: 469
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28
29 #define MAX_LINE 110
30 #define MAX_READ 105
31 #define BLANK ' '
32 #define LAND 'L'
33 #define WATER 'W'
34 #define wATER 'w'
35 #define NoQuery -1
36
37
38 int numberOfTimes;
39 int offRow[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
40 int offCol[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
41 char line[MAX_LINE];
42 char map[MAX_LINE][MAX_LINE];
43 int rowCnt;
44 int colCnt;
45 int qr, qc; /* query row and column */
46
47 void init()
48 {
49 /* FUNCTION init */
50 scanf(" %d ", &numberOfTimes); /* whitepace after %d will gobble up first blank line */
51 } /* FUNCTION init */
52
53 void dump()
54 {
55 /* FUNCTION dump */
56 int i,j;
57
58 printf("rowCnt = %d colCnt = %d\n", rowCnt, colCnt);
59 for (i=0; rowCnt>=i; i++)
60 {
61 /* for each row */
62 printf("%3d: ", i);
63 for (j=0; colCnt>j; j++)
64 {
65 /* for each column */
66 printf("%c", map[i][j]);
67 } /* for each column */
68 printf("\n");
69 } /* for each row */
70 printf("\n");
71 } /* FUNCTION dump */
72
73 void getQuery()
74 {
75 /* FUNCTION getQuery */
76 int sl;
77
78 DEBUG printf("getQuery: line |%s|\n", line);
79 sl = strlen(line);
80 if (3 > sl)
81 {
82 /* no query */
83 qr = NoQuery;
84 } /* no query */
85 else
86 {
87 /* good query here */
88 sscanf(line, " %d %d ", &qr, &qc);
89 line[0] = 0;
90 fgets(line, MAX_READ, stdin);
91 } /* good query here */
92 } /* FUNCTION getQuery */
93
94 void getInput()
95 {
96 /* FUNCTION getInput */
97 char * tmp;
98 int i;
99 int j;
100
101 map[1][0] = LAND;
102 tmp = fgets(&map[1][1], MAX_READ, stdin);
103 if (NULL == tmp)
104 {
105 map[1][0] = 0;
106 }
107 else
108 {
109 /* valid line read */
110 colCnt = strlen(map[1]);
111 rowCnt = 1;
112 DEBUG printf("colCnt = %d\n", colCnt);
113 /* loop until firt query reached */
114 while ((LAND == map[rowCnt][1]) || (WATER == map[rowCnt][1]))
115 {
116 /* loop until firt query reached */
117 map[rowCnt][0] = LAND; /* set inner barrier */
118 map[rowCnt][colCnt - 1] = LAND; /* wipe out newline */
119 DEBUG printf("getInput: map[%d] = |%s|\n", rowCnt, map[rowCnt]);
120 rowCnt++;
121 fgets(&map[rowCnt][1], MAX_READ, stdin);
122 } /* loop until firt query reached */
123 /* found neither land or water -- thi must be first query */
124 for (i=0, j=1; 31 < map[rowCnt][j]; j++, i++)
125 {
126 line[i] = map[rowCnt][j];
127 }
128 line[i] = 0;
129 /* fill in row 0 and row rowCnt as guard */
130 for (i=0; colCnt>i; i++)
131 {
132 /* fill lines */
133 map[0][i] = LAND;
134 map[rowCnt][i] = LAND;
135 } /* fill lines */
136 getQuery();
137 } /* valid line read */
138 } /* FUNCTION getInput */
139
140 int countLake(int qr, int qc, char fnd, char rplc)
141 {
142 /* FUNCTION countLake */
143 int tot = 0;
144 int i;
145
146 DEBUG printf("countlake: (qr=%d) (qc=%d) (fnd=%c) (rplc=%c) (map=%c)\n", qr, qc, fnd, rplc, map[qr][qc]);
147 if (fnd == map[qr][qc])
148 {
149 /* found water */
150 map[qr][qc] = rplc; /* mark water found */
151 tot = 1;
152 for (i=0; 8>i; i++)
153 {
154 /* look at each cell around this water cell */
155 tot = tot + countLake(qr+offRow[i], qc+offCol[i], fnd, rplc);
156 } /* look at each cell around this water cell */
157 } /* found water */
158 return tot;
159 } /* FUNCTION countLake */
160
161 void process()
162 {
163 /* FUNCTION process */
164 int sz;
165 char x1, x2;
166
167 while (NoQuery != qr)
168 {
169 /* proces query */
170 DEBUG printf("Query: (%d, %d)\n", qr, qc);
171 DEBUG dump();
172 x1 = map[qr][qc];
173 x2 = (WATER == x1) ? wATER : WATER;
174 sz = countLake(qr, qc, x1, x2);
175 printf("%d\n", sz);
176 getQuery();
177 } /* proces query */
178 } /* FUNCTION process */
179
180 int main()
181 {
182 /* main */
183 int i;
184
185 init();
186 for (i=0; i<numberOfTimes; i++)
187 {
188 /* while */
189 getInput();
190 if (0 != i)
191 {
192 printf("\n");
193 }
194 process();
195 } /* while */
196
197 return EXIT_SUCCESS;
198 } /* main */
199
200