注:本人英语很渣,题目大意大多来自百度~=0=
 
题目大意:这题是一个三维的迷宫题目,其中用'.'表示空地,'#'表示障碍物,'S'表示起点,'E'表示终点,求从起点到终点的最小移动次数.
 
对于题目给出数据的含义就是输入l,r,c,分别代表迷宫有l层,每层长宽分别是c,r。
 
对于数据以可以这样移动
(1,1,1)->(1,1,2)->(1,1,3)->(1,1,4)->(1,1,5)->(1,2,5)
->(1,3,5)->(1,3,4)->(1,4,4)->(2,4,4)->(2,4,5)->(3,4,,5)
共11步就可以到达终点
对于数据二明显不能到达,则输出Trapped
 
简单BFS  下面是代码
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <cstdlib>
 6 #include <cmath>
 7 #include <cctype>
 8 using namespace std;
 9  
10 bool maps[32][32][32];//maps用来记录地图可不可以走
11 int dir[6][3] = {0,1,0, 1,0,0, 0,-1,0, -1,0,0, 0,0,1, 0,0,-1};//方向
12 int L, R, C;
13  
14 struct node
15 {
16     int i, j, k;
17     int step;//step用来保存步数
18 } s, e; //s用来保存起点,e用来保存终点
19  
20 void BFS()
21 {
22     int x, y, z, i;
23     struct node ss;
24     queue<node>q;
25     q.push(s);
26  
27     while(!q.empty())
28     {
29         ss = q.front();
30         q.pop();
31         if(ss.i == e.i && ss.j == e.j && ss.k == e.k)//找到终点,输出步数
32         {
33             printf("Escaped in %d minute(s).\n", ss.step);
34             return;
35         }
36         for(i = 0; i < 6; i++)
37         {
38             x = ss.i + dir[i][0];
39             y = ss.j + dir[i][1];
40             z = ss.k + dir[i][2];
41             if(x >= 0 && x < L && y >= 0 && y < R && z >= 0 && z < C && maps[x][y][z])
42             {
43                 struct node v;
44                 maps[x][y][z] = false;//走过点标记, 防止重复搜索
45                 v = {x, y, z, ss.step + 1};
46                 q.push(v);
47             }
48         }
49     }
50    
51     printf("Trapped!\n");//全部搜索完毕,并不能达到终点
52 }
53  
54 int main()
55 {
56  
57     char ch;
58  
59     while(scanf("%d %d %d", &L, &R, &C), L || R || C)
60     {
61         memset(maps, false, sizeof(maps));
62  
63         for(int i = 0; i < L; i++)
64         {
65             for(int j = 0; j < R; j++)
66             {
67                 scanf(" ");
68                 for(int k = 0; k < C; k++)
69                 {
70                     scanf("%c", &ch);
71                     if(ch == '.') maps[i][j][k] = true;
72                     if(ch == 'S') s = {i, j, k, 0};
73                     if(ch == 'E') e = {i, j, k, 0}, maps[i][j][k] = true;
74                 }
75             }
76         }
77         BFS();
78     }
79    
80     return 0;
81 }

 

 

posted on 2015-07-20 17:38  毕竟我是王宇浩  阅读(204)  评论(0编辑  收藏  举报