Dungeon Master(POJ-2251)

Dungeon Master (POJ-2251)传送门

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!


分析

这道题用到了三维的数组(在原先的二维数组基础上多一个参数记录层数),地图是类似于一层一层往上走的那种
由于是最短的时间的问题,所以我们采取bfs( ) 进行搜索
bfs()从6个方向搜(因为是三维的,除了平面的走法还可以向上层走),每次记录时间miu[z][x][y],miu[z][x][y]是位于那个位置的时间,然后继续往外扩一圈(6个方向都走一次算一圈),以此类推,直到碰触到终点,时间即为最短
记得考虑一下边界问题

Code
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 1e2+22;
int level,row,col;
char map[maxn][maxn][maxn];
int	 vis[maxn][maxn][maxn];
int	 miu[maxn][maxn][maxn];
int dx[6] = {1,0,0,-1,0,0};
int dy[6] = {0,1,0,0,-1,0};
int dz[6] = {0,0,1,0,0,-1};
struct node
{
	int z,x,y;
	node(int z,int x,int y)
	{
		this->z = z;
		this->x = x;
		this->y = y;
		
	}
};
int bfs(node start , node end)
{
	//int flag = 0;
	//int cur = 0;
	queue<node>q;
	q.push(start);
//	vis[start.x][start.y][start.z] = 1;
	vis[start.z][start.x][start.y] = 1;
	while(!q.empty())
	{
		node p =q.front();
		q.pop();
		int x = p.x;
		int y = p.y;
		int z = p.z;
		for(int i = 0 ; i < 6 ; i++)
		{
			int next_z = z + dz[i];
			int next_x = x + dx[i];
			int next_y = y + dy[i];
			
			node tp = node(next_z,next_x,next_y);
			
			if( tp.z == end.z && tp.x == end.x && tp.y == end.y )
			{
				return miu[z][x][y]+1;
			}
			if(vis[tp.z][tp.x][tp.y] == 0 && map[tp.z][tp.x][tp.y] == '.' && tp.z >= 0 && tp.z < level && tp.x >= 0 && tp.x < row && tp.y >= 0 && tp.y < col)
			{
				miu[tp.z][tp.x][tp.y] = miu[z][x][y] + 1;
				//cur = miu[tp.x][tp.y][tp.z];
				//printf("miu[%d][%d][%d] = %d\n",tp.x,tp.y,tp.z,miu[tp.x][tp.y][tp.z]);
				vis[tp.z][tp.x][tp.y] = 1;
				q.push(tp);
			}
		}
	}
	return -maxn;
}
int main()
{
	while(scanf("%d %d %d",&level,&row,&col))
	{
		memset(vis,0,sizeof(vis));
		memset(map,0,sizeof(map));
		memset(miu,0,sizeof(miu));
		node start = node(0,0,0);
		node endin = node(0,0,0);
		//miu = 0;
		if(level == 0 && row == 0 && col == 0){	break;}
		for(int k = 0 ; k < level ; k++)
		{
			for(int i = 0 ; i < row ; i++)
			{
				for(int j = 0 ; j < col ; j++)
				{
					cin>>map[k][i][j];//第k层的第i行j列
				}
			}
		}
		for(int k = 0 ; k < level ; k++)
		{
			for(int i  = 0 ; i < row ; i++)
			{
				for(int j = 0 ; j < col ; j++)
				{
					if(map[k][i][j] == 'S'){	start = node(k,i,j);}
					if(map[k][i][j] == 'E'){	endin = node(k,i,j);}
				}
			}
		}
		int ans = bfs(start,endin);
		if(ans == -maxn)
		{
			cout<<"Trapped!"<<endl;
		}else
		{
			cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
		}
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值