利用广度优先搜索求两点之间的无权最短路径长和无权最短路径
- 这里的无权表示图中的所有边权值都相等
- 0表示可以走的路,1表示障碍无法通过
- 2 3 4 5 分别表示方向上右下左
- 缺点:使用BFS搜索两点之间的最短路径效率太低,不过可以当作基础知识储备为后续学习做铺垫,有兴趣的可以了解Dijkstra算法和A*、B*算法
#include<iostream>
#include<queue>
using namespace std;
//代表节点的坐标和从起始点到当前位置的无权最小路径长
typedef struct Vertex {
int x, y;//坐标
int dist;//无权最小路径长
Vertex(int x, int y, int dist) {
this->x = x;
this->y = y;
this->dist = dist;
}
}Vertex;
//地图
const int m = 5, n = 5;
char Map[m][n] ={
"0001",
"0100",
"1001",
"0100",
"0110" };
// 2 3 4 5 代表方向
// 上右下左
//无权最短路径
int UnWeighted() {
Vertex s = Vertex(0, 0, 0);//起始点
Map[