ZOJ1940 POJ2251 Dungeon Master 求三维迷宫路径问题,广度优先搜索

105 篇文章 0 订阅

这题其实就是求二维迷宫的路径问题的三维版,我用了最简单的BFS方法。在这里的结构体point要重载==,!=运算符。


/*******************************************************************************
 * Author : Neo Fung
 * Email : neosfung@gmail.com
 * Last modified : 2011-07-22 16:10
 * Filename : ZOJ1940 POJ2251 Dungeon Master.cpp
 * Description : 
 * *****************************************************************************/
// ZOJ1940 POJ2251 Dungeon Master.cpp : Defines the entry point for the console application.
//

// #include "stdafx.h"



#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <map>
#include <math.h>
#include <algorithm>
#include <numeric>
#include <functional>
#include <memory.h>

using namespace std;

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

bool operator==(const point& lhs,const point& rhs){
	return
		((lhs.x==rhs.x)&&(lhs.y==rhs.y)&&(lhs.z==rhs.z));
}

bool operator!=(const point& lhs,const point& rhs){
	return
		!((lhs.x==rhs.x)&&(lhs.y==rhs.y)&&(lhs.z==rhs.z));
}

int main(void)
{
// 	ifstream cin("data.txt");

	int L,R,C;
	int matrix[30][30][30];
	point START,END,now,temp;
	char ch;
	int pathway;
	queue<point> bfs;
	queue<int> path;

	while(cin>>L>>R>>C && (L||R||C))
	{
		for(int i=0;i<L;i++)
			for(int j=0;j<R;j++)
				for(int k=0;k<C;++k)
				{
					cin>>ch;
					matrix[i][j][k]=ch;
					if(ch == 'S')
					{
						START.x=i;
						START.y=j;
						START.z=k;
					}
					if(ch == 'E')
					{
						END.x=i;
						END.y=j;
						END.z=k;
					}
				}

		while(!bfs.empty()) bfs.pop();
		while(!path.empty()) path.pop();

		bfs.push(START);
		path.push(0);
		matrix[START.x][START.y][START.z]='#';

// 		now=START;

		while(!bfs.empty() && bfs.front()!=END )
		{
			now=bfs.front();
			pathway=path.front();
			++pathway;
			bfs.pop();
			path.pop();

			if(now.x+1<L)
			{
				temp=now;
				temp.x++;
				if(matrix[temp.x][temp.y][temp.z] != '#')
				{
					bfs.push(temp);
					path.push(pathway);
					matrix[temp.x][temp.y][temp.z] = '#';
				}
			}

			if(now.x-1>=0)
			{
				temp=now;
				temp.x--;				
				if(matrix[temp.x][temp.y][temp.z] != '#')
				{
					bfs.push(temp);
					path.push(pathway);
					matrix[temp.x][temp.y][temp.z] = '#';
				}
			}

			if(now.y+1<R)
			{
				temp=now;
				temp.y++;
				if(matrix[temp.x][temp.y][temp.z] != '#')
				{
					bfs.push(temp);
					path.push(pathway);
					matrix[temp.x][temp.y][temp.z] = '#';
				}
			}

			if(now.y-1>=0)
			{
				temp=now;
				temp.y--;
				if(matrix[temp.x][temp.y][temp.z] != '#')
				{
					bfs.push(temp);
					path.push(pathway);
					matrix[temp.x][temp.y][temp.z] = '#';
				}
			}

			if(now.z+1<C)
			{
				temp=now;
				temp.z++;
				if(matrix[temp.x][temp.y][temp.z] != '#')
				{
					bfs.push(temp);
					path.push(pathway);
					matrix[temp.x][temp.y][temp.z] = '#';
				}
			}

			if(now.z-1>=0)
			{
				temp=now;
				temp.z--;				
				if(matrix[temp.x][temp.y][temp.z] != '#')
				{
					bfs.push(temp);
					path.push(pathway);
					matrix[temp.x][temp.y][temp.z] = '#';
				}
			}


		}

		if (!bfs.empty())
		{
			cout<<"Escaped in "<<path.front()<<" minute(s)."<<endl;
		} 
		else
		{
			cout<<"Trapped!"<<endl;
		}

	} 

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值