A-Star算法是一种静态路网中求解最短路径最有效的直接搜索方法
其实百科有

http://baike.baidu.com/link?url=CvmkWQIAmztYgMq3Nk1WyWkDiC0koVQALKzE4wBF4CWbYBtT19iWMBdSht9LBf7ZjUnA509U-JGWvxDYBk5LCq

 

咳咳,直接上代码。各种注释也算是有助理解了,毕竟这还是抄的~

 

// A*寻路算法.cpp : 定义控制台应用程序的入口点。

// Win32控制台程序

#include
<math.h>

#include
<list>

 

using
namespace
std;

 

/*

把地图当成一个个的格子

公式:F=G+H

F:相对路径长度

G:从起点沿着产生的路径,移动到指定点的耗费(路径长度)

H:预估值,从指定的格子移动到终点格子的预计耗费

使用两个表来保存相关数据

启动列表:有可能将要经过的点存到启动列表

关闭列表:不会再被遍历的点

 

步骤

1、将起点格子加入启动列表中

2、在启动列表中查找权值(F值)最小的格子

3、查找它周围的能走的格子

4、把这些格子加入启动列表中,已经在启动或关闭列表中的格子不用加入

5、把这些加入启动列表的格子的"父格子"设为当前格子

6、再把当前格子从启动列表中删除,加入关闭列表中

7、如果终点在启动列表中,则找到路径,退出流程,不进行第9步

8、如果启动列表中没有格子了,说明没有找到路径,退出流程,不进行第9步

9、跳转第2步

*/

 

// 0:可行走的点

// 1:阻挡点

// 2:路径

// 3:起点

// 4:终点

int
g_PathLattice[10][10] =

{

    { 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,3,0,1,0,4,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 },

};

 

struct
Node

{

    int
row; // 行

    int
rank; // 列

    int
f;

    int
g;

    int
h;

    Node * pParent; // 当前结点路径的前一个结点(父格子)

};

 

#define
LatticeLen 10 // 格子边长

 

// 函数前向声明

int
Distance(int
row1, int
rank1, int
row2, int
rank2);

bool
IsNodeInList(Node * pNode, list<Node *> list);

Node * GetNearestNode(list<Node *> list, Node * Rec);

void
GetNearNodeList(Node * pNode, list<Node *> & listNear,

    list<Node *> listStart, list<Node *> listEnd, Node * pEndNode);

void
EraseFromList(Node * pNode, list<Node *> & listStart);

void
ClearList(list<Node *> nodeList);

 

int
main()

{

    // 起点

    int
rowStart;

    int
rankStart;

 

    // 终点

    int
rowEnd;

    int
rankEnd;

 

    // 查找起点和终点的位置

    for (int
i = 0; i < 10; i++)

    {

        for (int
j = 0; j < 10; j++)

        {

            if (g_PathLattice[i][j] == 3)

            {

                rowStart = i;

                rankStart = j;

            }

            if (g_PathLattice[i][j] == 4)

            {

                rowEnd = i;

                rankEnd = j;

            }

        }

    }

 

    // 起点

    Node * nodeStart = new
Node;

    nodeStart->row = rowStart;

    nodeStart->rank = rankStart;

    nodeStart->g = 0;

    nodeStart->h = Distance(rowStart, rankStart, rowEnd, rankEnd);

    nodeStart->f = nodeStart->h;

    nodeStart->pParent = nullptr;

 

    // 终点

    Node * nodeEnd = new
Node;

    nodeEnd->row = rowEnd;

    nodeEnd->rank = rankEnd;

 

    // 定义启动列表和关闭列表

    list<Node *> listStart;

    list<Node *> listEnd;

 

    // 把起点加入启动列表

    listStart.push_back(nodeStart);

 

    // 当前结点

    Node * pNowNode = nullptr;

 

    // 如果终点在启动列表中,则已经找到路径,退出循环

    while (!IsNodeInList(nodeEnd, listStart))

    {

        Node * Rec = nullptr;

        // 查找权值最小的格子作为当前点

        pNowNode = GetNearestNode(listStart, Rec);

 

        // 如果没有找到,则说明没有路径

        if (pNowNode == nullptr)

        {

            break;

        }

 

        // 存放当前格子周围能加入启动列表的格子

        list<Node *> listNear;

        GetNearNodeList(pNowNode, listNear, listStart, listEnd, nodeEnd);

 

        // 将当前结点加入关闭列表中

        listEnd.push_back(pNowNode);

 

        // 将当前结点从启动列表中删除

        EraseFromList(pNowNode, listStart);

 

        // 将周围点加入启动列表中

        for (list<Node *>::iterator
it = listNear.begin();

            it
!=
listNear.end(); it++)

        {

            listStart.push_back(*it);

        }

    }

 

    if (pNowNode == nullptr)

    {

        printf("路径不存在\n");

        ClearList(listStart);

        ClearList(listEnd);

        delete
nodeEnd;

 

        return 0;

    }

 

    // 在启动列表中找到终点

    Node * pNodeFind = nullptr;

    for (list<Node *>::iterator
it = listStart.begin();

        it
!=
listStart.end(); it++)

    {

        if ((*it)->row == nodeEnd->row &&

            (*it)->rank == nodeEnd->rank)

        {

            pNodeFind = (*it);

            break;

        }

    }

 

    while (pNodeFind)

    {

        g_PathLattice[pNodeFind->row][pNodeFind->rank] = 2;

        pNodeFind = pNodeFind->pParent;

    }

 

    for (int
i = 0; i < 10; i++)

    {

        for (int
j = 0; j < 10; j++)

        {

            if (g_PathLattice[i][j] == 0)

            {

                printf("^ ");

            }

            else
if (g_PathLattice[i][j] == 1)

            {

                printf("* ");

            }

            else
if (g_PathLattice[i][j] == 2)

            {

                printf("# ");

            }

        }

        printf("\n");

    }

 

    ClearList(listStart);

    ClearList(listEnd);

 

    delete
nodeEnd;

 

    return 0;

}

 

int
Distance(int
row1, int
rank1, int
row2, int
rank2)

{

    // 格子的中点坐标

    int
x1 = rank1 * LatticeLen + LatticeLen / 2;

    int
y1 = row1 * LatticeLen + LatticeLen / 2;

    int
x2 = rank2 * LatticeLen + LatticeLen / 2;

    int
y2 = row2 * LatticeLen + LatticeLen / 2;

 

    return (int)sqrt((double)((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));

}

 

bool
IsNodeInList(Node * pNode, list<Node *> NodeList)

{

    for (list<Node *>::iterator
it = NodeList.begin();

        it
!=
NodeList.end(); it++)

    {

        if (pNode->row == (*it)->row && pNode->rank == (*it)->rank)

        {

            return
true;

        }

    }

 

    return
false;

}

 

Node * GetNearestNode(list<Node *> NodeList, Node * Rec)

{

    int
tempF = 1000000;

    for (list<Node *>::iterator
it = NodeList.begin();

        it
!=
NodeList.end(); it++)

    {

        if ((*it)->f < tempF)

        {

            Rec = *it;

            tempF = (*it)->f;

        }

    }

 

    return
Rec;

}

 

void
GetNearNodeList(Node * pNode, list<Node *> & listNear,

    list<Node *> listStart, list<Node *> listEnd, Node * pEndNode)

{

    // 将结点旁边的8个点加入到listNear中

    // 在启动或关闭列表中的点不能加入listNear

    // 阻挡点不能加入listNear

    for (int
i = -1; i <= 1; i++)

    {

        for (int
j = -1; j <= 1; j++)

        {

            if (i == 0 && j == 0)

            {

                // 自己格子

                continue;

            }

 

            int
rowTemp = pNode->row + i;

            int
rankTemp = pNode->rank + j;

 

            if (rowTemp < 0 || rankTemp < 0 || rowTemp > 9 || rankTemp > 9)

            {

                // 越界

                continue;

            }

 

            if (g_PathLattice[rowTemp][rankTemp] == 1)

            {

                // 阻挡点

                continue;

            }

 

            Node
node;

            node.row = rowTemp;

            node.rank = rankTemp;

            if (IsNodeInList(&node, listStart))

            {

                // 在启动列表中

                continue;

            }

            if (IsNodeInList(&node, listEnd))

            {

                // 在关闭列表中

                continue;

            }

 

            Node * pNearNode = new
Node;

            pNearNode->g = pNode->g + Distance(pNode->row, pNode->rank, rowTemp, rankTemp);

            pNearNode->h = Distance(rowTemp, rankTemp, pEndNode->row, pEndNode->rank);

            pNearNode->f = pNearNode->g + pNearNode->h;

            pNearNode->row = rowTemp;

            pNearNode->rank = rankTemp;

            pNearNode->pParent = pNode;

            listNear.push_back(pNearNode);

        }

    }

}

 

void
EraseFromList(Node * pNode, list<Node *> & listStart)

{

    for (list<Node *>::iterator
it = listStart.begin();

        it
!=
listStart.end(); it++)

    {

        if (pNode->row == (*it)->row && pNode->rank == (*it)->rank)

        {

            listStart.erase(it);

            return;

        }

    }

}

 

void
ClearList(list<Node *> nodeList)

{

    for (list<Node *>::iterator
it = nodeList.begin();

        it
!=
nodeList.end(); it++)

    {

        delete
*it;

    }

}

 

 

 

 

一个小笔记(5):A*算法的更多相关文章

  1. 一个小笔记(2):Socket网络编程

    网络通信的流程: 服务器端申请套接字 -> 绑定套接字到本地,打开端口 -> 监听端口 -> 等待接受消息 -> 有消息之后,读取消息 客户端申请套接字 -> 向服务端发 ...

  2. 学习完nio的一个小笔记吧

    这是一个nio网络通信服务端的demo,主要就学习了selector的一些用法,以及它里面的事件类型 selector是对nio的一个优化,它能保证既能高效处理线程中的事件,又能保证线程不会一直占用c ...

  3. 一个小笔记(8):EN_2

    Why is programming fun? What delights may its practitioner expect as his reward? First is the sheer ...

  4. 一个小笔记(7):EN_1

    For nearly ten years, the Unified Modeling Language(UML) has been the industry standard for visualiz ...

  5. JavaScript关于返回数据类型一个小小的笔记

    Javascript关于返回数据类型的一个小笔记 javascript数据类型有两种. 一种是基本数据类型:String.Number.Boolean.Symbol.Underfine.Null 一种 ...

  6. c++学习笔记---04---从另一个小程序接着说

    从另一个小程序接着说 文件I/O 前边我们已经给大家简单介绍和演示过C和C++在终端I/O处理上的异同点. 现在我们接着来研究文件I/O. 编程任务:编写一个文件复制程序,功能实现将一个文件复制到另一 ...

  7. c++学习笔记---03---从一个小程序说起2

    从一个小程序说起2 要求:编写一个程序,要求用户输入一串整数和任意数目的空格,这些整数必须位于同一行中,但允许出现在该行中的任何位置.当用户按下键盘上的"Enter"键时,数据输入 ...

  8. c++学习笔记---02---从一个小程序说起

    从一个小程序说起 这一讲的主要目的是帮助大家在C语言的背景知识上与C++建立联系. 问题探索 问题:对一个整型数组求和. 要求:定义一个存储着 n 个元素的数组,要求用C语言完成这个任务. 赶紧的:大 ...

  9. python笔记_查看函数调用栈的一个小技巧

    一.背景 最近在看一个开源框架的源码,涉及到的内容非常杂乱,有的函数不知道是在什么时候被谁给调用了?调用的时候传入了什么参数?为了解决这个问题,写了一个小的装饰器. 二.实现 这个装饰器函数主要参考了 ...

随机推荐

  1. [Xcode 实际操作]八、网络与多线程-(24)社会化分享功能开发包的安装和配置:微信、QQ、微博

    目录:[Swift]Xcode实际操作 本文将演示如何在开放平台注册应用,并获得相关的密钥信息,用于实现后面文章的微博分享功能. 一.新浪微博开放平台 [登录]->[微服务]->[粉丝服务 ...

  2. 线程安全 原子性 可见性 顺序性 volatile

  3. flask_之URL

    URL篇 在分析路由匹配过程之前,我们先来看看 flask 中,构建这个路由规则的两种方法: 通过 @app.route() decorator 通过 app.add_url_rule,这个方法的签名 ...

  4. [洛谷3935]Calculating

    题目链接:https://www.luogu.org/problemnew/show/P3935 首先显然有\(\sum\limits_{i=l}^rf(i)=\sum\limits_{i=1}^rf ...

  5. HDU6299(2018多校第一场)

    Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6299 两个字符串的排序可以分成四种情况: (1)str1左少右多 vs str2 左多右 ...

  6. 12.Maps

    1       Maps 1.1  Map声明和访问 maps中的元素是key-value对儿,key与value之间使用冒号(:)分割.创建一个空value的map,使用[:].默认情况下,map的 ...

  7. asp.net core分块上传文件

    写完asp.net多文件上传(http://www.cnblogs.com/bestckk/p/5987383.html)后,感觉这种上传还是有很多缺陷,于是...(省略一万字,不废话).这里我没用传 ...

  8. 547 Friend Circles 朋友圈

    班上有 N 名学生.其中有些人是朋友,有些则不是.他们的友谊具有是传递性.如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友.所谓的朋友圈,是指所有朋友的集合.给 ...

  9. 083 Remove Duplicates from Sorted List 有序链表中删除重复的结点

    给定一个排序链表,删除所有重复的元素使得每个元素只留下一个.案例:给定 1->1->2,返回 1->2给定 1->1->2->3->3,返回 1->2- ...

  10. Github开源项目单

    以下涉及到的数据统计与 2019 年 5 月 1 日 12 点,数据来源:https://github.com/trending/java?since=monthly . 下面的内容从 Java 学习 ...