poj 2251 bfs基础

来源:互联网 发布:小学生沉迷网络的案例 编辑:程序博客网 时间:2024/06/10 03:35

Description

You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides. 

Is an escape possible? If yes, how long will it take? 

Input

The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size). 
L is the number of levels making up the dungeon. 
R and C are the number of rows and columns making up the plan of each level. 
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

Output

Each maze generates one line of output. If it is possible to reach the exit, print a line of the form 
Escaped in x minute(s).

where x is replaced by the shortest time it takes to escape. 
If it is not possible to escape, print the line 
Trapped!

Sample Input

3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!
题意:给你一个图,不过就是三维的,然后让你从s走到E,求最短距离
思路就不说了直接bfs,不过就是三维而已
ac代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;int x,y,z,vis[50][50][50],c;char a[50][50][50];int cnx[6]= {1,0,-1,0,0,0};int cny[6]= {0,-1,0,1,0,0};int cnz[6]= {0,0,0,0,1,-1};int x1,y1,z1;int x2,y2,z2;struct node{    int xx,yy,zz;    int k;};int bfs(int m,int n,int q){    queue<node> que;    node head,next;    head.xx=m;    head.yy=n;    head.zz=q;    head.k=0;    que.push(head);    vis[m][n][q]=1;    while(!que.empty())    {        head=que.front();        que.pop();        if(a[head.xx][head.yy][head.zz]=='E')        {            return head.k;        }        for(int i=0;i<6;i++)        {            int m1=head.xx+cnx[i];            int n1=head.yy+cny[i];            int q1=head.zz+cnz[i];            if(m1<0||m1>=x||n1<0||n1>=y||q1<0||q1>=z||vis[m1][n1][q1]==1||a[m1][n1][q1]=='#')            continue;            next.xx=m1;            next.yy=n1;            next.zz=q1;            next.k=head.k+1;            que.push(next);            vis[m1][n1][q1]=1;        }    }    return 0;}int main(){    while(cin>>x>>y>>z)    {        if(x==0&&y==0&&z==0)        break;        for(int i=0; i<x; i++)            for(int j=0; j<y; j++)                for(int k=0; k<z; k++)                {                    cin>>a[i][j][k];                    if(a[i][j][k]=='S')                    {                        x1=i,y1=j,z1=k;                    }                    if(a[i][j][k]=='E')                    {                        x2=i,y2=j,z2=k;                    }                }     memset(vis,0,sizeof(vis));     int h;     h=bfs(x1,y1,z1);     if(h==0)     cout<<"Trapped!"<<endl;     else     printf("Escaped in %d minute(s).\n",h);    }}


0 0