百练 / 2016计算机学科夏令营上机考试: F (广度优先搜索)

题目来源:http://bailian.openjudge.cn/practice/2251/

类似还有一题也是宽搜:Prime Path

2251:Dungeon Master

总时间限制: 1000ms      内存限制: 65536kB

描述

You are trapped in a 3D dungeon and need to find thequickest way out! The dungeon is composed of unit cubes which may or may not befilled 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 bysolid rock on all sides. 

Is an escape possible? If yes, how long will it take? 

输入

The input consists of a number of dungeons. Each dungeondescription starts with a line containing three integers L, R and C (alllimited 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 eachlevel. 
Then there will follow L blocks of R lines each containing C characters. Eachcharacter describes one cell of the dungeon. A cell full of rock is indicatedby a '#' and empty cells are represented by a '.'. Your starting position isindicated by 'S' and the exit by the letter 'E'. There's a single blank lineafter each level. Input is terminated by three zeroes for L, R and C.

输出

Each maze generates one line of output. If it is possibleto 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!

样例输入

3 4 5

S....

.###.

.##..

###.#

 

#####

#####

##.##

##...

 

#####

#####

#.###

####E

 

1 3 3

S##

#E#

###

 

0 0 0

样例输出

Escaped in 11 minute(s).

Trapped!

-----------------------------------------------------

解题思路

广度优先搜索

广度优先搜索的标准做法涉及3个数据结构:

1. 双端队列path,承载宽搜的算法;

2. 多维数组或HashMap father,与输入的矩阵同尺寸, 记录path上每个节点的父节点,用于回溯给出搜索到的最短路

3. 多维数组或HashMap step,与输入的矩阵同尺寸,记录从起始点到每一个节点经过的最短路长度

如果需要回溯求解最短道路经过哪些节点,则回溯过程可以计算最短路的长度,无需第3个数据结构step.

此题由于不需要回溯最短路,所以不需要第2个数据结构 father.

本题宽搜核心代码

while(!path.empty())
{
	current = path.front();			// 取队首节点
	path.pop_front();				// 队首节点出队
	for (i=0; i<6; i++)				// 尝试6种移动方向
	{
		next = walk(current,i);
		if (judge(next,l,r,c,maze) && step[next.x][next.y][next.z] == -1)// 相邻节点是有效节点且没走过
		{
			path.push_back(next);										// 将相邻节点入队
			step[next.x][next.y][next.z] = step[current.x][current.y][current.z] + 1;	// 路长+1
		}
		if (next == end)
		{
			found = true;
			break;
		}
	}
	if (found == true)
	{
		break;
	}
}

-----------------------------------------------------

代码

#include<fstream>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;

class node {
public:
	int x,y,z;
	node(void)
	{
		x = -1;
		y = -1;
		z = -1;
	}
	node(int xx, int yy, int zz)
	{
		x = xx;
		y = yy;
		z = zz;
	}
	void operator= (const node & b)
	{
		x = b.x;
		y = b.y;
		z = b.z;
	}
	bool operator== (const node &b)
	{
		return ((x==b.x) && (y==b.y) && (z==b.z));
	}

};

node walk(node cur, int i)			// 0~5分别代表6个方向
{
	int x = cur.x;
	int y = cur.y;
	int z = cur.z;
	switch(i)
	{
	case 0:
		return node(x+1,y,z);
	case 1:
		return node(x-1,y,z);
	case 2:
		return node(x,y+1,z);
	case 3:
		return node(x,y-1,z);
	case 4:
		return node(x,y,z+1);
	case 5:
		return node(x,y,z-1);
	default:
		return node(-1,-1,-1);
	}
}

bool judge(node cur, int l, int r, int c, char *** maze)
{
	if (cur.x >= l || cur.x <0 || cur.y >= r || cur.y <0 || cur.z >= c || cur.z <0)
	{
		return false;
	}
	else if (maze[cur.x][cur.y][cur.z] == '#')
	{
		return false;
	}
	else
	{
		return true;
	}
}


int main()
{
#ifndef ONLINE_JUDGE
	ifstream fin("xly2016F.txt");
	if (!fin)
	{
		exit(1);
	}
	int l,r,c,i,j,k;
	deque<node> path;						// 宽搜双端队列
	node start, end, current, next;
	bool found = false;
	fin >> l >> r >> c;
	while (l && r && c)
	{
		path.clear();
		found = false;
		char*** maze = new char**[l];		// 地图信息
		int*** step = new int**[l];		// 记录走到每个点的步数
		for (i=0; i<l; i++)
		{
			maze[i] = new char*[r];
			step[i] = new int*[r];
			for (j=0; j<r; j++)
			{
				maze[i][j] = new char[c];
				step[i][j] = new int[c];
			}
		}
		for (i=0; i<l; i++)
		{
			for (j=0; j<r; j++)
			{
				for (k=0; k<c; k++)
				{
					fin >> maze[i][j][k];
					step[i][j][k] = -1;		// 步数初始化为-1表示没有该点走过
					if (maze[i][j][k] == 'S')
					{
						start.x = i;
						start.y = j;
						start.z = k;
						step[i][j][k] = 0;	// 出发点的step初始化为0
					}
					else if (maze[i][j][k] == 'E')
					{
						end.x = i;
						end.y = j;
						end.z = k;
					}
				}
			}
		}
		path.push_back(start);
		while(!path.empty())
		{
			current = path.front();			// 取队首节点
			path.pop_front();				// 队首节点出队
			for (i=0; i<6; i++)				// 尝试6种移动方向
			{
				next = walk(current,i);
				if (judge(next,l,r,c,maze) && step[next.x][next.y][next.z] == -1)// 相邻节点是有效节点且没走过
				{
					path.push_back(next);										// 将相邻节点入队
					step[next.x][next.y][next.z] = step[current.x][current.y][current.z] + 1;	// 路长+1
				}
				if (next == end)
				{
					found = true;
					break;
				}
			}
			if (found == true)
			{
				break;
			}
		}
		if (found)
		{
			cout << "Escaped in " << step[end.x][end.y][end.z] << " minute(s)." << endl;
		}
		else
		{
			cout << "Trapped!" << endl;
		}


		for (i=0; i<l; i++)
		{
			for (j=0; j<r; j++)
			{
				delete[] maze[i][j];
				delete[] step[i][j];
			}
			delete[] maze[i];
			delete[] step[i];
		}
		delete[] maze;
		delete[] step;

		fin >> l >> r >> c;
	}
	fin.close();
	return 0;

#endif
#ifdef	ONLINE_JUDGE
	int l,r,c,i,j,k;
	deque<node> path;						// 宽搜双端队列
	node start, end, current, next;
	bool found = false;
	cin >> l >> r >> c;
	while (l && r && c)
	{
		path.clear();
		found = false;
		char*** maze = new char**[l];		// 地图信息
		int*** step = new int**[l];		// 记录走到每个点的步数
		for (i=0; i<l; i++)
		{
			maze[i] = new char*[r];
			step[i] = new int*[r];
			for (j=0; j<r; j++)
			{
				maze[i][j] = new char[c];
				step[i][j] = new int[c];
			}
		}
		for (i=0; i<l; i++)
		{
			for (j=0; j<r; j++)
			{
				for (k=0; k<c; k++)
				{
					cin >> maze[i][j][k];
					step[i][j][k] = -1;		// 步数初始化为-1表示没有该点走过
					if (maze[i][j][k] == 'S')
					{
						start.x = i;
						start.y = j;
						start.z = k;
						step[i][j][k] = 0;	// 出发点的step初始化为0
					}
					else if (maze[i][j][k] == 'E')
					{
						end.x = i;
						end.y = j;
						end.z = k;
					}
				}
			}
		}
		path.push_back(start);
		while(!path.empty())
		{
			current = path.front();			// 取队首节点
			path.pop_front();				// 队首节点出队
			for (i=0; i<6; i++)				// 尝试6种移动方向
			{
				next = walk(current,i);
				if (judge(next,l,r,c,maze) && step[next.x][next.y][next.z] == -1)// 相邻节点是有效节点且没走过
				{
					path.push_back(next);										// 将相邻节点入队
					step[next.x][next.y][next.z] = step[current.x][current.y][current.z] + 1;	// 路长+1
				}
				if (next == end)
				{
					found = true;
					break;
				}
			}
			if (found == true)
			{
				break;
			}
		}
		if (found)
		{
			cout << "Escaped in " << step[end.x][end.y][end.z] << " minute(s)." << endl;
		}
		else
		{
			cout << "Trapped!" << endl;
		}


		for (i=0; i<l; i++)
		{
			for (j=0; j<r; j++)
			{
				delete[] maze[i][j];
				delete[] step[i][j];
			}
			delete[] maze[i];
			delete[] step[i];
		}
		delete[] maze;
		delete[] step;

		cin >> l >> r >> c;
	}
	return 0;
#endif
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值