标签:
解题思路:三维数组,简单宽搜,注意细节。
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 using namespace std; 6 const int maxn = 35; 7 int dir[6][3] = {0, 0, 1, 0, 0, -1, 0, 1, 0, 0, -1, 0, 8 1, 0, 0, -1, 0, 0}; //上、下 9 int L, R, C, sz, sx, sy; 10 char mapp[maxn][maxn][maxn]; 11 12 struct node{ 13 int z, x, y, cnt; 14 }p, n; 15 16 queue<node> q; 17 18 int BFS(int a, int b, int c) 19 { 20 while(!q.empty()) q.pop(); //清空队列 21 mapp[a][b][c] = ‘#‘; //把起始点标记为# 22 p.z = a, p.x = b, p.y = c, p.cnt = 0; 23 q.push(p); 24 25 while(!q.empty()) 26 { 27 p = q.front(); 28 q.pop(); 29 30 for(int i = 0; i < 6; i++) 31 { 32 int zz = p.z + dir[i][0]; 33 int xx = p.x + dir[i][1]; 34 int yy = p.y + dir[i][2]; 35 36 if(mapp[zz][xx][yy] == ‘#‘) continue; //走过的或不能走的路就进行下一次循环 37 if(mapp[zz][xx][yy] == ‘E‘) return p.cnt + 1; //碰到‘E’就返回,记住加1 38 mapp[zz][xx][yy] = ‘#‘; 39 n.z = zz, n.x = xx, n.y = yy, n.cnt = p.cnt + 1; 40 q.push(n); 41 } 42 } 43 return -1; 44 } 45 46 int main() 47 { 48 char str[maxn]; 49 while(~scanf("%d %d %d", &L, &R, &C) && (L || R || C)) 50 { 51 memset(mapp, ‘#‘, sizeof(mapp)); 52 for(int i = 1; i <= L; i++) 53 { 54 for(int j = 1; j <= R; j++) 55 { 56 scanf("%s", str+1); //这里记住要加1,scanf不吃回车键。 57 for(int k = 1; k <= C; k++) 58 { 59 mapp[i][j][k] = str[k]; 60 if(mapp[i][j][k] == ‘S‘) sz = i, sx = j, sy = k; 61 } 62 } 63 } 64 65 int ans = BFS(sz, sx, sy); 66 67 if(ans == -1) printf("Trapped!\n"); //说明没有找到出口 68 else printf("Escaped in %d minute(s).\n", ans); 69 } 70 return 0; 71 }
标签:
原文地址:http://www.cnblogs.com/loveprincess/p/4854422.html