Dungeon Master 和上题基本相同 bfs(),求最优值 不同的是三维bfs(),存储有技巧 (u=x*r*c+y*c+z);
2013-10-14 21:42
393 查看
[align=left]Problem Description[/align]
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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
[align=left]Sample Input[/align]
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E
**************************************************************************************************************************
bfs() 存储有技巧
***************************************************************************************************************************
View Code
坚持!!!!!!!!!!!
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?
[align=left]Input[/align]
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.
[align=left]Output[/align]
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!
[align=left]Sample Input[/align]
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E
**************************************************************************************************************************
bfs() 存储有技巧
***************************************************************************************************************************
#include<iostream> #include<string> #include<cstring> #include<cmath> #include<cstdio> #include<queue> using namespace std; int dx[6]={1,-1,0,0,0,0}; int dy[6]={0,0,0,0,1,-1}; int dz[6]={0,0,1,-1,0,0}; int maze[101][101][101]; int vis[101][101][101]; int dis[101][101][101]; int l,r,c,i,j,k; int que[100001]; int stx,sty,stz; int bfs(int xt,int yt,int zt) { int rear,front; rear=front=0; int u=xt*r*c+yt*c+zt;//经典,使数存的更简洁 que[rear++]=u; vis[xt][yt][zt]=1; while(front<rear) { int su=que[front]; front++; int x=su/(r*c); int y=(su%(r*c))/c; int z=su%c; for(int it=0;it<6;it++) { int xa=x+dx[it]; int ya=y+dy[it]; int za=z+dz[it]; if(xa>=0&&xa<l&&ya>=0&&ya<r&&za>=0&&za<c&&maze[xa][ya][za]!=0&&!vis[xa][ya][za]) { u=xa*r*c+ya*c+za; que[rear++]=u; dis[xa][ya][za]=dis[x][y][z]+1; vis[xa][ya][za]=1; if(maze[xa][ya][za]==2) return dis[xa][ya][za]; } } } return 0; } int main() { char ch; while(scanf("%d%d%d",&l,&r,&c)&&l&&r&&c) { getchar(); memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); memset(maze,0,sizeof(maze)); for(i=0;i<l;i++) { for(j=0;j<r;j++) { for(k=0;k<c;k++) { scanf("%c",&ch); if(ch=='#')maze[i][j][k]=0; if(ch=='.')maze[i][j][k]=1; if(ch=='E')maze[i][j][k]=2; if(ch=='S') { stx=i; sty=j; stz=k; } } getchar(); } getchar(); } int ans=bfs(stx,sty,stz); if(ans==0) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n",ans); } return 0; }
View Code
坚持!!!!!!!!!!!
相关文章推荐
- 两种写法不同作用相同的存储过程
- POJ 2251 Dungeon Master (三维BFS)
- POJ 2251 Dungeon Master (三维BFS)
- poj 2251 Dungeon Master 三维bfs
- POJ 2251 Dungeon Master(三维BFS)
- zju 1940 Dungeon Master(三维BFS暴搜题)
- pku 2251 Dungeon Master 基本BFS
- uva 532 Dungeon Master(三维bfs)
- 为表名不同但属性相同的多个表添加字段的mysql存储过程示例
- POJ-2251-Dungeon Master 三维BFS
- poj 2251 Dungeon Master ----BFS (三维六个方向)
- 暑期第一弹<搜索> B - Dungeon Master(三维BFS,6个状态)
- poj2251--Dungeon Master(搜索练习3-三维bfs)
- ZOJ 1940 Dungeon Master 三维BFS
- UVA Dungeon Master (三维BFS)
- 【POJ2251】Dungeon Master 三维 BFS Hash(12/1000)
- HashMap存入相同key的两种不同的存储方式
- POJ - 2251 Dungeon Master(三维BFS)
- POJ 2251 Dungeon Master(三维6方向BFS)
- POJ-2251 Dungeon Master(简单的三维bfs)