poj 2251 逃出牢笼,简单的bfs

来源:互联网 发布:5号电池多少毫安 知乎 编辑:程序博客网 时间:2024/04/29 20:26
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18031 Accepted: 6994

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!

Source

Ulm Local 1997



题意:很常规的三维空间的宽搜,就不多讲了

代码:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int ms = 1e6;struct node{    int l,r,c;    int steps;};node queue[ms];int front,rear,visit[40][40][40],map[40][40][40];int L,R,C;int move[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};void Init(){    front = rear = 0;    return;}bool IsEmpty(){    if(front == rear)        return true;    return false;}void Push(node m){    if(visit[m.l][m.r][m.c])        return;    visit[m.l][m.r][m.c] = 1;    if(rear == ms)        rear = 0;    queue[rear++] = m;    return;}void Pop(){    if(front == rear)        return;    front++;    return;}bool over(int l,int r,int c){    if(l<0 || l>= L || r<0 || r>=R || c<0 || c>=C)        return true;    return false;}int main(){    char tmp;    node start,top,newn;    int el,er,ec;    int ans;    while(1){        scanf("%d %d %d",&L,&R,&C);        if(!L)            return 0;        Init();        memset(visit,0,sizeof(visit));        ans = -1;        for(int i = 0; i < L; i++){            for(int j = 0; j < R;j++){                for(int k = 0; k < C;k++){                    scanf(" %c",&tmp);                    if(tmp == 'S'){                        start.l = i;                        start.r = j;                        start.c = k;                        start.steps = 0;                        Push(start);                    }                                            if(tmp == 'E'){                        el = i,er = j,ec = k;                    }                                        if(tmp == '#')                        map[i][j][k] = 1;                    else                        map[i][j][k] = 0;                }            }        }        while(!IsEmpty()){            top = queue[front];            if(top.l == el && top.r == er && top.c == ec){                ans = top.steps;                printf("Escaped in %d minute(s).\n",ans);                break;            }            Pop();            newn.steps = top.steps+1;                        for(int i = 0; i < 6; i++){                newn.l = top.l+move[i][0];                newn.r = top.r+move[i][1];                newn.c = top.c+move[i][2];                if(!over(newn.l,newn.r,newn.c))                    if(!map[newn.l][newn.r][newn.c])                        Push(newn);            }        }        if(ans == -1)            printf("Trapped!\n");    }}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 购买房子的发票丢了怎么办 购买的二手房发票丢失怎么办 购买空调时的发票丢失怎么办 发票购票本丢了怎么办 留学中介费贵了怎么办 车祸伤者不肯出院怎么办 增值税普通发票发票联丢失怎么办 纳税识别号错了怎么办 电子发票税号填写错误怎么办 发票抬头名称写错了怎么办 滴滴发票抬头写错了怎么办 普票抬头开错了怎么办 发票号码打错了怎么办 个人税票开错了怎么办 普票地址错了怎么办 专票税率开高了怎么办 专票税率开错了怎么办 发票姓名写错了怎么办 首付发票姓名写错怎么办 一般纳税人税率开错了怎么办 电子税票开错了怎么办 税率是6%开成3%怎么办 医院收费收错了怎么办 发票少打一个字怎么办 增值税发票抬头错了一个字怎么办 暂估入账跨年了怎么办 电子发票写错了怎么办 发票纳税人识别号写错了怎么办 电子发票忘填邮箱怎么办 公司名跟发票抬头不一样怎么办 买空调不给发票怎么办 卖苹果手机没有发票怎么办 刷机字库刷坏了怎么办 维棠不能播放视频怎么办 m3u8卡顿的厉害怎么办 下载了PDF不能用怎么办 pdf格式在电脑打不开怎么办 电脑上arm格式打不开怎么办 极米桌面无响应怎么办 jpg格式的文件打印模糊怎么办 苹果迅雷mp4格式看不了怎么办