2021JMU蓝桥校选搜索bfs

7-5 爱瞎跑的学长

题目
三维的搜索题,我也不知道为什么会卡内存,出的时候没注意这个,以前题目改的。
vis数组记录这个点有没有走过,fr数组记录上一个点的位置,ste数组是走到当前这一步需要的动作。
每一个点的定义

struct Point
{
	int l;
	int x;
	int y;
	int step;
};

记录位置和到这个位置是第几步
题目要求的路径字典序最小解决办法

int dx[6] = { -1, 0, 1, 0,0,0 };
int dy[6] = {  0, 0, 0,-1,1,0 };
int dz[6] = {  0,-1, 0, 0,0,1 };
char d[7] = "bdflru";

下一步的选择直接字典序排序,这样就保证当前走的一定是最符合条件的。
要求的输出路径,用了一个递归函数,依次输出走到这一步需要的位置(从终点倒推回来),通过递归的形式实现正序。
然后就是bfs中的判断边界条件和是否可以向上向下爬比较复杂

			if (i == 1||i==5) {
				if ((map[now.l][now.x][now.y] != '+') && (map[next.l][next.x][next.y] != '+'))continue;
			}
			if ((map[next.l][next.x][next.y] == '*') || (next.x < 0) || (next.x >= l) || (next.y < 0) || (next.y >= r) || (next.l < 0) || (next.l >= n))
				continue;

然后源代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int inf = 0xffffff;
char map[52][105][105];
int vis[52][105][105];
char ste[52][105][105];
int n, l, r;
int ans;
struct Point
{
	int l;
	int x;
	int y;
	int step;
};
Point s, w,fr[52][105][105];
int dx[6] = { -1, 0, 1, 0,0,0 };
int dy[6] = {  0, 0, 0,-1,1,0 };
int dz[6] = {  0,-1, 0, 0,0,1 };
char d[7] = "bdflru";
bool flag = 0;
void BFS()
{
	queue<Point> q;
	vis[s.l][s.x][s.y] = 1;
	memset(vis, 0, sizeof(vis));
	q.push(s);
	while (!q.empty())
	{
		Point now = q.front();
		q.pop();
		for (int i = 0; i < 6; i++)
		{
			Point next;
			next.x = now.x + dx[i];
			next.y = now.y + dy[i];
			next.l = now.l + dz[i];
			next.step = now.step + 1;
			if (i == 1||i==5) {
				if ((map[now.l][now.x][now.y] != '+') && (map[next.l][next.x][next.y] != '+'))continue;
			}
			if ((map[next.l][next.x][next.y] == '*') || (next.x < 0) || (next.x >= l) || (next.y < 0) || (next.y >= r) || (next.l < 0) || (next.l >= n))
				continue;
			if (map[next.l][next.x][next.y] == 'E')
			{
				ans = next.step;
				flag = 1;
				ste[next.l][next.x][next.y] = d[i];
				fr[next.l][next.x][next.y].l = now.l;
				fr[next.l][next.x][next.y].x = now.x;
				fr[next.l][next.x][next.y].y = now.y;
				return;
			}
			if (((map[next.l][next.x][next.y] == '.') || (map[next.l][next.x][next.y] == '+')) && !vis[next.l][next.x][next.y])
			{

				vis[next.l][next.x][next.y] = 1;
				ste[next.l][next.x][next.y] = d[i];
				q.push(next);
				fr[next.l][next.x][next.y].l = now.l;
				fr[next.l][next.x][next.y].x = now.x;
				fr[next.l][next.x][next.y].y = now.y;
			}
		}
	}
}
void prin(int x,int y,int l) {
	if (x == s.x && y == s.y && l == s.l)return;
	prin(fr[l][x][y].x, fr[l][x][y].y, fr[l][x][y].l);
	printf("%c",ste[l][x][y]);
}
int main()
{
	int T;
	cin >> T;
	getchar();
	while (T--)
	{
		int wl, wx, wy;
		scanf("%d%d%d", &n, &l, &r);
		if (n == 0 && l == 0 && r == 0) break;
		memset(map, 0, sizeof(map));
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < l; j++)
			{
				for (int k = 0; k < r; k++)
				{
					cin >> map[i][j][k];
					if (map[i][j][k] == 'P')
					{
						s.l = i;
						s.x = j;
						s.y = k;
					}
					if (map[i][j][k] == 'E')
					{
						wl = i;
						wx = j;
						wy = k;
					}
				}
			}

		}
		s.step = 0;
		flag = 0;
		BFS();
		if (flag) {
			printf("%d ", ans);
			prin(wx,wy,wl);
			printf("\n");
		}
		else
			printf("xie'e\n");
	}
	return 0;
}

感觉是有点烦,代码有点长,测的时候一直在纠结数据怎么出

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值