poj 2251 Dungeon Master( bfs )

题目:http://poj.org/problem?id=2251

简单三维 bfs不解释, 1A,     上代码

 1 #include <iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<stack>
 6 #include<queue>
 7 #include<iomanip>
 8 #include<cmath>
 9 #include<map>
10 #include<vector>
11 #include<algorithm>
12 using namespace std;
13 
14 int a,b,c;
15 int vis[50][50][50],G[50][50][50];
16 int dx[6]={0,0,0,0,-1,1};
17 int dy[6]={0,0,1,-1,0,0};
18 int dz[6]={1,-1,0,0,0,0};
19 struct node
20 {
21     int l,r,c,step;
22 }e,s,pos,next;
23 int bfs(int l,int r,int c)
24 {
25     int i;
26     queue<node>q;
27     next.l=l; next.r=r; next.c=c; next.step=0;
28     vis[l][r][c]=1;
29     q.push(next);
30     while(!q.empty())
31     {
32         pos=q.front();
33         q.pop();
34         for(i=0; i<6; i++)
35         {
36             next.l=pos.l+dx[i]; next.r=pos.r+dy[i];
37             next.c=pos.c+dz[i]; next.step=pos.step+1;
38             if(!vis[next.l][next.r][next.c]&&G[next.l][next.r][next.c])
39             {
40                 vis[next.l][next.r][next.c]=1;
41                 q.push(next);
42             }
43             if(next.l==s.l&&next.r==s.r&&next.c==s.c)
44             return next.step;
45         }
46     }
47     return -1;
48 }
49 int main()
50 {
51     int i,j,k,x;
52     char ch;
53     while(cin>>a>>b>>c&&(a!=0||b!=0||c!=0))
54     {
55         memset(G,0,sizeof(G));
56         memset(vis,0,sizeof(vis));
57         for(i=1; i<=a; i++)
58         for(j=1; j<=b; j++)
59         for(k=1; k<=c; k++)
60         {
61             cin>>ch;
62             if(ch=='.')
63             G[i][j][k]=1;
64             if(ch=='E')
65             {
66                 e.l=i; e.r=j; e.c=k;
67                 G[i][j][k]=1;
68             }
69             if(ch=='S')
70             {
71                 s.l=i; s.r=j; s.c=k;
72                 G[i][j][k]=1;
73             }
74         }
75         x=bfs(e.l,e.r,e.c);
76         if(x==-1)
77         cout<<"Trapped!"<<endl;
78         else
79         printf("Escaped in %d minute(s).\n",x);
80     }
81     return 0;
82 }

 

 

 

posted @ 2013-08-26 17:30  水门  阅读(189)  评论(0编辑  收藏  举报