我是靠谱客的博主 标致哑铃,最近开发中收集的这篇文章主要介绍UVa532 - Dungeon Master,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

题目地址:点击打开链接

BFS搜索六个方向即可

C++代码:

#include <iostream>
#include <string>
#include <deque>
#include <vector>
#include <cstring>
using namespace std;
const int maxsize = 40;
int visited[maxsize][maxsize][maxsize];
string s[maxsize][maxsize];
int num[maxsize][maxsize][maxsize];
int L,R,C;
int d[6][3]={
	0,0,-1,
	0,0,1,
	0,1,0,
	0,-1,0,
	1,0,0,
	-1,0,0
			};
struct Point
{
	int x,y,z;
	Point(int x,int y,int z)
	{
		this->x=x;
		this->y=y;
		this->z=z;
	}
	Point(const Point &p)
	{
		this->x=p.x;
		this->y=p.y;
		this->z=p.z;
	}
};
bool BFS(int start_L,int start_R,int start_C,int end_L,int end_R,int end_C)
{
	deque<Point> dvi;
	Point p(start_L,start_R,start_C);
	dvi.push_back(p);
	visited[start_L][start_R][start_C]=1;
	while(!dvi.empty()&&(dvi.front().x!=end_L||dvi.front().y!=end_R||dvi.front().z!=end_C))
	{
		p=dvi.front();
		dvi.pop_front();
		for(int i=0;i<6;++i)
		{
			int x=p.x+d[i][0];
			int y=p.y+d[i][1];
			int z=p.z+d[i][2];
			if(x>=0&&x<L&&y>=0&&y<R&&z>=0&&z<C&&!visited[x][y][z]&&s[x][y][z]!='#')
			{
				num[x][y][z]=num[p.x][p.y][p.z]+1;
				visited[x][y][z]=1;
				Point q(x,y,z);
				dvi.push_back(q);
			}
		}
	}
	if(!dvi.empty())
		return true;
	else
		return false;
}
int main()
{
	while(cin>>L>>R>>C&&(L||R||C))
	{
		int i,j,k;
		int start_L,start_R,start_C;
		int end_L,end_R,end_C;
		for(i=0;i<L;++i)
		{
			for(j=0;j<R;++j)
			{
				cin>>s[i][j];
				for(k=0;k<s[i][j].size();++k)
				{
					if(s[i][j][k]=='S')
					{
						start_L=i;
						start_R=j;
						start_C=k;
						break;
					}
					else
					{
						if(s[i][j][k]=='E')
						{
							end_L=i;
							end_R=j;
							end_C=k;
							break;
						}
					}
				}
			}
		}
		memset(visited,0,sizeof(visited));
		memset(num,0,sizeof(num));
		if(BFS(start_L,start_R,start_C,end_L,end_R,end_C))
			cout<<"Escaped in "<<num[end_L][end_R][end_C]<<" minute(s)."<<endl;
		else
			cout<<"Trapped!"<<endl;
	}
	return 0;
}


最后

以上就是标致哑铃为你收集整理的UVa532 - Dungeon Master的全部内容,希望文章能够帮你解决UVa532 - Dungeon Master所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(71)

评论列表共有 0 条评论

立即
投稿
返回
顶部