-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuva10189.c
65 lines (62 loc) · 1.84 KB
/
uva10189.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* UVA 10189 - Minesweeper
* https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=13&page=show_problem&problem=1130
* $ g++ uva10189.c -lm -lcrypt -O2 -pipe -DONLINE_JUDGE
*/
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
const char SAFE_SQ = '.';
const char MINE_SQ = '*';
int field[MAX_SIZE][MAX_SIZE];
char buffer[MAX_SIZE + 1];
int n = 0;
int m = 0;
int field_count = 1;
char c;
while(scanf("%d %d", &n, &m) != EOF) {
if (n == 0 && m == 0) {
break;
}
if (field_count > 1) {
printf("\n");
}
printf("Field #%d:\n", field_count++);
// Read in field.
for(int j = 0; j < n; j++) {
scanf("%s", buffer);
for(int i = 0; i < m; i++) {
if(buffer[i] == MINE_SQ) {
field[j][i] = 1;
} else if(buffer[i] == SAFE_SQ) {
field[j][i] = 0;
} else {
printf("Error: %c read.", buffer[i]);
return 1;
}
}
}
// Count mines.
for(int j = 0; j < n; j++) {
for(int i = 0; i < m; i++) {
int count = 0;
for(int y = j - 1; y <= j + 1; y++) {
for(int x = i - 1; x <= i + 1; x++) {
if(y>=0 && y <n && x>=0 && x<m) {
count += field[y][x];
}
}
}
// The center square is improperly counted above.
//count -= field[j][i];
if(field[j][i]) {
printf("*");
} else {
printf("%d", count);
}
}
printf("\n");
}
}
return 0;
}