标签:
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 }
标签:
原文地址:http://www.cnblogs.com/wangyuhao/p/4662089.html