Dungeon Master(POJ-2251)

CoderSilence發表於2020-09-27

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;
}

相關文章