[Breadth-first traversal]|BFS|Tree and graph traversal ideas|Simple ideas of the shortest path

1. Breadth-first traversal

The algorithm implementation of breadth-first traversal is relatively simple. First of all, we can know its traversal idea. The breadth-first traversal idea is the idea of ​​a group of mice. Starting from one point, many mice are used. These mice are very lazy. Every day, every mouse It will only take one step. Of course, when we are simulating this entry and exit, we use the queue to simulate. This simulation is actually the operation of pushing the queue into and out of the stack.

insert image description hereBreadth-first traversal has no repeated operations, and each point will be traversed at most once. The traversal simulation method is to store the lower-level nodes obtained each time in a queue, and this queue will only complete the previous layer. The operation will proceed to the next level of recursive operation.

Second, the idea and code implementation of breadth-first traversal

1. The idea of ​​breadth-first traversal

Breadth-first traversal is a kind of idea of ​​queue. There is no nesting in breadth-first traversal. Generally speaking, there is an initial point in breadth-first traversal, and in breadth-first traversal, we generally need the first set of The data is processed to ensure that certain values ​​and parameters at the first point of the queue can be enqueued.

	queue <pair<int,int> > q ;
	//1.create queue ;
	d[0][0] = 0 ;
	q.push({
    
    0,0});
	//2.finish queue 参数 ,这里是位置距离参数 

Then it is to judge whether there is a value in the queue and perform a recursive operation. When there is still a value in the queue, we take the first value in the queue to find its lower layer. We need to judge whether the lower layer meets the value condition. When we can complete the fetch, we will perform the assignment operation.

1. The queue is not empty
2. Take the head of the queue, pop the head of the queue <> to judge the following elements, judge whether this element can be
3, process the parameters of the elements that can be taken, and push them into the stack

if(q.size())
		//1.判断队列是否不为空
	{
    
    
		auto t = q.front() ;
		q.pop() ;
		//2.下一层判定 —— 取队列(记得弹出)
		for(int i = 0 ; i < 4 ; i ++ )
		{
    
    
			int x = t.first+ dx[i] ;
			int y = t.second + dy[i] ;
			//2. 下一层判定 —— 取可能的下一层的值,并且进行判断
			if(x >= 0 && y >= 0 && y < m && x < n && d[x][y] == - 1 )
			{
    
    
				//3.下一层判定 —— 对参数进行处理
				d[x][y] = d[t.first][t.second] + 1 ;
				q.push({
    
    x,y}) ;
			}
		}
	}

2, the code of breadth-first traversal

#include <iostream>
#include <queue>
#include <algorithm>

using namespace std ;
const int N = 110 ;
int n , m ;

int g[N][N] ;
int d[N][N] ;
 

int bfs()
{
    
    

	queue <pair<int,int> > q ;
	//1.create queue ;
	d[0][0] = 0 ;
	q.push({
    
    0,0});
	//2.finish queue 参数 

	int dx[4] = {
    
    0 , 1 , 0 , - 1 };
	int dy[4] = {
    
    1 , 0 , -1,   0 };

	if(q.size())
		//1.判断队列是否不为空
	{
    
    
		auto t = q.front() ;
		q.pop() ;
		//2.下一层判定 —— 取队列(记得弹出)
		for(int i = 0 ; i < 4 ; i ++ )
		{
    
    
			int x = t.first+ dx[i] ;
			int y = t.second + dy[i] ;
			//2. 下一层判定 —— 取可能的下一层的值,并且进行判断
			if(x >= 0 && y >= 0 && y < m && x < n && d[x][y] == - 1 )
			{
    
    
				//3.下一层判定 —— 对参数进行处理
				d[x][y] = d[t.first][t.second] + 1 ;
				q.push({
    
    x,y}) ;
			}
		}
	}
	return d[n - 1][m - 1] ;
}

int main () 
{
    
    
	
	cin >>  n >> m ;
	for(int i = 0 ; i < n ; i ++ )
		for(int j = 0 ; j < m ; j ++ )
			cin >> g[i][j] ,d[i][j] = - 1;
	cout << bfs() << endl ;
	return 0 ;
}

Third, the template for breadth-first traversal

The main idea of ​​breadth-first traversal is the processing of a queue, which is a core operation of BFS .

First of all, we should create a queue to complete the push of the value of the first point and the assignment of parameters

When there are still parameters in the queue, we perform a loop operation to find all possible points
1, first take the head of the queue
2, and take the point operation
3 for the opposite head to judge whether it is reasonable

Complete the assignment operation

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

void bfs ()
{
    
    
	queue <int>  q;
	q.push() ;
	参数赋值
	// 完成队列的创建 , 队列第一个值的赋值操作
	while (q.size() )
	{
    
    
		int t = q.front() ;
		//取队头 
		for( 向下的递归)
		{
    
    
		if(判断能不能取)
		{
    
    
			//参数赋值,压入操作
		}
		}
	}

}


int main ()
{
    
    
	return 0 ;
}

Supongo que te gusta

Origin blog.csdn.net/wen030803/article/details/131860959
Recomendado
Clasificación