[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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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
- [kuangbin带你飞]专题1 简单搜索 B
- [kuangbin带你飞]专题一 简单搜索 B POJ2251
- [kuangbin带你飞]专题一 简单搜索 B
- [kuangbin带你飞]专题一 简单搜索-B
- 2251&[kuangbin带你飞]专题一 简单搜索 B
- [kuangbin带你飞]专题一 简单搜索 B
- [kuangbin带你飞]专题1 简单搜索 A
- [kuangbin带你飞]专题1 简单搜索 C
- [kuangbin带你飞]专题1 简单搜索 D
- [kuangbin带你飞]专题1 简单搜索 E
- [kuangbin带你飞]专题1 简单搜索 F
- [kuangbin带你飞]专题1 简单搜索 G
- [kuangbin带你飞]专题1 简单搜索 H
- [kuangbin带你飞]专题1 简单搜索 I
- kuangbin带你飞专题一(搜索)B
- [kuangbin带你飞]专题一 简单搜索 B - Dungeon Master(POJ 2251)
- [kuangbin带你飞]专题一 简单搜索 B - Dungeon Master poj2251
- [kuangbin带你飞]专题一 简单搜索->B - Dungeon Master
- the class required for this tool,‘rviz_visual_tools/KeyTool’,could not be loaded
- 封装顺序栈(模拟实现stack)
- 封装顺序栈(模拟实现stack)
- 文章标题
- IT道德与伦理
- [kuangbin带你飞]专题1 简单搜索 B
- 使用函数调用任意输入两个数 并输出最大(小)值
- 初探MyBatis
- MATLAB神经网络训练结果各参数解释
- 大数学家陶哲轩谈时间管理与高效工作的方法
- 加载外部的properties配置文件
- selenium+python截图不成功
- 岭回归——额外参数alpha和预测系数coefs的关系
- 电路基础学习笔记5:实验验证电压源与电流源的等效变换