3Ddungeon-------三维搜索-----偷个懒 把 亡命逃窜 的代码修改了一下 拿来用了

题 很简单  就是给一个   三维的迷宫然后 开你起始地点 S 问你能不能到达 出口 E 能的话 需要多长时间 ?

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<iostream>
 5 #include<algorithm>
 6 #include<queue>
 7 #include<vector>
 8 #include<set>
 9 #include<stack>
10 #include<string>
11 #include<sstream>
12 #include<map>
13 #include<cctype>
14 using namespace std;
15 int a,b,c,visited[35][35][55],mark;
16 char a1[35][35][35];
17 int b1[6][3]={1,0,0,-1,0,0,0,0,1,0,0,-1,0,1,0,0,-1,0};
18 struct node
19 {
20     int x,y,z,step;
21 };
22 queue<node>Q;
23 void BFS(int x,int y,int z)
24 {
25     node q={x,y,z,0};
26     visited[x][y][z]=1;
27     Q.push(q);
28     while(!Q.empty())
29     {
30         node e=Q.front();
31         Q.pop();
32         for(int i=0;i<6;i++)
33         {
34             if(mark)
35                 return ;
36             q.x=e.x+b1[i][0],q.y=e.y+b1[i][1],q.z=e.z+b1[i][2];
37             if(q.x>=0&&q.x<a&&q.y>=0&&q.y<b&&q.z>=0&&q.z<c&&!visited[q.x][q.y][q.z]&&a1[q.x][q.y][q.z]!='#')   //  没有 超出 范围   并且 没有访问 且  不是墙
38             {
39                 visited[q.x][q.y][q.z]=1;
40                 q.step=e.step+1;
41                 Q.push(q);
42                 if(a1[q.x][q.y][q.z]=='E')
43                 {
44                     mark=q.step;
45                 }
46             }
47         }
48     }
49 }
50 int main()
51 {
52     int t,sx,sy,sz;
53     while(scanf("%d%d%d",&a,&b,&c),(a||b||c))
54     {
55         for(int i=0;i<a;i++)
56             for(int j=0;j<b;j++)
57             for(int q=0;q<c;q++)
58         {
59             scanf("  %c",&a1[i][j][q]);
60             if(a1[i][j][q]=='S')
61             {
62                 sz=i;
63                 sx=j;
64                 sy=q;
65             }
66         }
67         memset(visited,0,sizeof(visited));
68         mark=0;
69         while(!Q.empty())
70             Q.pop();
71         BFS(sx,sy,sz);
72         if(mark)
73             printf("Escaped in %d minute(s).
",mark);
74         else
75             printf("Trapped!
");
76     }
77     return 0;
78 }
原文地址:https://www.cnblogs.com/A-FM/p/5349951.html