/home/toolbox/public_html/solutions/105/10592/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: 10592 - Freedom Fighter
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 #define MAX_GRID 55
29 #define EMPTY_CHAR '.'
30 #define SECTOR_CHAR '*'
31 #define FREEDOM_CHAR 'B'
32 #define freedom_CHAR 'b'
33 #define ENEMY_CHAR 'P'
34 #define enemy_CHAR 'p'
35 #define BOUNDARY '!'
36 #define VISITED '&'
37
38 int gridSize;
39 char grid[MAX_GRID][MAX_GRID];
40 int fightingGroups;
41 int freedom;
42 int enemy;
43 int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
44 int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
45
46 void init()
47 {
48 /* FUNCTION init */
49 int i;
50
51 for (i=0; (gridSize+2)>i; i++)
52 {
53 /* for i */
54 grid[0][i] = BOUNDARY;
55 grid[gridSize+1][i] = BOUNDARY;
56 grid[i][0] = BOUNDARY;
57 grid[i][gridSize+1] = BOUNDARY;
58 } /* for i */
59 } /* FUNCTION init */
60
61 void dump()
62 {
63 /* FUNCTION dump */
64 int i;
65 int j;
66
67 for (i=0; gridSize+1>=i; i++)
68 {
69 /* for i */
70 for (j=0; gridSize+1>=j; j++)
71 {
72 /* for j */
73 printf("%c", grid[i][j]);
74 } /* for j */
75 printf("\n");
76 } /* for i */
77 printf("\n");
78 } /* FUNCTION dump */
79
80 int getInput()
81 {
82 /* FUNCTION getInput */
83 int i;
84 int j;
85 char tmp;
86 int moreToDo;
87
88 scanf(" %d ", &gridSize);
89 moreToDo = (0 != gridSize);
90
91 if (moreToDo)
92 {
93 /* data to read */
94 for (i=1; gridSize>=i; i++)
95 {
96 /* for i */
97 for (j=1; gridSize>=j; j++)
98 {
99 /* for j */
100 scanf("%c", &grid[i][j]);
101 } /* for j */
102 scanf("%c", &tmp); /* skip over end of line */
103 } /* for i */
104 } /* data to read */
105 return moreToDo;
106 } /* FUNCTION getInput */
107
108 int check(int x, int y)
109 {
110 /* FUNCTION check */
111 int ret;
112
113 ret = (SECTOR_CHAR == grid[x][y]) || (FREEDOM_CHAR == grid[x][y]) || (ENEMY_CHAR == grid[x][y]);
114 return ret;
115 } /* FUNCTION check */
116
117 void fill(int x, int y, char g)
118 {
119 /* FUNCTION fill */
120 int i;
121 if (check(x, y))
122 {
123 /* check */
124 DEBUG printf("fill(%d,%d) %c - %c\n", x, y, grid[x][y], g);
125 if (SECTOR_CHAR == grid[x][y])
126 {
127 grid[x][y] = g;
128 }
129 if (FREEDOM_CHAR == grid[x][y])
130 {
131 grid[x][y] = freedom_CHAR;
132 }
133 if (ENEMY_CHAR == grid[x][y])
134 {
135 grid[x][y] = enemy_CHAR;
136 }
137 for (i=0; 8>i; i++)
138 {
139 /* for each possible move */
140 fill(x+dx[i], y+dy[i], g);
141 } /* for each possible move */
142 } /* check */
143 } /* FUNCTION fill */
144
145 void countGroup(int x, int y, char g)
146 {
147 /* FUNCTION countGroup */
148 int i;
149
150 switch (grid[x][y])
151 {
152 /* process each char */
153 case freedom_CHAR:
154 freedom++;
155 grid[x][y] = VISITED;
156 for (i=0; 8>i; i++)
157 {
158 countGroup(x+dx[i], y+dy[i], g);
159 }
160 break;
161 case enemy_CHAR:
162 enemy++;
163 grid[x][y] = VISITED;
164 for (i=0; 8>i; i++)
165 {
166 countGroup(x+dx[i], y+dy[i], g);
167 }
168 break;
169 default:
170 if (g == grid[x][y])
171 {
172 /* current sector */
173 grid[x][y] = VISITED;
174 for (i=0; 8>i; i++)
175 {
176 countGroup(x+dx[i], y+dy[i], g);
177 }
178 } /* current sector */
179 } /* process each char */
180 } /* FUNCTION countGroup */
181
182 void process()
183 {
184 /* FUNCTION process */
185 int i;
186 int j;
187 char group = 'q';
188
189 init();
190 dump();
191 for (i=1; gridSize>=i; i++)
192 {
193 /* for i */
194 for (j=1; gridSize>=j; j++)
195 {
196 /* for j */
197 if (SECTOR_CHAR == grid[i][j])
198 {
199 /* found a sector */
200 fill(i, j, group);
201 freedom = 0;
202 enemy = 0;
203 dump();
204 countGroup(i, j, group);
205 group++;
206 printf("Sector #%d: contain %d freedom fighter group(s) & %d enemy group(s)\n", (group-'q'), freedom, enemy);
207 } /* found a sector */
208 } /* for j */
209 } /* for i */
210 dump();
211 printf("Total %d group(s) are in fighting position.\n", fightingGroups);
212 } /* FUNCTION process */
213
214 int main()
215 {
216 /* main */
217 int i;
218 int moreToDo;
219
220 moreToDo = getInput();
221 while (moreToDo)
222 {
223 /* while */
224 process();
225 moreToDo = getInput();
226 } /* while */
227
228 return EXIT_SUCCESS;
229 } /* main */
230
231