poj2251 DungeonMaster BFS

poj2251

  • BFS
#pragma warning(disable:4996)
#include<iostream>
#include<string>
#include<cmath>
#include<ctype.h>
#include<memory.h>
#include<string.h>
#include<algorithm>
#include<map>
#include<iomanip>
#include<set>
#include<list>
#include<vector>
#include<stack>
#include<queue>
#define ll long long int
using namespace std;
struct node
{
	int x, y, z;
	int step;//记录走过的步数,最终即为答案
};
int L, R, C;
char a[35][35][35];
int vis[35][35][35];
int dir[10][5] = { {1,0,0},{-1,0,0}, {0,1,0},{0,-1,0},{0,0,1},{0,0,-1} };

queue<node> q;
int bfs(node s, node e)
{
	memset(vis, 0, sizeof(vis));
	while (!q.empty()) q.pop();

	s.step = 0;
	vis[s.x][s.y][s.z] = 1;
	q.push(s);
	while (!q.empty())
	{
		node now = q.front();
		q.pop();

		if (now.x == e.x && now.y == e.y && now.z == e.z)
			return now.step;

		for (int i = 0; i < 6; i++)
		{
			node t;
			t.x = now.x + dir[i][0];
			t.y = now.y + dir[i][1];
			t.z = now.z + dir[i][2];

			if (t.x >= 1 && t.x <= L &&
				t.y >= 1 && t.y <= R &&
				t.z >= 1 && t.z <= C &&
				a[t.x][t.y][t.z] != '#' &&
				!vis[t.x][t.y][t.z])
			{
				vis[t.x][t.y][t.z] = 1;
				t.step = now.step + 1;
				q.push(t);
			}
		}
	}
	return -1;
}

int main()
{
	while (1)
	{
		memset(a, 0,sizeof(a));
		memset(vis, 0, sizeof(vis));

		cin >> L >> R >> C;
		if (L == 0 && R == 0 && C == 0)
			break;
		for (int i = 1; i <= L; i++)
			for (int j = 1; j <= R; j++)
				for (int k = 1; k <= C; k++)
					cin >> a[i][j][k];

		node s, e;
		for (int i = 1; i <= L; i++)
			for (int j = 1; j <= R; j++)
				for (int k = 1; k <= C; k++)
				{
					if (a[i][j][k] == 'S')
					{
						s.x = i; s.y = j; s.z = k;
					}
					if (a[i][j][k] == 'E')
					{
						e.x = i; e.y = j; e.z = k;
					}
				}
		int ans = bfs(s, e);
		if (ans == -1)
			cout << "Trapped!" << endl;
		else
			cout << "Escaped in " << ans << " minute(s)." << endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值