Giter VIP home page Giter VIP logo

maze_astar's Introduction

求解迷宫路径

寻路问题,输入一个方格表示的地图,要求用A*算法找到并输出从起点(再放各种标示字母S)到终点(在方格中标示字母T)的代价最小的路径,有如下条件及要求:
1.每一步都落在方格中,而不是横竖线的交叉点
2.灰色格子表示障碍,无法通行
3.在每个格子处,若无障碍,下一步可以达到八个相邻的格子,并且可以到达无障碍的相邻格子。其中,向上、下、左、右四个方向移动的代价为1,向四个斜角方向移动的代价为√2
4.在一些特殊格子上行走要花费额外的地形代价,比如黄色格子代表沙漠,经过它的代价为4;蓝色格子代表溪流,经过它的代价为2;白色格子为普通地形,经过它的代价为0
5.经过一条路径的总代价为移动代价+地形代价。其中移动代价是路径上所做的所有移动的代价的总和;地形代价为路径上除起点外所有格子的地形代价的总和。
详细过程可参考pdf文档或代码。

单向A*算法

算法A* single(D, S, T)
输入 :平面上n个点的二维数组D,起始点S,终点T
输出 :从S到T的路径
1: openlist.push(S)
2: While(true)
3: 寻找openlist中F值最低的格,称为当前格P
4: closelist.push(P)
5: If 它不可通过||已经在closelist中 Then ;
6: If 它不在openlsit中
7: Then openlist.push(P),把P作为该格的父节点,计算FGH值
8: If 它已在openlist中
9: Then If 它的F值更低
10: Then 把P作为该格的父节点,重新计算FGH值
11: If 目标格已经在openlist中
12: Then break
13: 从T开始,沿着每一格的父节点回溯,直到回到S,输出路径

双向A*算法

双向A*算法无法保证最优解

算法A* bidirection(D, S, T)
输入 :平面上n个点的二维数组D,起始点S,终点T
输出 :从S到T的路径
1: openlist1.push(S)
2: openlist2.push(T)
3: While(true)
4: 寻找openlist1中F值最低的格,称为当前格P1
5: 寻找openlist2中F值最低的格,称为当前格P2
6: If P1不可通过||已经在closelist1中 Then ;
7: If 它已在openlist1中
8: Then If 它的F值更低
9: Then 把P1作为该格的父节点,重新计算FGH值
10: If P2不可通过||已经在closelist2中 Then ;
11: If 它已在openlist2中
12: Then If 它的F值更低
13: Then 把P2作为该格的父节点,重新计算FGH值
14: If openlist1和openlist2中存在相同点
15: Then break
16: 从S和T分别开始,沿着每一格的父节点回溯,直到回到相遇点,输出路径

结果展示

初始地图

单向A*结果

双向A*结果

初始地图2

单向A*结果

双向A*结果

maze_astar's People

Contributors

huiyanwen avatar

Stargazers

qinwentu avatar  avatar ZHIXIN avatar jack avatar  avatar Zeno avatar  avatar pipi_xia avatar lenovotcldellhp avatar  avatar  avatar Lyndon-N avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.