先介绍队列的定义 queue<类型名>变量名,如
queue<int>q,queue或queue<node>que;(struct node(结构体类型));
1、 q.front() 取出队首元素
2、 q.empty() 判断是否为空,时空返回1
3、 q.pop() 删除队首元素
4、 q.push(变量)加入变量
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
bool visit[35][35][35];
char map[35][35][35];
int dir[6][3]={1 , 0, 0, -1, 0, 0, 0, 1, 0, 0, -1, 0, 0, 0, 1, 0, 0, -1};
int l, m, n;
struct node{
int x, y, z;
int step;
};
int bfs(int x, int y, int z){
memset(visit, false, sizeof(visit));
node u;
u.x = x, u.y = y, u.z = z, u.step = 0;
queue<node> q;
q.push(u);
while(!q.empty()){
u = q.front();
if(map[u.x][u.y][u.z] == 'E') return u.step;
q.pop();
for(int i = 0; i < 6; ++i){
node v;
v.x = u.x + dir[i][0];
v.y = u.y + dir[i][1];
v.z = u.z + dir[i][2];
if(!visit[v.x][v.y][v.z] && map[v.x][v.y][v.z] != '#' && v.x >= 0 && v.x < l && v.y >= 0 && v.y < m && v.z >= 0 && v.z < n){
visit[v.x][v.y][v.z] = true;
v.step = u.step + 1;
q.push(v);
}
}
}
return 0;
}
int main(){
int sx, sy, sz;
while(~scanf("%d%d%d%*c", &l, &m, &n) && n && l && m){
memset(map, 0, sizeof(map));
for(int i = 0; i < l; ++i)
for(int j = 0; j < m; ++j){
scanf("%s", map[i][j]);
for(int k = 0; k < n; ++k)
if(map[i][j][k] == 'S'){
sx = i, sy = j, sz = k;
}
}
int step = bfs(sx, sy, sz);
if(step)printf("Escaped in %d minute(s).\n", step);
else printf("Trapped!\n");
}
return 0;
}