hdu1312 Red and Black 简单BFS

简单BFS模版题

不多说了。。。。。

直接晒代码哦。。。。

 1 #include<cstdlib>
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<string>
 5 #include<cmath>
 6 #include<queue>
 7 #include<cstring>
 8 #include<fstream>
 9 using namespace std;
10 int n,m;
11 int sum=0;
12 class node
13 {
14        public: 
15        int x;
16        int y;
17 }cur,next;
18 queue<node> q;
19 int map[21][21];
20 int d[4][2]={1,0,-1,0,0,1,0,-1};
21 void init()
22 {
23         memset(map,0,sizeof(map));
24 }
25 void bfs()
26 {
27         while(!q.empty())
28         {
29                 cur=q.front();
30                 q.pop();
31                 for(int i=0;i<4;i++)
32                 {
33                         int x=cur.x+d[i][0];
34                         int y=cur.y+d[i][1];
35                         if(x>n||x<1||y>m||y<1||map[x][y]==0) continue;
36                         next.x=x;
37                         next.y=y;
38                         map[x][y]=0;
39                         sum++;
40                         q.push(next);
41                 }
42         }
43 }
44 int main()
45 {
46         while(scanf("%d%d",&m,&n)!=EOF)
47         {
48                 sum=0;
49                 getchar();
50                 if(n==0&&m==0) break;
51                 for(int i=1;i<=n;i++)
52                 {
53                         for(int j=1;j<=m;j++)
54                         {
55                                 char c;
56                                c=getchar();
57                                if(c=='.') map[i][j]=1;
58                                if(c=='#') map[i][j]=0;
59                                if(c=='@') 
60                                {
61                                         cur.x=i;
62                                         cur.y=j;
63                                         map[i][j]=0;
64                                }
65                         }
66                         getchar();
67                 }
68                 sum++;
69                 q.push(cur);
70                 bfs();
71                 cout<<sum<<endl;
72          }
73          return 0;
74 }
View Code
原文地址:https://www.cnblogs.com/xiaozhuyang/p/hdu1312.html