Repository navigation
Expand file tree
/
Copy path2014_C_A.cpp
More file actions
127 lines (123 loc) · 2.48 KB
/
Copy path2014_C_A.cpp
File metadata and controls
127 lines (123 loc) · 2.48 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
char board[304][304];
struct Point{
int r, c;
Point():r(0), c(0) {
}
Point(int rr, int cc):r(rr), c(cc) {
}
};
int countMiner(int r, int c, char board[304][304], int n) {
int cnt = 0;
//upper left
if (r > 0 && c > 0 && board[r-1][c-1] == '*') {
cnt++;
}
// above
if (r > 0 && board[r-1][c] == '*') {
cnt++;
}
// upper right
if (r > 0 && c < n-1 && board[r-1][c+1] == '*') {
cnt++;
}
// right
if (c < n-1 && board[r][c+1] == '*') {
cnt++;
}
//bottom right
if (r < n-1 && c < n-1 && board[r+1][c+1] == '*') {
cnt++;
}
// bottom
if (r < n-1 && board[r+1][c] == '*') {
cnt++;
}
// bottom left
if (r < n-1 && c > 0 && board[r+1][c-1] == '*') {
cnt++;
}
// left
if (c > 0 && board[r][c-1] == '*') {
cnt++;
}
return cnt;
}
void revealBoard(int rr, int cc, char board[304][304], int n) {
queue<Point> q;
q.push(Point(rr, cc));
while (!q.empty()) {
Point tmp = q.front();
q.pop();
if (board[tmp.r][tmp.c] == '.') {
board[tmp.r][tmp.c] = countMiner(tmp.r, tmp.c, board, n) + '0';
if (board[tmp.r][tmp.c] == '0') {
int r = tmp.r;
int c = tmp.c;
//upper left
if (r > 0 && c > 0 && board[r-1][c-1] == '.') {
q.push(Point(r-1, c-1));
}
// above
if (r > 0 && board[r-1][c] == '.') {
q.push(Point(r-1, c));
}
// upper right
if (r > 0 && c < n-1 && board[r-1][c+1] == '.') {
q.push(Point(r-1, c+1));
}
// right
if (c < n-1 && board[r][c+1] == '.') {
q.push(Point(r, c+1));
}
//bottom right
if (r < n-1 && c < n-1 && board[r+1][c+1] == '.') {
q.push(Point(r+1, c+1));
}
// bottom
if (r < n-1 && board[r+1][c] == '.') {
q.push(Point(r+1, c));
}
// bottom left
if (r < n-1 && c > 0 && board[r+1][c-1] == '.') {
q.push(Point(r+1, c-1));
}
// left
if (c > 0 && board[r][c-1] == '.') {
q.push(Point(r, c-1));
}
}
}
}
}
int main() {
int T, N;
scanf("%d", &T);
for (int i = 1; i <= T; ++i) {
scanf("%d", &N);
for (int j = 0; j < N; ++j) {
scanf("%s", board[j]);
}
int cnt = 0;
for (int j = 0; j < N; ++j) {
for (int k = 0; k < N; ++k) {
if (board[j][k] == '.' && countMiner(j, k, board, N) == 0) {
revealBoard(j, k, board, N);
cnt += 1;
}
}
}
for (int j = 0; j < N; ++j) {
for (int k = 0; k < N; ++k) {
if (board[j][k] == '.') {
cnt += 1;
}
}
}
cout << "Case #" << i << ": " << cnt << endl;
}
return 0;
}