BFS-广度优先搜索(Breadth-First-Search)

山再高,往上爬,总能登顶;
路再长,走下去,定能到达。

BFS-广度优先搜索(Breadth-First-Search)

在这里插入图片描述
首先来张图来具体的形象化理解一下什么是广度优先搜索。在考虑哈夫曼距离的条件下,寻找最短的方案到达某点就可以以自己为中心方圆开始查询。这就是bfs。
然后用一张GIF图来显示代码实现的逻辑图
在这里插入图片描述
整体逻辑如上图所示,我们需要一个数组判断,一个来储存(接收),一个来存放任务。
我们就是在这个队列中的第一个元素开始走起,走过的不能走的都标记成1不再去走,其余的扩展一个存一个,如果走到了最后的目的地,那么就结束bfs
我们需要选择队列的原因是,队列是先进先出,这样就可以保证一定的顺序性。
我们就可以得出以下代码

#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<utility>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#pragma warning(disable:4244)
#define PI 3.1415926536
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const char char_inf = 127;
inline ll read() {
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
inline void out(ll a)
{
	if (a < 0)putchar('-'), a = -a;
	if (a >= 10)out(a / 10);
	putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1)res = (res * x) % mod;
		x = (x * x) % mod; n >>= 1;
	}
	return res;
}
#define read read()
int  n, m;
struct node {
	int x, y, step;
};
node temp;
bool judge[1000][1000];//判断数组,判断是否已经走过,或者是否可走
queue<node>q;
int dx[] = { 0,0,1,-1 };//简便行走上下左右
int dy[] = { 1,-1,0,0 };
void bfs()
{
	while (1)//一直走,知道无解或者有答案为止
	{
		if (q.empty())//如果已经走完全部的队列都没有结果就说明无解
		{
			cout << -1 << endl;
			return;
		}
		temp = q.front();//取任务
		q.pop();//删除任务
		judge[temp.x][temp.y] = 1;//该点已走过
		for (int i = 0; i < 4; i++)//四个方向都加队列
			if (!judge[temp.x + dx[i]][temp.y + dy[i]])//如果没有走过,可行就加
			{
				if (temp.x + dx[i] == n && temp.y + dy[i] == m)//如果当前走到了终点,就输出答案
				{
					cout << temp.step + 1 << endl;
					return;
				}
				q.push({ temp.x + dx[i], temp.y + dy[i], temp.step + 1 });//不是终点就入队列
			}
	}
}
int main()
{
	n = read, m = read;
	for (int i = 0; i <= n + 1; i++)judge[i][0] = judge[i][m + 1] = 1;//围墙
	for (int i = 0; i <= m + 1; i++)judge[0][i] = judge[n + 1][i] = 1;//围墙
	for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)judge[i][j] = read;//接收数据
	q.push({ 1,1,1 });//入队列第一个元素
	bfs();
}

By-轮月

发布了32 篇原创文章 · 获赞 8 · 访问量 1168

猜你喜欢

转载自blog.csdn.net/qq_35339563/article/details/105027590