胜利大逃亡(九度OJ 1456)(BFS搜索)

#include<stdio.h>
#include<queue>
using namespace std;
bool mark[50][50][50];
int maze[50][50][50];
int walknext[][3] =
{
	1,0,0,
	-1,0,0,
	0,1,0,
	0,-1,0,
	0,0,1,
	0,0,-1
};
struct point
{
	int x, y, z;
	int t;
};
queue<point> q;
int BFS(int x, int y, int z)
{
	while (q.empty() == false)
	{
		point now = q.front();
		q.pop();
		for (int i = 0; i < 6; i++)
		{
			int nx = now.x + walknext[i][0];
			int ny = now.y + walknext[i][1];
			int nz = now.z + walknext[i][2];
			if (nx < 0 || nx >= x || ny < 0 || ny >= y || nz < 0 || nz >= z)
				continue;
			if (maze[nx][ny][nz] == 1)
				continue;
			if (mark[nx][ny][nz] == true)
				continue;
			point temp;
			temp.x = nx;
			temp.y = ny;
			temp.z = nz;
			temp.t = now.t + 1;
			q.push(temp);
			mark[nx][ny][nz] = true;
			if (nx == x - 1 && ny == y - 1 && nz == z - 1)
				return temp.t;
		}
	}
	return -1;
}
int main()
{
	int a, b, c, t;
	while (scanf("%d%d%d%d",&a,&b,&c,&t) != EOF)
	{
		for (int i = 0; i < a; i++)
		{
			for (int j = 0; j < b; j++)
			{
				for (int k = 0; k < c; k++)
				{
					scanf("%d", &maze[i][j][k]);
					mark[i][j][k] = false;
				}
			}
		}
		while (!q.empty())
			q.pop();
		mark[0][0][0] = true;
		point temp;
		temp.x = temp.y = temp.z = temp.t = 0;
		q.push(temp);
		int rec = BFS(a, b, c);
		if (rec <= t)
			printf("%d\n", rec);
		else
			printf("-1\n");
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值