[kuangbin带你飞]专题1 简单搜索 B

来源:互联网 发布:心动网络 校招 编辑:程序博客网 时间:2024/05/16 17:16

题目:

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 39370 Accepted: 14971

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


大致题意:有一个三维的迷宫,L代表迷宫的层数,R和C分别代表同一层的行和列。人从‘S’的位置开始走,走到‘E’的位置为成功逃离。每次移动耗时一秒,方向为上下左右前后。‘.’是可走的地方,‘#’是墙,不能走。问是否可以走出去,走出去最快耗时几秒。


思路:BFS板子题

三维的就多加上下两个方向就行了。


#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<iostream>#include<algorithm>#include<queue>#include<map>#include<vector>#include<stack>typedef long long ll;using namespace std;int L,R,C;char mapp[50][50][50];bool vis[50][50][50]; //vis记录某个位置是否已经被搜到过 int sc,si,sj;int ec,ei,ej;int dir[6][3] = {{1,0,0} , {-1,0,0} , {0,1,0} , {0,-1,0} , {0,0,1} , {0,0,-1}};//dir数组记录6个方向 struct node{int c;int x;int y;int step;};//结构体记录一个点的坐标和从起点走到这个点的步数。  node now,nxt;int bfs(){queue<node> Q;now.c = sc , now.x = si , now.y = sj;now.step = 0;vis[sc][si][sj] = 0;Q.push(now);while(!Q.empty()){now = Q.front() , Q.pop();if(now.c==ec && now.x==ei && now.y==ej){return now.step;                  //退出条件:走到出口 }for(int i = 0 ; i<6 ; i++){nxt.c = now.c + dir[i][0];nxt.x = now.x + dir[i][1];nxt.y = now.y + dir[i][2];if(nxt.c>=0 && nxt.c<L && nxt.x>=0 && nxt.x<R && nxt.y>=0 &&nxt.y<C && mapp[nxt.c][nxt.x][nxt.y]!='#' && vis[nxt.c][nxt.x][nxt.y]==0){//若按此方向走到的下个点没有越界,且未被访问过,且这个点不是墙,则可以走。 nxt.step = now.step+1;Q.push(nxt);vis[nxt.c][nxt.x][nxt.y] = 1;}//以上代码表示若这个方向可以走,则加入队列。走的步数要比当前步数+1。 }}return -1;   //若从此返回,代表找不到出口 }int main(){ios::sync_with_stdio(false);while (~scanf("%d %d %d",&L,&R,&C)){if(L==0 && R==0 && C==0)break;for(int i = 0 ; i<L ; i++){for(int j = 0 ; j<R ; j++){scanf("%s",mapp[i][j]);for(int k = 0 ; k<C ; k++){if(mapp[i][j][k] == 'S'){sc = i;si = j;sj = k;}else if(mapp[i][j][k] == 'E'){ec = i;ei = j;ej = k;}}}}memset(vis,0,sizeof(vis));int ans = bfs();if(ans>=0){printf("Escaped in %d minute(s).\n",ans);}else{printf("Trapped!\n");}}return 0;}

阅读全文
0 0
原创粉丝点击