Oil Deposits HDU - 1241 (简单bfs)(找有多少个连通块)

题目链接:https://cn.vjudge.net/problem/HDU-1241

注意:搜索八个方向

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 #include <stack>
 6 #include <algorithm>
 7 #include <cmath>
 8 #include <map>
 9 #define mem(a,b) memset(a,b,sizeof(a));
10 using namespace std;
11 #define INF 0x3f3f3f3f
12 typedef long long ll;
13 int dir[8][2] = {0,1,0,-1,1,0,-1,0,1,-1,1,1,-1,1,-1,-1};
14 const int maxn = 5000005;
15 int r,c,vis[105][105];
16 string s[105];
17 struct node{
18     int x,y;
19     node(int x1,int y1):x(x1),y(y1){};
20 };
21 void bfs(int x,int y) {
22     queue<node>q;
23     vis[x][y] = 1;
24     q.push(node(x,y));
25     while(!q.empty()) {
26         node temp = q.front();
27         q.pop();
28         for(int i = 0; i < 8; i++) {
29             int fx = temp.x + dir[i][0],fy = temp.y + dir[i][1];
30             if(fx >=0 && fx < r && fy >=0 && fy < c && !vis[fx][fy] && s[fx][fy] == '@')
31             {
32                 vis[fx][fy] = 1;
33                 q.push(node(fx,fy));
34             }
35         }
36     }
37 }
38 int main()
39 {
40     while(cin >> r >> c && r &&  c) {
41         mem(vis,0);
42         for(int i = 0; i < r; i++) {
43             cin >> s[i];
44         }
45         int ans = 0;
46         for(int i = 0; i < r; i++) {
47             for(int j = 0; j < c; j++) {
48                 if(s[i][j] == '@' && !vis[i][j]) {
49                     bfs(i,j);
50                     ans++;
51                 }
52             }
53         }
54         cout << ans << endl;
55     }
56     return 0;
57 }

 

posted on 2019-08-19 14:21  一只小毛球  阅读(136)  评论(0编辑  收藏  举报

导航