广度寻路算法

1、广度寻路规则
与深度寻路沿着某一方向走不同的是,广度寻路将当前层所有可能要走的路都列举出来并遍历,然后接着下一层遍历,最后谁先走到终点谁就是最短路径。
2、程序实现
利用vector容器实现。还是定义起始点与终点,将初始点标记已经走过。接着定义当前点的位置与将要走的位置,并为这些点建立vector容器。上、下、左、右四个方向,对应的row+±-或col+±-。做一个判断当前点是否能走,不能走的有2种情况:1、当前点已经走过或者墙。2、超出地图范围。
3、具体程序如下

// 广度寻路算法.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;
}

猜你喜欢

转载自blog.csdn.net/appup/article/details/115748998