breadth pathfinding algorithm

1. Breadth pathfinding rules
Different from depth pathfinding that follows a certain direction, breadth pathfinding lists all possible paths for the current layer and Traversing, and then traversing the next level, whoever reaches the end first is the shortest path.
2. Program implementation
Use vector container to implement. Or define the starting point and end point, and mark the initial point as passed. Then define the position of the current point and the location to be walked, and create a vector container for these points. The four directions of up, down, left, and right correspond to row+±- or col+±-. Make a judgment whether the current point can be walked. There are two situations where it cannot be walked: 1. The current point has been passed or a wall. 2. Beyond the scope of the map.
3. The specific procedures are as follows: :

// 广度寻路算法.cpp : 定义控制台应用程序的入口点。
/*
广度寻路算法是:将所有可能的路都列举出来,然后一层一层去走,谁先到谁就是最短路径
广度优先寻路算法实际上是对一个地图上所有可通行的点的遍历从而找出一条最短路径
*/

#include "stdafx.h"
#include<vector>
#include<iostream>
using namespace std;



#define ROW 11
#define COL 11

struct Node //树节点
{
    
    
	int row, col;
	Node* parent;//父节点
	vector<Node*>child;//子节点
};

struct MapNode  
{
    
    
	int value;
	bool isWalk;
};

struct MapPoint// 地图的行列
{
    
    
	int row;
	int col;		 
};

enum tryDir  //上下左右四个方向
{
    
    
	em_Down,
	em_Up,
	em_Left,
	em_Right,
};

int gMap[ROW][COL] = {
    
    
		{
    
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
		{
    
     1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
		{
    
     1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1 },
		{
    
     1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1 },
		{
    
     1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1 },
		{
    
     1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1 },
		{
    
     1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
		{
    
     1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0 },
		{
    
     1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1 },
		{
    
     1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1 },
		{
    
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};

MapNode gMapNode[ROW][COL];

void InitMapNode();

Node* CreatNode(MapPoint point);

bool isCanWalk(MapPoint point);

int _tmain(int argc, _TCHAR* argv[])
{
    
    
	InitMapNode();

	MapPoint startPos = {
    
     1, 1 }, endPos = {
    
     7, 10 };
	gMapNode[startPos.row][startPos.col].isWalk = true;//将初始位置标记为走过

	bool isFindEnd = false;

	Node* pRoot = CreatNode(startPos);

	MapPoint currentPos = startPos;
	MapPoint nextPos;

	vector<Node*>currentLevel;
	currentLevel.push_back(pRoot);

	while (1)
	{
    
    
		vector<Node*>nextLevel;//存储每一层可通行的点 

		for (int i = 0; i < currentLevel.size(); i++)//遍历当前层所有节点
		{
    
    
			for (int j = 0; j < 4; j++)//四个方向
			{
    
    
				nextPos.col = currentLevel[i]->col;//将要走的下一个节点
				nextPos.row = currentLevel[i]->row;
				switch (j)
				{
    
    
				case em_Down:nextPos.row++; break;
				case em_Up:nextPos.row--; break;
				case em_Left:nextPos.col--; break;
				case em_Right:nextPos.col++; break;
				}
				if (isCanWalk(nextPos))//判断下一个是否能走
				{
    
    
					if (nextPos.row==endPos.row && nextPos.col==endPos.col)
					{
    
    
						isFindEnd = true;//找到终点
						break;
					}

					Node* pNew = CreatNode(nextPos);//产生一个新节点

					currentLevel[i]->child.push_back(pNew);//将可以走的这个点作为上一个可以走的节点孩子入树
					pNew->parent = currentLevel[i];//上一个可以走的节点作为当前可以走的节点的父亲
					gMapNode[nextPos.row][nextPos.col].isWalk = true;//标记已经走过了
					nextLevel.push_back(pNew);//
				}

			}
		}
		if (isFindEnd)
		{
    
    
			printf("找到了终点\n");
			break;
		}
		if (nextLevel.size()==0)
		{
    
    
			printf("没有找到终点啊\n");
			break;
		}
		currentLevel = nextLevel;
	}

	return 0;
}

void InitMapNode()
{
    
    
	for (int i = 0; i < ROW; i++)
	{
    
    
		for (int j = 0; j < COL; j++)
		{
    
    
			gMapNode[i][j].value = gMap[i][j];
		}
	}
}


Node* CreatNode(MapPoint point)
{
    
    
	Node* pNode = new Node;
	pNode->col = point.col;
	pNode->row = point.row;
	pNode->parent = 0;
	return pNode;
}

bool isCanWalk(MapPoint point)
{
    
    
	if (point.row>=ROW || point.row<0 || point.col>=COL || point.col<0)
	{
    
    
		return false;
	}

	if (gMapNode[point.row][point.col].value == 1 && gMapNode[point.row][point.col].isWalk==true)
	{
    
    
		return false;
	}
	return true;
}

Guess you like

Origin blog.csdn.net/appup/article/details/115748998