Breadth first search algorithm

Breadth first search algorithm

Introduction to Algorithm

Breadth first search is also called breadth first search or horizontal first search. The algorithm is a graph search algorithm. The algorithm starts from the starting point and then searches down layer by layer. If the target is found or all nodes are searched, the algorithm ends.

Practical practice

This algorithm can be used to solve the maze path finding problem. Two core parts need to be dealt with when solving this problem. The first part is to find the next layer (new feasible position), and the second part is to record the new feasible position. Since the search process is a downward search layer by layer, that is, after exploring a node, you need to record the new feasible position and the previous node that reached this position, and delist the position and start the search for the next node , Here use the queue to store data (first in, first out). Since every feasible position found is recorded the last position to reach this point, at this time, the last searched end of the algorithm can be used to output the reverse order layer by layer (because each searched position records the previous Position) The output result is the feasible path. In order to prevent the location found is the location that has been searched, mark the location that has been walked, here I mark it as -1.
The source code picture is as follows: The
Breadth first search algorithm
source code is as follows:

#include<iostream>
#include<stdlib.h>
using namespace std;
typedef struct {
	int x;
	int y;
	int last;//记录上一个标号
}decation;
typedef struct {
	decation* point;
	int length;
	int list_size;
}LIST;
int lybyrince[10][10] = { 1,1,1,1,1,1,1,1,1,1,
						  1,0,0,0,0,0,0,0,0,1,
						  1,0,1,1,1,1,1,1,0,1,
						  1,1,0,1,1,1,1,1,0,1,
						  1,0,1,0,1,1,1,1,0,1,
						  1,1,1,1,0,1,1,1,0,1,
						  1,1,1,0,1,1,1,1,1,1,
						  1,1,1,1,0,1,1,1,0,1,
						  1,1,1,1,1,0,0,0,0,1,
						  1,1,1,1,1,1,1,1,1,1 };
int xx[9] = { 0,1,1,0,-1,-1,-1,0,1 };//不同方向下的x的偏移值
int yy[9] = { 0,0,-1,-1,-1,0,1,1,1 };//不同方向下的y的偏移值
int search_direct(int a, LIST& b, int c, int d);
void show(LIST a);
int main()
{
	int biaozhi;
	int n = 1;
	int last_x = 8; int last_y = 8;
	decation* p;
	LIST list;
	list.point = (decation*)malloc(100 * sizeof(decation));
	list.length = 2; list.list_size = 100;
	p = list.point;
	p[0].last = 0;
	p[1].x = 1; p[1].y = 1; p[1].last = 0;//记入开始位置
	lybyrince[1][1] = -1;//把走过的位置记为-1,防止寻找到的位置是走过的
	while (1)
	{
		biaozhi = search_direct(n, list, last_x, last_y);
		if (biaozhi) break;//知道寻找到终点结束循环
		n = n + 1;//一层一层向外寻找可以通过的路径
	}
	show(list);
}
int search_direct(int a, LIST& b, int c, int d)
{
	decation* pp = b.point;
	int i = b.length-1;
	for (int n = 1; n <= 8; n++)//一共八个方向
	{
		if (lybyrince[pp[a].x + xx[n]][pp[a].y + yy[n]] == 0)//寻找没有走过的并且没有障碍的位置
		{
			pp[i + 1].x = pp[a].x + xx[n]; pp[i + 1].y = pp[a].y + yy[n];//计入新找到的可行位置的坐标
			pp[i + 1].last = a;//记入这一步的位置是由哪一步到来的
			i = i + 1;
			lybyrince[pp[i].x][pp[i].y] = -1;//此时新到达的位置已经走过了,把他标记为-1;
			b.length = b.length + 1;//线性表长度加一
			if (pp[i].x == c && pp[i].y == d)
				break;//如果找到的位置是终点则结束循环
		}
	}
	if (pp[i].x == c && pp[i].y == d) return 1;//如果找到的位置是终点返回一来退出主函数里的循环
	else return 0;
}
void show(LIST a)//输出经过的路径
{
	int n;
	n = a.length - 1;
	while (n != 0)
	{
		cout << a.point[n].x << " " << a.point[n].y << endl;
		n = a.point[n].last;
	}
}

Guess you like

Origin blog.csdn.net/ownplace/article/details/109036029