本文主要是介绍UVA 532 POJ 2251 Dungeon Master (BFS),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
http://poj.org/problem?id=2251
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=473
Dungeon Master
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 5 S.... .###. .##.. ###.###### ##### ##.## ##...##### ##### #.### ####E1 3 3 S## #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<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define mmusing namespace std;int vis[33][33][33];
int fx,fy,fz,tx,ty,tz;
int L,R,C;
int f,r;int dx[]={0,0,0,0,1,-1};
int dy[]={0,0,1,-1,0,0};
int dz[]={1,-1,0,0,0,0};struct record
{int x,y,z,t;record(int xx=0,int yy=0,int zz=0,int tt=0){x=xx;y=yy;z=zz;t=tt;}record(const record &r){x=r.x;y=r.y;z=r.z;t=r.t;}
}q[33333];int BFS()
{while (f<=r){record n(q[f++]);//printf("%d %d %d %d\n",n.x,n.y,n.z,n.t);if (n.x==tx && n.y==ty && n.z==tz) return n.t;int x,y,z;for (int i=0;i<6;i++){x=n.x+dx[i];y=n.y+dy[i];z=n.z+dz[i];if (vis[x][y][z]!=-1){vis[x][y][z]=-1;q[++r]=record(x,y,z,n.t+1);}}}return -1;
}int main()
{#ifndef ONLINE_JUDGEfreopen("/home/fcbruce/文档/code/t","r",stdin);#endif // ONLINE_JUDGEwhile (scanf("%d%d%d",&L,&R,&C),L||R||C){char c;memset(vis,-1,sizeof(vis));for (int i=1;i<=L;i++){getchar();for (int j=1;j<=R;j++){for (int k=1;k<=C;k++){c=getchar();if (c=='#') continue;vis[i][j][k]=0;if (c=='S'){fx=i;fy=j;fz=k;}if (c=='E'){tx=i;ty=j;tz=k;}}getchar();}}//printf("%d %d %d %d\n",fx,fy,fz,0);q[f=r=0]=record(fx,fy,fz,0);vis[fx][fy][fz]=-1;int k=BFS();if (~k){printf("Escaped in %d minute(s).\n",k);}else{printf("Trapped!\n");}}return 0;
}
这篇关于UVA 532 POJ 2251 Dungeon Master (BFS)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!