码迷,mamicode.com
首页 > 其他好文 > 详细

ZOJ - 2165 Red and Black

时间:2014-07-18 16:12:09      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   

BFS题,只是没有目标位置,只需记下走过的黑色的块数就行

 1 #include<stdio.h>
 2 #include<string.h>
 3 const int maxn=1000,maxm=25;
 4 int vis[maxm][maxm],mat[maxm][maxm],dir[5][3]={{1,0},{0,-1},{-1,0},{0,1}};
 5 int w,h;
 6 char s[maxm][maxm];
 7 struct node{
 8     int xpos;
 9     int ypos;
10     int step;
11     void init(int x,int y,int z)
12     {
13         xpos=x;
14         ypos=y;
15         step=z;
16     }
17 }que[maxn];
18 int bfs(node sour);
19 int main()
20 {
21     while(scanf("%d%d",&w,&h)==2 && w && h){
22         node source;
23         for(int i=0;i<h;i++)
24             scanf("%s",s[i]);
25         memset(mat,0,sizeof(mat));
26         for(int i=0;i<h;i++)
27         for(int j=0;j<w;j++){
28             if(s[i][j]==#)
29                 mat[i][j]=1;
30             else if(s[i][j]==@){
31                 mat[i][j]=0;
32                 source.xpos=i;
33                 source.ypos=j;
34             }
35             else
36                 continue;
37         }
38       int maxsteps=bfs(source);
39       printf("%d\n",maxsteps);
40     }
41     return 0;
42 }
43 int bfs(node sour)
44 {
45     int maxsteps=1;
46     sour.step=1;
47     int head,tail;
48     que[head=tail=0]=sour;
49     tail++;
50     memset(vis,0,sizeof(vis));
51     vis[sour.xpos][sour.ypos]=1;
52     while(head!=tail){
53         node a=que[head];
54         for(int i=0;i<4;i++){
55             int nx=a.xpos+dir[i][0];
56             int ny=a.ypos+dir[i][1];
57             if(nx>=h || ny>=w || nx<0 || ny<0)
58                 continue;
59             if(vis[nx][ny])
60                 continue;
61             if(mat[nx][ny])
62                 continue;
63             node c;
64             c.init(nx,ny,a.step+1);
65             que[tail]=c;
66             vis[nx][ny]=1;
67             maxsteps++;
68             tail=(tail+1)%maxn;
69         }
70         head=(head+1)%maxn;
71     }
72     return maxsteps;
73 }

ZOJ - 2165 Red and Black,布布扣,bubuko.com

ZOJ - 2165 Red and Black

标签:style   blog   color   os   io   for   

原文地址:http://www.cnblogs.com/BMESwimming/p/3851871.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!