Dungeon Master
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 21116 | Accepted: 8210 |
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 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s).Trapped!
这题让我改了好久。。。。原因很简单因为,没有分清r,c分别表示的什么但是题目都说 了,所以这两天脑子进水了。。。。- -!
也没啥好说的,就是让求3维空间S到E的最短距离。很显然BFS。
主要是构建人的行走方向,按照一定的顺序来行走。
需要强调一点,scanf("%s",str)在遇到'\n'(回车)或' '(空格)时输入结束,但'\n'(回车)或' '(空格)停留在出入缓冲区,如处理不慎会影响下面的输入;gets(str)遇到'\n'(回车)时输入结束,但'\n'(回车)已被替换为'\0',存储于字符串中,输入缓冲中没有遗留的'\n'(回车),不会影响后续的输入。若用gets输入的话前后两边的getchar()必不可少,前边的在输入整数的getchar(),肯定不能少。至于在后边的getchar()为什么要有,因为gets()#include <iostream> #include <cstdio> #include <cstring> #include <map> #include<algorithm> #include<math.h> #include<queue> using namespace std; struct node { int a,b,ceng,ans; }; int l,r,c; char Map[40][40][40]; bool vis[40][40][40],bj; int mo[6][3]= {{-1,0,0},{1,0,0},{0,0,1},{0,-1,0},{0,0,-1},{0,1,0}}; void bfs(int ce,int s1,int s2) { queue<node>q; while(!q.empty()) q.pop(); memset(vis,false,sizeof(vis)); node f1,f2; f1.ceng=ce;f1.a=s1;f1.b=s2; f1.ans=0; vis[ce][s1][s2]=true; q.push(f1); while(!q.empty()) { f1=q.front(); q.pop(); if(Map[f1.ceng][f1.a][f1.b]=='E') { bj=true; printf("Escaped in %d minute(s).\n",f1.ans); return ; } for(int i=0;i<6;i++) { f2=f1; f2.ceng+=mo[i][0]; f2.a+=mo[i][1]; f2.b+=mo[i][2]; if(f2.ceng>=0&&f2.ceng<l&&f2.a>=0&&f2.a<r&&f2.b>=0&&f2.b<c&&Map[f2.ceng][f2.a][f2.b]!='#'&&!vis[f2.ceng][f2.a][f2.b]) { f2.ans++; vis[f2.ceng][f2.a][f2.b]=true; q.push(f2); } } } } int main() { int i,j,k; int ce,s1,s2; while(~scanf("%d%d%d",&l,&r,&c)) { bj=false; if(l==0&&r==0&&c==0) break; for(i=0;i<l;i++)//并且一定要注意输入的问题 { for(j=0;j<r;j++) { scanf("%s",Map[i][j]); } } for(i=0;i<l;i++) { for(j=0;j<r;j++) { for(k=0;k<c;k++) { if(Map[i][j][k]=='S') { ce=i; s1=j; s2=k; } } } } bfs(ce,s1,s2); if(!bj) puts("Trapped!"); } return 0; }
能够将所有的字符读取进来如上所述将'\n'代替为‘\0’储存在串中因为串的大小固定,所以必有字符串没有读入,串中。而%s没这么随便什么都读取。
getchar();
for(i=0;i<l;i++)
{
for(j=0;j<r;j++)
{
gets(Map[i][j]);
}
getchar();
}