其他分享
首页 > 其他分享> > HDU 1253(胜利大逃亡)

HDU 1253(胜利大逃亡)

作者:互联网

基础广搜题。

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN = 55;

struct status //状态
{
	int x, y, z; //位置坐标
	int step; //步数
};

int A, B, C, T;
int mp[MAXN][MAXN][MAXN]; //地图
int vis[MAXN][MAXN][MAXN]; //访问情况
int dir[6][3] = { {0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0} }; //方向

//广搜,查找起点到终点的最小步数
int BFS()
{
	status start; //起点
	start.x = start.y = start.z = 0;
	start.step = 0;
	vis[start.x][start.y][start.z] = 1;

	queue<status> q;
	q.push(start);
	status now, next;
	while (!q.empty())
	{
		now = q.front();
		q.pop();
		if (now.x == A - 1 && now.y == B - 1 && now.z == C - 1) //到达终点
			return now.step;
		if (now.step > T) //超出步数限制
			return -1;
		for (int i = 0; i < 6; i++) //依次搜索6个方向
		{
			next.x = now.x + dir[i][0];
			next.y = now.y + dir[i][1];
			next.z = now.z + dir[i][2];
			if (next.x >= 0 && next.x < A && next.y >= 0 && next.y < B && next.z >= 0 && next.z < C
				&& vis[next.x][next.y][next.z] == 0 && mp[next.x][next.y][next.z] == 0)
			{
				next.step = now.step + 1;
				vis[next.x][next.y][next.z] = 1;
				q.push(next);
			}
		}
	}
	return -1;
}

int main()
{
	int K;
	scanf("%d", &K);
	while (K--)
	{
		scanf("%d%d%d%d", &A, &B, &C, &T);
		for (int i = 0; i < A; i++) //输入地图
		{
			for (int j = 0; j < B; j++)
				for (int k = 0; k < C; k++)
					scanf("%d", &mp[i][j][k]);
		}
		memset(vis, 0, sizeof(vis));
		printf("%d\n", BFS());
	}
	return 0;
}

继续加油。

标签:HDU,int,逃亡,next,start,MAXN,1253,&&,now
来源: https://blog.csdn.net/Intelligence1028/article/details/104758818