#include <stdio.h>
#include <vector> //动态数组
#include <cstring>
using namespace std;//C++的动态数组
#define ROWS 21
#define COLS 21
//代价
#define ONE_len 10
enum direct {
p_right,//右
p_up,//上
p_left,//左
p_down,//下
};
int map[ROWS][COLS] = {
// 0 1 2 3 4 5 6 7 8 9
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0},//0
{1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1},
{1,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1},//1
{1,0,1,1,1,0,1,1,1,0,1,1,1,0,1,0,1,1,1,0,1},
{1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1},//2
{1,1,1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1},
{1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1},//3
{1,0,1,1,1,0,1,0,1,1,1,1,1,1,1,0,1,1,1,0,1},
{1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1},//4
{1,0,1,1,1,1,1,0,1,1,1,1,1,0,1,1,1,1,1,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1},//5
{1,0,1,1,1,0,1,1,1,1,1,1,1,0,1,0,1,1,1,0,1},
{1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1,0,0,0,1},//6
{1,1,1,0,1,1,1,0,1,1,1,1,1,0,1,0,1,0,1,1,1},
{1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,1},//7
{1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,0,1},
{1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,1,0,1,0,1},//8
{1,1,1,0,1,0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,1},
{0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1},//9
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
// 0 1 2 3 4 5 6 7 8 9
};
void make_map() //打印地图
{
int i, j;
for (i = 0; i < 21; i++)
{
for (j = 0; j < 21; j++)
{
switch (map[i][j])
{
case 0:printf(" "); break;
case 1:printf("墙"); break;
case 2:printf("十"); break;
}
}
printf("\n");
}
}//打印地图
struct POINT {
int x; //横轴
int y; //纵轴
int f;
int g;
int h;
// void setf() { f = g + h; }
}; //点结构体
struct TREE {
POINT pos;
vector<TREE*> child;
TREE* pParent;
}; //树节点结构体
int geth(POINT pos, POINT endpos)//获得h值
{
int x = (pos.x > endpos.x) ? (pos.x - endpos.x) : (endpos.x - pos.x);
int y = (pos.y > endpos.y) ? (pos.y - endpos.y) : (endpos.y - pos.y);
return ONE_len * (x+y);
}
TREE* createNode(int x, int y) {
TREE* pNew = new TREE;// 开内存
memset(pNew,0, sizeof(TREE));//全部为空
pNew->pos.x = x;
pNew->pos.y = y;
return pNew;
};
int main()
{
//标记是否找到终点
bool isfindend = false;
//记录点是否走过
bool poseMap[ROWS][COLS] = { 0 };
//起点
POINT begpos = {19,0};
//终点
POINT endpos = {1,20};
//准备一棵树
TREE* pRoot = NULL;
//起点入树
pRoot = createNode(begpos.x, begpos.y);
//标记起点走过
poseMap[begpos.x][begpos.y] = true;
//准备一个数组存最小f
vector<TREE*> buff;
//当前点
TREE* pCurrent = pRoot;
//找当前点的相邻点
TREE* pChild = NULL;
vector<TREE*>::iterator it;//循环遍历
vector<TREE*>::iterator itmin;//f最小
//make_map();
while (1) //循环寻路
{
for (int i = 0; i < 4; i++)
{
TREE* pChild= createNode(pCurrent->pos.x, pCurrent->pos.y);
switch (i){
case p_right:
pChild->pos.y++;
pChild->pos.g += ONE_len;
break;
case p_up:
pChild->pos.x--;
pChild->pos.g += ONE_len;
break;
case p_left:
pChild->pos.y--;
pChild->pos.g += ONE_len;
break;
case p_down:
pChild->pos.x++;
pChild->pos.g += ONE_len;
break;
}
if (map[pChild->pos.x][pChild->pos.y] != 1//不是障碍物
&& poseMap[pChild->pos.x][pChild->pos.y] != 1)//没有走过
{
//计算h值
pChild->pos.h = geth(pChild->pos, endpos);
//计算f值
pChild->pos.f = pChild->pos.g + pChild->pos.h;
//pChild->pos.setf();
//入树
pCurrent->child.push_back(pChild);//试探点成为当前点的孩子
pChild->pParent = pCurrent;//当前点成为试探点的父
//存入buff数组
buff.push_back(pChild);
}
else {delete pChild;}//释放内存
}
//从buff里找出f值最小的点
itmin = buff.begin();//假设第一个最小
for (it = buff.begin(); it != buff.end(); it++)
{
itmin = (((*it)->pos.f < (*itmin)->pos.f) ? it : itmin);
}
//走
pCurrent = *itmin;
//标记走过点
poseMap[pCurrent->pos.x][pCurrent->pos.y] = true;
// 从buff中删掉
buff.erase(itmin);
//判断是否找到终点
if (pCurrent->pos.x == endpos.x
&& pCurrent->pos.y == endpos.y)
{
isfindend = true;
break;
}
}
if (isfindend)
{
printf("找到终点了\n");
while (pCurrent)
{ // printf("(%d,%d)", current->pos.row, current->pos.col);
int q,e;
q=pCurrent->pos.x;
e=pCurrent->pos.y;
map[q][e] = 2;
pCurrent = pCurrent->pParent;
}
make_map();
}
return 0;
}
改变终点和起点的值,