View Code of Problem 1313

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 35;
char map[maxn][maxn][maxn];
bool used[maxn][maxn][maxn];
int arr[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
struct Node
{
    int x, y, z;            
    int step;                
}start,end,cur;
int L,R,C;
void bfs()
{
	Node node;
	queue<Node> q;
	q.push(start);
	while(!q.empty()){
		cur=q.front();
		q.pop();
		if(cur.x==end.x&&cur.y==end.y&&cur.z==end.z){
			return;
		}
		else
		{
			used[cur.x][cur.y][cur.z]=true;
			for(int i=0;i<6;i++){
				node.x=cur.x+arr[i][0];
				node.y=cur.y+arr[i][1];
				node.z=cur.z+arr[i][2];
				if((node.x >= 0) && (node.x < L) && (node.y >= 0) && (node.y < R) && (node.z >= 0) && (node.z < C) && (!used[node.x][node.y][node.z]) && (map[node.x][node.y][node.z] == '.' || map[node.x][node.y][node.z] == 'E'))
				{
					node.step=cur.step+1;
					used[node.x][node.y][node.z]=true;
					q.push(node);
				}
			}
		}
	}
}
int main(){
	while(~scanf("%d%d%d",&L,&R,&C)){
		if((L==0)&&(R==0)&&(C==0)){
			break;
		}
		memset(used,false,sizeof(used));
		for(int i=0;i<L;i++){
			getchar();
			for(int j=0;j<R;j++){
				for(int k=0;k<C;k++){
					scanf("%c",&map[i][j][k]);
					if (map[i][j][k] == 'S') {
                        start.x = i;
                        start.y = j;
                        start.z = k;
                        start.step = 0;
                    }
                    else if (map[i][j][k] == 'E')
                    {
                        end.x = i;
                        end.y = j;
                        end.z = k;
                    }	
				}
				getchar();
			}
		}
		bfs();
		if (cur.x == end.x && cur.y == end.y && cur.z == end.z)
            printf("Escaped in %d minute(s).\n", cur.step);
        else
            printf("Trapped!\n");
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 1313