#pragma once
#include <iostream>
#include <list>
#include <cmath>
#include <Windows.h>
using namespace std;
typedef struct dian
{
int y;
int x;
struct dian* befront;
int f;
int g;//到起点
int h;//到终点
}Dian;
class AXXL
{
private:
list <Dian*> listdian;
list <Dian*> shifang;
public:
int StarX;
int StarY;
int EndX;
int EndY;
int X;
int Y;
int* map;
AXXL(int sx, int sy, int ex, int ey, int x, int y, int* pp);
void setmap();
bool isCanMove(int l, int r);
void xunlu();
Dian* minF();
void shuchu();
};
#include "AXXL.h"
AXXL::AXXL(int sx, int sy, int ex, int ey, int x, int y, int* pp)
{
this->StarX = sx;
this->StarY = sy;
this->EndX = ex;
this->EndY = ey;
this->X = x;
this->Y = y;
this->map = pp;
}
void AXXL::setmap()
{
system("cls");
for (size_t i = 0; i < Y; i++)
{
for (size_t j = 0; j < X; j++)
{
cout << *(map + (i * X) + j);
}
cout << endl;
}
cout << endl;
}
bool AXXL::isCanMove(int x, int y)
{
if (x<0||x>(this->X-1)||y<0||y>(this->Y-1))
{
return false;
}
if (*(map+(y*this->X)+x)==1|| *(map + (y * (this->X)) + x) == 2|| *(map + (y * this->X) + x) == 3)
{
return false;
}
return true;
}
void AXXL::xunlu()
{
int t_x;
int t_y;
Dian* t = new Dian;
t->x = this->StarX;
t->y = this->StarY;
t->befront = NULL;
t->g = (abs(t->x-this->StarX)+abs(t->y-this->StarY));
t->h= (abs(t->x - this->EndX) + abs(t->y - this->EndY));
t->f = t->g + t->h;
listdian.push_back(t);
shifang.push_back(t);
do
{
if (listdian.empty())
{
break;
}
t = minF();
t_x = t->x;
t_y = t->y;
if (t_x == EndX && t_y == EndY)
{
break;
}
if (isCanMove(t_x-1,t_y))
{
*(map + (t_y * (this->X)) + t_x - 1) = 2;
Dian* nks = new Dian();
nks->x = t_x - 1;
nks->y = t_y;
nks->befront = t;
nks->g = (abs(t_x-1 - this->StarX) + abs(t_y - this->StarY));
nks->h = (abs(t_x-1 - this->EndX) + abs(t_y - this->EndY));
nks->f = nks->g + nks->h;
listdian.push_back(nks);
shifang.push_back(nks);
nks = NULL;
}
if (isCanMove(t_x, t_y+1))
{
*(map + ((t_y+1) * this->X) + t_x) = 2;
Dian* nks = new Dian();
nks->x = t_x;
nks->y = t_y+1;
nks->befront = t;
nks->g = (abs(t_x - this->StarX) + abs(t_y+1 - this->StarY));
nks->h = (abs(t_x - this->EndX) + abs(t_y+1 - this->EndY));
nks->f = nks->g + nks->h;
listdian.push_back(nks);
shifang.push_back(nks);
nks = NULL;
}
if (isCanMove(t_x + 1, t_y))
{
*(map + (t_y * this->X) + t_x+1) = 2;
Dian* nks = new Dian();
nks->x = t_x+1;
nks->y = t_y;
nks->befront = t;
nks->g = (abs(t_x+1 - this->StarX) + abs(t_y - this->StarY));
nks->h = (abs(t_x+1 - this->EndX) + abs(t_y - this->EndY));
nks->f = nks->g + nks->h;
listdian.push_back(nks);
shifang.push_back(nks);
nks = NULL;
}
if (isCanMove(t_x , t_y-1))
{
*(map + ((t_y-1) * this->X) + t_x) = 2;
Dian* nks = new Dian();
nks->x = t_x;
nks->y = t_y-1;
nks->befront = t;
nks->g = (abs(t_x - this->StarX) + abs(t_y-1 - this->StarY));
nks->h = (abs(t_x - this->EndX) + abs(t_y-1 - this->EndY));
nks->f = nks->g + nks->h;
listdian.push_back(nks);
shifang.push_back(nks);
nks = NULL;
}
t = NULL;
listdian.pop_front();
//setmap();
if (listdian.empty())
{
cout << "无路可走" << endl;
}
//Sleep(10);
} while (true);
}
Dian* AXXL::minF()
{
list<Dian*>::iterator it1;
list<Dian*>::iterator it2;
for (it1 = listdian.begin(); it1 != listdian.end(); it1++)
{
for (it2 = next(it1,1); it2 != listdian.end(); it2++)
{
if ((*it1)->f>(*it2)->f)
{
swap(*it1, *it2);
}
}
}
return listdian.front();
}
void AXXL::shuchu()
{
//system("cls");
Dian* luxian=NULL;
if (!listdian.empty())
{
luxian = listdian.front();
}
int i = 0;
while (luxian!=NULL)
{
*(map + (luxian->y) * this->X + luxian->x) = 5;
luxian = luxian->befront;
i++;
}
while (!shifang.empty())
{
free(shifang.front());
shifang.pop_front();
}
listdian.clear();
shifang.clear();
for (size_t i = 0; i < Y; i++)
{
for (size_t j = 0; j < X; j++)
{
//cout << (*(map + (i * X) + j));
if (*(map + (i * X) + j)==5)
{
cout << "*";
}
else if (*(map + (i * X) + j) == 1)
{
cout << "#";
}
else
{
cout << " ";
}
}
cout << endl;
}
cout << endl;
cout << i << endl;
}
#include <iostream>
#include "AXXL.h"
using namespace std;
int map[20][20] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{3,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,4},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
int main()
{
AXXL a(0,9,19,9,20,20,&map[0][0]);
//a.setmap();
a.xunlu();
a.shuchu();
return 0;
}
![](https://img-blog.csdnimg.cn/52c83bf375e9483aaadd0cf1766d8b97.png)