L

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <queue>
 4 using namespace std;
 5 
 6 int n, m;
 7 const int maxn = 105;
 8 string s[maxn];
 9 int f[8][2] = { {0, 1},{1, 0},{-1, 0},{0, -1},{1, 1},{-1, -1},{1, -1},{-1, 1} };
10 
11 struct node{
12     int x, y;
13 }now, last;
14 queue<node>q;
15 void bfs(int x, int y){
16     while(!q.empty())   q.pop();
17     last.x = x;
18     last.y = y;
19     q.push(last);
20     s[x][y] = '*';
21     while(!q.empty()){
22         last = q.front(); q.pop();
23         for(int i = 0;i < 8;i++){
24             now.x = last.x + f[i][0];
25             now.y = last.y + f[i][1];
26 
27             if(now.x < 0 || now.y < 0|| now.x >= n || now.y >= m|| s[now.x][now.y] == '*')
28                 continue;
29             q.push(now);
30             s[now.x][now.y] = '*';
31         }
32     }
33 }
34 
35 int main(){
36     while(cin >> n >> m){
37         if(n == 0 && m == 0 )
38             break;
39         for(int i = 0;i < n;i++)
40             cin >> s[i];
41         int ans = 0;
42         for(int i = 0;i < n;i++){
43             for(int j = 0;j < m;j++){
44                 if(s[i][j] == '@'){
45                     ans++;
46                     bfs(i, j);
47                 }
48             }
49         }
50         cout << ans << endl;
51     }
52     return 0;
53 }
原文地址:https://www.cnblogs.com/ouyang_wsgwz/p/8970925.html