/home/toolbox/public_html/solutions/102/10279/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-10-12
20 * Purpose: fun
21 * Problem: 10279
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 #define MAX_SIZE 15
29 #define EMPTY 0
30 #define MINE -1
31 #define UNKNOWN -2
32
33 int numberOfTimes;
34 int touched;
35 /* mines will have a border row and column of UNKNOWN all the way around */
36 int mines[MAX_SIZE][MAX_SIZE];
37 int xoff[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
38 int yoff[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };
39 int size;
40
41 void init()
42 {
43 /* FUNCTION init */
44 int i;
45
46 /* mark boundary spots as unknown */
47 for (i=0; MAX_SIZE>i; i++)
48 {
49 /* for each place */
50 mines[i][0] = UNKNOWN;
51 mines[0][i] = UNKNOWN;
52 } /* for each place */
53 scanf("%d ", &numberOfTimes);
54 } /* FUNCTION init */
55
56 void dump()
57 {
58 /* FUNCTION dump */
59 int i,j;
60 printf("size = %d\n", size);
61 for (i=0; MAX_SIZE>i; i++)
62 {
63 /* for each row */
64 for (j=0; MAX_SIZE>j; j++)
65 {
66 /* for each column */
67 printf("%3d ", mines[i][j]);
68 } /* for each column */
69 printf("\n");
70 } /* for each row */
71 } /* FUNCTION dump */
72
73 void getInput()
74 {
75 /* FUNCTION getInput */
76 int i;
77 int j;
78 char line[MAX_SIZE];
79
80 scanf(" %d ", &size);
81 touched = FALSE;
82 /* get mine locations */
83 mines[size+1][size+1] = UNKNOWN;
84 for (i=1; size>=i; i++)
85 {
86 /* for */
87 /* since minefield may be less than max, set outer edges to UNKNOWN */
88 mines[size+1][i] = UNKNOWN;
89 mines[i][size+1] = UNKNOWN;
90 scanf(" %s ", line);
91 DEBUG printf("line = (%s)\n", line);
92 for (j=0; size>j; j++)
93 {
94 /* for each char */
95 switch (line[j])
96 {
97 /* switch */
98 case '.':
99 mines[i][j+1] = UNKNOWN;
100 break;
101 case '*':
102 mines[i][j+1] = MINE;
103 break;
104 } /* switch */
105 } /* for each char */
106 } /* for */
107 DEBUG dump();
108 /* get explored locations */
109 for (i=1; size>=i; i++)
110 {
111 /* for */
112 scanf(" %s ", line);
113 for (j=0; size>j; j++)
114 {
115 /* for each char */
116 if ('x' == line[j])
117 {
118 /* explored */
119 touched = touched || (MINE == mines[i][j+1]);
120 if (MINE == mines[i][j+1])
121 {
122 /* touched a mine */
123 touched = TRUE;
124 } /* touched a mine */
125 else
126 {
127 /* space is clear */
128 mines[i][j+1] = EMPTY;
129 } /* space is clear */
130 } /* explored */
131 } /* for each char */
132 } /* for */
133 DEBUG dump();
134 } /* FUNCTION getInput */
135
136 void process()
137 {
138 /* FUNCTION process */
139 int i;
140 int j;
141 int k;
142 int tot;
143
144 if (touched)
145 {
146 /* mine touched -- just output mines */
147 for (i=1; size>=i; i++)
148 {
149 /* for each row */
150 for (j=1; size>=j; j++)
151 {
152 /* for each column */
153 if (MINE == mines[i][j])
154 {
155 /* mine */
156 printf("*");
157 } /* mine */
158 else
159 {
160 /* number so print */
161 printf(".");
162 } /* number so print */
163 } /* for each column */
164 printf("\n");
165 } /* for each row */
166 } /* mine touched -- just output mines */
167 else
168 {
169 /* no mine touched print board */
170 for (i=1; size>=i; i++)
171 {
172 /* for each row */
173 for (j=1; size>=j; j++)
174 {
175 /* for each column */
176 if (EMPTY == mines[i][j])
177 {
178 /* update count in empty */
179 tot = 0;
180 for (k=0; 8>k; k++)
181 {
182 /* for each of the 8 surrounding spots */
183 if (MINE == mines[i+xoff[k]][j+yoff[k]])
184 {
185 /* found a mine - increment count */
186 tot = tot + 1;
187 } /* found a mine - increment count */
188 } /* for each of the 8 surrounding spots */
189 mines[i][j] = tot;
190 } /* update count in empty */
191 } /* for each column */
192 } /* for each row */
193 /* output result */
194 for (i=1; size>=i; i++)
195 {
196 /* for each row */
197 for (j=1; size>=j; j++)
198 {
199 /* for each column */
200 if (0 > mines[i][j])
201 {
202 /* unknown or a mine */
203 printf(".");
204 } /* unknown or a mine */
205 else
206 {
207 /* number so print */
208 printf("%1d", mines[i][j]);
209 } /* number so print */
210 } /* for each column */
211 printf("\n");
212 } /* for each row */
213 } /* no mine touched print board */
214 } /* FUNCTION process */
215
216 int main()
217 {
218 /* main */
219 int i;
220
221 init();
222 for (i=0; i<numberOfTimes; i++)
223 {
224 /* while */
225 getInput();
226 if (0 < i)
227 {
228 printf("\n");
229 }
230 process();
231 } /* while */
232
233 return EXIT_SUCCESS;
234 } /* main */
235
236