poj 2251 Dungeon Master

这道是个用个简单的宽搜或者深搜就能做出来,只需要注意有上,下,左,上一层,下一层,后共计6个方向。

难点在于读懂题意,地牢分为了n层,注意到这一点就简单了。

#include <iostream>
#include <queue>
using namespace std;
const int MAXSIZE = 300;
struct coord {
	int x, y, z;
	int step;
	bool operator()(const coord &a, const coord &b)
	{
		return a.step > b.step;
	}
};
char map[MAXSIZE][MAXSIZE][MAXSIZE];
int direct[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,-1,0,0,1};
int main()
{
	int i, j, k;
	while (true)
	{
		int a, b, c;
		coord start;
		int sum;
		priority_queue<coord, vector<coord>, coord> s;
		cin >> i >> j >> k;
		if (i == 0 && j == 0 && k == 0)
			break;
		for (c = 1; c <= i; c++)
			for (a = 1; a <= j; a++)
				for (b = 1; b <= k; b++)
				{
					cin >> map[c][a][b];
					if (map[c][a][b] == 'S')
					{
						start.z = c;
						start.x = a;
						start.y = b;
						start.step = 0;
					}
				}	
		s.push(start);
		map[start.z][start.x][start.y] = '#';
		bool flag = false;
		while (!s.empty())
		{
			coord kk = s.top();
			s.pop();
			for (int b = 0; b < 6; b++)
			{
				start.z = kk.z + direct[b][0];
				start.x = kk.x + direct[b][1];
				start.y = kk.y + direct[b][2];
				if (start.z >= 1 && start.x >= 1 && start.z >= 1 && start.z <= i&&start.x <= j&&start.y <= k)
				{
					if (map[start.z][start.x][start.y] == '.')
					{
						start.step = kk.step + 1;
						s.push(start);
						map[start.z][start.x][start.y] = '#';
					}
					else if (map[start.z][start.x][start.y] == 'E')
					{
						flag = true;
						sum = kk.step + 1;
						break;
					}
				}
			}
			if (flag)
				break;
		}
		if (flag)
			cout << "Escaped in " << sum << " minute(s)." << endl;
		else
			cout << "Trapped!" << endl;
	}
}

你可能感兴趣的:(poj,宽搜)