首页 > 其他 > 详细

BFS模版

时间:2014-03-22 12:21:35      阅读:603      评论:0      收藏:0      [点我收藏+]

http://acm.hdu.edu.cn/showproblem.php?pid=1241

模版STL

bubuko.com,布布扣
 1 #include<iostream>
 2 #include<queue>
 3 #include<algorithm>
 4 #include<stdio.h>
 5 #include<string.h>
 6 #define maxn 101
 7 using namespace std;
 8 typedef struct
 9 {
10     int x,y;
11 }Node;
12 Node node[maxn];
13 int n,m,cnt=0;
14 int dir[8][2]={0,1,1,0,0,-1,-1,0,1,-1,-1,1,-1,-1,1,1};
15 int vis[maxn][maxn];
16 char map[maxn][maxn];
17 int is_ok(Node s)
18 {
19    if(map[s.x][s.y]==*)
20     return 0;
21    if(vis[s.x][s.y])
22     return 0;
23    if(s.x<0||s.x>=n||s.y<0||s.y>=m)
24     return 0;
25    return 1;
26 }
27 queue<Node>q;
28 void bfs(Node st)
29 {
30    Node now,next;
31    q.push(st);
32  vis[st.x][st.y]=1;
33    while(!q.empty())
34    {
35        now=q.front();
36        for(int i=0;i<8;i++)
37        {
38            next.x=now.x+dir[i][0];
39            next.y=now.y+dir[i][1];
40            if(is_ok(next))
41            {
42                q.push(next);
43                vis[next.x][next.y]=1;
44            }
45        }
46        //printf("%d %d\n",next.x,next.y);
47        q.pop();
48    }
49 }
50 int main()
51 {
52    //freopen("in.txt","r",stdin);
53     while(scanf("%d",&n)!=EOF&&n)
54     {
55         scanf("%d",&m);
56         for(int i=0;i<n;i++)
57             scanf("%s",map[i]);
58         cnt=0;
59         memset(vis,0,sizeof(vis));
60         Node no;
61         for(int i=0;i<n;i++)
62             for(int j=0;j<m;j++)
63         {
64             if(map[i][j]==@&&!vis[i][j])
65                 {
66                     no.x=i;
67                     no.y=j;
68                     bfs(no);
69                     cnt++;
70                 }
71         }
72         printf("%d\n",cnt);
73     }
74     return 0;
75 }
bubuko.com,布布扣

BFS模版,布布扣,bubuko.com

BFS模版

原文:http://www.cnblogs.com/lyf123456/p/3617281.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!