-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVA_00572.cpp
61 lines (50 loc) · 1.83 KB
/
UVA_00572.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
#include <iostream>
#include <queue>
using namespace std;
int main(void)
{
int n, m, i, j, i1, j1, cnt;
queue <pair<int, int>> q;
while(1)
{
cin >> m >> n;
if(m == 0)
break;
string s[m];
cnt = 0;
for(i = 0; i < m; i++)
cin >> s[i];
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
if(s[i][j] == '@')
{
cnt++;
q.push(make_pair(i, j));
while(!q.empty())
{
i1 = q.front().first;
j1 = q.front().second;
q.pop();
s[i1][j1] = '*';
if(i1 > 0 && j1 > 0 && s[i1-1][j1-1] == '@')
q.push(make_pair(i1-1, j1-1));
if(i1 > 0 && j1 < n-1 && s[i1-1][j1+1] == '@')
q.push(make_pair(i1-1, j1+1));
if(i1 < m-1 && j1 > 0 && s[i1+1][j1-1] == '@')
q.push(make_pair(i1+1, j1-1));
if(i1 < m-1 && j1 < n-1 && s[i1+1][j1+1] == '@')
q.push(make_pair(i1+1, j1+1));
if(i1 > 0 && s[i1-1][j1] == '@')
q.push(make_pair(i1-1, j1));
if(j1 > 0 && s[i1][j1-1] == '@')
q.push(make_pair(i1, j1-1));
if(i1 < m-1 && s[i1+1][j1] == '@')
q.push(make_pair(i1+1, j1));
if(j1 < n-1 && s[i1][j1+1] == '@')
q.push(make_pair(i1, j1+1));
}
}
cout << cnt << "\n";
}
return 0;
}