中装建设股票有潜力吗搜索引擎优化报告
广度优先搜索算法(BFS)是一种用于图形和树数据结构的搜索算法。该算法从根节点开始搜索,然后依次访问每个相邻节点。在搜索过程中,每个节点都标记为已访问,以避免重复访问。BFS算法适用于寻找最短路径的问题,因为它保证找到的解是距离根节点最近的解。
BFS算法的基本思想是使用队列保存已经访问的节点。首先将根节点入队,然后从队列中取出一个节点进行访问,并将与该节点相邻的未访问节点入队。重复这个过程,直到队列为空为止。BFS算法的时间复杂度为O(V+E),其中V为节点数,E为边数。
#include<iostream>//广度搜索,迷宫例题
using namespace std;
typedef struct node
{int x;int y;int f;int s;
};
int main()
{node que[2501];int a[51][51] = { 0 };//储存地图int book[51][51] = { 0 };//标记是否走过int next[4][2] = { {0,1},{1,0},{0,-1},{-1,0} };//右,下,左,上int head, tail;int x, y, q, p,n,m,tx,ty;cout << "输入地图规模:" << endl;cin >> n >> m;//输入地图规模for (int i = 1; i <= n; i++)for (int j = 1; j <= m; j++)cin >> a[i][j];cout << "输入起点、终点:" << endl;cin >> x >> y >> q >> p;//输入起点,终点head = 1, tail = 1;que[tail].x = x;que[tail].y = y;que[tail].f = 0;que[tail].s = 0;tail++;book[x][y] = 1;int flag = 0;//标记是否到达终点while (head < tail){for (int k = 0; k < 4; k++){tx = que[head].x + next[k][0];ty = que[head].y + next[k][1];if (tx<1 || tx>n || ty < 1 || ty>n)continue;if (a[tx][ty] == 0 && book[tx][ty] == 0)//没有障碍物且没走过{book[tx][ty] = 1;//标记已经走过que[tail].x = tx;que[tail].y = ty;que[tail].f = head;que[tail].s = que[head].s + 1;tail++;}if (tx == q && ty == p){flag = 1;break;}}if (flag == 1)break;head++;}cout << que[tail - 1].s;
}