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

發佈了68 篇原創文章 · 獲贊 14 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章