-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-11244.cpp
72 lines (56 loc) · 1.48 KB
/
uva-11244.cpp
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
//uva 11244
//Counting Stars
#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;
int BFS(vector <string> & sky, int x, int y);
int main(void)
{
int r, c;
while (true) {
cin >> r >> c;
if (r == 0 && c == 0)
break;
vector <string> sky(r);
for (int i = 0; i < r; ++i)
cin >> sky[i];
int n_star = 0;
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j)
if (sky[i][j] == '*'){
int count = BFS(sky, i, j);
if (count == 1)
++n_star;
}
cout << n_star << endl;
}
return 0;
}
int BFS(vector <string> & sky, int x, int y)
{
int xs[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int ys[] = {-1, 0, 1, -1, 1, -1, 0, 1};
int count = 1;
int r = sky.size();
int c = sky[0].size();
queue < pair <int, int> > Queue;
sky[x][y] = '.';
Queue.push(make_pair(x, y));
while (!Queue.empty()) {
pair <int, int> front = Queue.front();
Queue.pop();
for (int i = 0; i < 8; ++i){
int x1 = front.first + xs[i];
int y1 = front.second + ys[i];
if (x1 >= 0 && x1 < r && y1 >= 0 && y1 < c)
if (sky[x1][y1] == '*'){
Queue.push(make_pair(x1, y1));
sky[x1][y1] = '.';
++count;
}
}
}
return count;
}