BFS模版

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

STL queue模版

 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 }

 数组模拟队列模版

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

 

posted @ 2014-03-22 11:27  清风旋叶  阅读(124)  评论(0编辑  收藏  举报