我这个仅针对7行7列的迷宫写的,写的比较急,要计算n行m列的迷宫稍微改下就行了
代码如下,若有不妥请高手指点:
#include <iostream>
#include <set>
#include <string>
#include <cmath>
#include <cstdlib>
#define N 100
using namespace std;
struct node
{
double h,g,f;
int x,y;
bool operator<(node other)const
{
return f<other.f;
}
};
int map[N][N];//迷宫,1表示可以走,0表示不可以走
int row,col;//矩阵的行和列
multiset<node> open;//可以拓展节点
multiset<node>::iterator it;
node close[N][N];//已经走过的节点
int step[8][2] = {1,0,0,1,-1,0,0,-1,1,1,1,-1,-1,1,-1,-1};//走的方式
int prev[N*N];//存放前一个节点
void Init()
{
memset(close,0,sizeof(close));
}
double getg(node a,node b)//获取当前点到终点的路径值
{
return sqrt(1.0*(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool Is_Match(node a,node b)
{
if(a.x == b.x && a.y == b.y)
{
return true;
}
return false;
}
node begin,end;
int