/home/toolbox/public_html/solutions/8/852/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: 2020-10-31
20 * Purpose: fun
21 * Problem: 852 - Deciding Victory in Go
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 #define MAX_GRID 11
29 #define X 100
30 #define BLACK 100
31 #define O 200
32 #define WHITE 200
33 #define BOUNDARY -1
34 #define EMPTY 0
35 #define BOARD_START 1
36 #define BOARD_END 9
37
38 int numberOfTimes;
39 int grid[MAX_GRID][MAX_GRID];
40 int black;
41 int white;
42 int blackBounded;
43 int whiteBounded;
44 int sz;
45
46 void init()
47 {
48 /* FUNCTION init */
49 int i;
50 int j;
51
52 scanf("%d ", &numberOfTimes);
53 for (i=0; MAX_GRID>i; i++)
54 {
55 /* for i */
56 grid[i][0] = BOUNDARY;
57 grid[i][10] = BOUNDARY;
58 grid[0][i] = BOUNDARY;
59 grid[10][i] = BOUNDARY;
60 } /* for i */
61 } /* FUNCTION init */
62
63 void initEach()
64 {
65 /* FUNCTION initEach */
66 int i;
67 int j;
68
69 black = 0;
70 white = 0;
71 for (i=BOARD_START; BOARD_END>=i; i++)
72 {
73 /* for i */
74 for (j=BOARD_START; BOARD_END>=j; j++)
75 {
76 /* for j */
77 grid[i][j] = EMPTY;
78 } /* for j */
79 } /* for i */
80 } /* FUNCTION initEach */
81
82 void dump()
83 {
84 /* FUNCTION dump */
85 int i;
86 int j;
87
88 for (i=BOARD_START; BOARD_END>=i; i++)
89 {
90 /* for i */
91 for (j=BOARD_START; BOARD_END>=j; j++)
92 {
93 /* for j */
94 printf(" %3d", grid[i][j]);
95 } /* for j */
96 printf("\n");
97 } /* for i */
98 printf("\n");
99 } /* FUNCTION dump */
100
101 void getInput()
102 {
103 /* FUNCTION getInput */
104 int i;
105 int j;
106 char tmp;
107
108 for (i=BOARD_START; BOARD_END>=i; i++)
109 {
110 /* for i */
111 for (j=BOARD_START; BOARD_END>=j; j++)
112 {
113 /* for j */
114 scanf("%c", &tmp);
115 switch (tmp)
116 {
117 /* switch */
118 case 'X':
119 grid[i][j] = X;
120 black++;
121 break;
122 case 'O':
123 grid[i][j] = O;
124 white++;
125 break;
126 } /* switch */
127 } /* for j */
128 scanf("%c", &tmp); /* skip over end of line */
129 } /* for i */
130 } /* FUNCTION getInput */
131
132 int check(int x, int y)
133 {
134 /* FUNCTION check */
135 int ret;
136
137 ret = (EMPTY == grid[x][y]);
138 if (ret)
139 {
140 /* spot is empty */
141 blackBounded = blackBounded ||
142 (BLACK == grid[x-1][y]) || (BLACK == grid[x+1][y]) ||
143 (BLACK == grid[x][y-1]) || (BLACK == grid[x][y+1]);
144 whiteBounded = whiteBounded ||
145 (WHITE == grid[x-1][y]) || (WHITE == grid[x+1][y]) ||
146 (WHITE == grid[x][y-1]) || (WHITE == grid[x][y+1]);
147 } /* spot is empty */
148 return ret;
149 } /* FUNCTION check */
150
151 void fill(int x, int y, int t)
152 {
153 /* FUNCTION fill */
154 int i;
155 int j;
156
157 if (check(x, y))
158 {
159 /* check */
160 grid[x][y] = t;
161 sz++;
162 fill(x-1, y, t);
163 fill(x+1, y, t);
164 fill(x, y-1, t);
165 fill(x, y+1, t);
166 } /* check */
167 } /* FUNCTION fill */
168
169 void process()
170 {
171 /* FUNCTION process */
172 int i;
173 int j;
174 int fillValue = 10;
175
176 DEBUG printf("Black %d White %d\n", black, white);
177 for (i=BOARD_START; BOARD_END>=i; i++)
178 {
179 /* for i */
180 for (j=BOARD_START; BOARD_END>=j; j++)
181 {
182 /* for j */
183 if (EMPTY == grid[i][j])
184 {
185 /* look for empties */
186 blackBounded = FALSE;
187 whiteBounded = FALSE;
188 sz = 0;
189 fill(i, j, fillValue);
190 if ((blackBounded) && (! whiteBounded))
191 {
192 black = black + sz;
193 }
194 if ((whiteBounded) && (! blackBounded))
195 {
196 white = white + sz;
197 }
198 fillValue++;
199 } /* look for empties */
200 } /* for j */
201 } /* for i */
202 DEBUG dump();
203 printf("Black %d White %d\n", black, white);
204 } /* FUNCTION process */
205
206 int main()
207 {
208 /* main */
209 int i;
210
211 init();
212 for (i=0; i<numberOfTimes; i++)
213 {
214 /* while */
215 initEach();
216 getInput();
217 process();
218 } /* while */
219
220 return EXIT_SUCCESS;
221 } /* main */
222
223