-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSW expert-파핑파핑 지뢰찾기.cpp.txt
125 lines (107 loc) · 1.93 KB
/
SW expert-파핑파핑 지뢰찾기.cpp.txt
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
#include<iostream>
#include<vector>
#include<fstream>
#include<string>
#include<queue>
using namespace std;
char arr[301][301];
bool visit[301][301];
int dx[8] = { -1,1,0,0,-1,-1,1,1 };
int dy[8] = { 0,0,-1,1,-1,1,-1,1 };
int N;
void simulate(int x, int y)
{
int ret = 0;
for (int i = 0; i < 8; i++)
{
int Gx = x + dx[i];
int Gy = y + dy[i];
if (Gx >= 0 && Gx < N&&Gy >= 0 && Gy < N)
{
if (arr[Gx][Gy] == '*')
ret++;
}
}
arr[x][y] = ret+'0';
}
int main()
{
int testcase,countT;
cin >> testcase;
countT = 1;
while (testcase!=0)
{
cin >> N;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
cin >> arr[i][j];
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
visit[i][j] = false;
if (arr[i][j] != '*')
{
simulate(i, j);
}
}
}
queue<pair<int, int>> q;
int ret = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (arr[i][j] == '0' && !visit[i][j])
{
ret++;
q.push(make_pair(i, j));
visit[i][j] = true;
while (!q.empty())
{
int x = q.front().first;
int y = q.front().second;
q.pop();
bool check_insert = true;
for (int k = 0; k < 8; k++)
{
int Gx = x + dx[k];
int Gy = y + dy[k];
if (Gx >= 0 && Gx < N&&Gy >= 0 && Gy < N)
{
if (arr[Gx][Gy] == '*')
check_insert = false;
}
}
if (check_insert)
{
for (int k = 0; k < 8; k++)
{
int Gx = x + dx[k];
int Gy = y + dy[k];
if (Gx >= 0 && Gx < N&&Gy >= 0 && Gy < N && !visit[Gx][Gy])
{
visit[Gx][Gy] = true;
q.push(make_pair(Gx, Gy));
}
}
}
}
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (arr[i][j] != '*' && !visit[i][j])
ret++;
}
}
cout << "#" <<countT++<<" "<<ret<< endl;
testcase--;
}
return 0;
}