蓝桥杯高频考点真题单——1.走迷宫

BFS,AC了,但是我的dfs过不了QWQ

AC代码:

#include <iostream>
#include <queue>
using namespace std;


struct Node
{
	int x, y;
	int step=0;	//当前步数
};


int n, m;
int a, b, c, d;
int minstep = 0x3f3f3f3f;
int g[102][102] = { 0 };
int book[102][102] = { 0 };

int mx[4] = { 0,0,1,-1 };
int my[4] = { 1,-1,0,0 };


void bfs()
{
	queue<Node>q;
	q.push({ a,b,0 });
	while (!q.empty())
	{
		Node cur = q.front();
		q.pop();

		if (cur.x == c && cur.y == d)
			minstep = min(minstep, cur.step);

		for (int i = 0; i < 4; i++)
		{
			Node nex;
			nex.x = cur.x + mx[i];
			nex.y = cur.y + my[i];
			nex.step = cur.step + 1;

			if (g[nex.x][nex.y] == 0 || book[nex.x][nex.y] == 1)	continue;
			q.push(nex);
			book[nex.x][nex.y] = 1;
		}
	}
}


int main()
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++)
			cin >> g[i][j];
	cin >> a >> b >> c >> d;
	bfs();
	if (minstep!=0x3f3f3f3f)	cout << minstep;
	else   cout << -1;

	return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值