B - 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…
.###.
.##…
###.#

##.##
##…

#.###
####E

1 3 3
S##
#E#

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

这道题还是迷宫问题,但是把二维的迷宫变成了三维,要设好多不同的变量名,有些乱。
总的来说就是注意细节吧。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;

int l,r,c;
int z,x,y;
int dz[]={-1,1,0,0,0,0};
int dx[]={0,0,-1,1,0,0};
int dy[]={0,0,0,0,-1,1};
char m[35][35][35];
int d[35][35][35];
struct zxy
{
	int z,x,y;
};
typedef struct zxy s;

int bfs()
{
	queue<s> q;
	s a,b,e;
	for(int i=0;i<l;i++)
		for(int j=0;j<r;j++)
			for(int h=0;h<c;h++)
			{
				if(m[i][j][h]=='S')//起点 
				{
					d[i][j][h]=0;
					a.z=i;a.x=j;a.y=h;
					q.push(a);
				}
				if(m[i][j][h]=='E')//终点 
				{
					e.z=i;e.x=j;e.y=h;
				}
			}
	while(q.size())
	{
		a=q.front();q.pop();
		if(m[a.z][a.x][a.y]=='E') break;
		for(int i=0;i<6;i++)
		{
			int nz=a.z+dz[i];
			int nx=a.x+dx[i];
			int ny=a.y+dy[i];
			if(nz>=0&&nz<l&&nx>=0&&nx<r&&ny>=0&&ny<c&&d[nz][nx][ny]==inf&&m[nz][nx][ny]!='#')
			{
				d[nz][nx][ny]=d[a.z][a.x][a.y]+1;
				b.z=nz;b.x=nx;b.y=ny;
				q.push(b);
			}
		}
	}
	return d[e.z][e.x][e.y];//返回到终点的距离 
}
int main()
{
	while(scanf("%d%d%d",&l,&r,&c)!=EOF)
	{
		if(l==0&&r==0&&c==0) break;
		for(int i=0;i<l;i++)
			for(int j=0;j<r;j++)
				scanf("%s",m[i][j]);
		memset(d,inf,sizeof d);
		int ans=bfs();
		if(ans==inf)
			printf("Trapped!\n");
		else
			printf("Escaped in %d minute(s).\n",ans);
	}
	return 0;
}

晕死,第二次做因为多组输入的话,最后的队列需要清空,在自定义函数bfs中定义队列即可。但是我定义在了全局。

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;

int l,r,c,d[35][35][35];
char ma[35][35][35];
int dz[]={1,-1,0,0,0,0};
int dx[]={0,0,1,-1,0,0};
int dy[]={0,0,0,0,1,-1};
int gz,gx,gy;
typedef struct
{
	int zz,xx,yy;
}pos;
pos p,s,n;

int bfs()
{
	queue<pos> q;
	for(int i=0;i<l;i++)
		for(int j=0;j<r;j++)
			for(int h=0;h<c;h++)
			{
				if(ma[i][j][h]=='S')
				{
					s.zz=i;
					s.xx=j;
					s.yy=h;
					d[i][j][h]=0;
					q.push(s);
				}
				if(ma[i][j][h]=='E')
				{
					gz=i;
					gx=j;
					gy=h;
				}	
			}
	while(q.size())
	{
		p=q.front();q.pop();
		if(ma[p.zz][p.xx][p.yy]=='E')
			break;
		for(int i=0;i<6;i++)
		{
			int nz=p.zz+dz[i];
			int nx=p.xx+dx[i];
			int ny=p.yy+dy[i];
			if(nz>=0&&nz<l&&nx>=0&&nx<r&&ny>=0&&ny<c&&d[nz][nx][ny]==inf&&ma[nz][nx][ny]!='#')//注意这个地方,只要不是#就可以走 
			{
				n.zz=nz;n.xx=nx;n.yy=ny;
				q.push(n);
				d[nz][nx][ny]=d[p.zz][p.xx][p.yy]+1;
			}
		}
	}
	return d[gz][gx][gy];
}

int main(void)
{
	int ans;
	while(scanf("%d%d%d",&l,&r,&c)!=EOF&&l!=0&&r!=0&&c!=0)
	{
		for(int i=0;i<l;i++)
			for(int j=0;j<r;j++)
				scanf("%s",ma[i][j]);
		memset(d,inf,sizeof(d));
		ans=bfs();
		if(ans==inf)
			printf("Trapped!\n");
		else
			printf("Escaped in %d minute(s).\n",ans);
	}
	return 0;
}

第三次的代码,感觉还是要注意细节

注意声明结构体的方式:

P p = {x, y, z}; 
que.push(p);

POJ如果像下面这样会报错

que.push(P(x, y, z));
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 35, INF = 0x3f3f3f3f;

struct P{int z, x, y;};

int l, r, c;
int sz, sx, sy, gz, gx, gy;
char ma[N][N][N];
int d[N][N][N];
int dz[] = {0, 0, 0, 0, 1, -1}, dx[] = {0, 0, 1, -1, 0, 0}, dy[] = {1, -1, 0, 0, 0, 0};

int bfs()
{
	memset(d, 0x3f, sizeof d);
	queue<P> que;
	P sp = {sz, sx, sy};
	que.push(sp);
	d[sz][sx][sy] = 0;
	
	while (que.size()) {
		P p = que.front(); que.pop();
		if (p.z == gz && p.x == gx && p.y == gy) {
			break;
		}
		for (int i = 0; i < 6; i++) {
			int nz = p.z + dz[i], nx = p.x + dx[i], ny = p.y + dy[i];
			if (nz >= 0 && nz < l && nx >= 0 && nx < r && ny >= 0 && ny < c && ma[nz][nx][ny] != '#' && d[nz][nx][ny] == INF) {
				P np = {nz, nx, ny};
				que.push(np);
				d[nz][nx][ny] = d[p.z][p.x][p.y] + 1;
			}
		}
	}
	return d[gz][gx][gy];
}

int main(void)
{
	while (cin >> l >> r >> c && l) {
		for (int i = 0; i < l; i++) {
			for (int j = 0; j < r; j++) {
				for (int k = 0; k < c; k++) {
					cin >> ma[i][j][k];
					if (ma[i][j][k] == 'S') {
						sz = i, sx = j, sy = k;
					} else if (ma[i][j][k] == 'E') {
						gz = i, gx = j, gy = k;
					} 
				}
			}
		}
		
		int step = bfs();
		if (step != INF)
			printf("Escaped in %d minute(s).\n", step);
		else
			printf("Trapped!\n");
	}
	
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值