{"id":2981,"date":"2024-02-14T18:01:02","date_gmt":"2024-02-14T10:01:02","guid":{"rendered":""},"modified":"2024-02-14T18:01:02","modified_gmt":"2024-02-14T10:01:02","slug":"A*\u7b97\u6cd5\u4ecb\u7ecd","status":"publish","type":"post","link":"https:\/\/mushiming.com\/2981.html","title":{"rendered":"A*\u7b97\u6cd5\u4ecb\u7ecd"},"content":{"rendered":"
\u4e00\u3001\u9002\u7528\u573a\u666f<\/p>\n
\u5728\u4e00\u5f20\u5730\u56fe\u4e2d\uff0c\u7ed8\u5236\u4ece\u8d77\u70b9\u79fb\u52a8\u5230\u7ec8\u70b9\u7684\u6700\u4f18\u8def\u5f84\uff0c\u5730\u56fe\u4e2d\u4f1a\u6709\u969c\u788d\u7269\uff0c\u5fc5\u987b\u7ed5\u5f00\u969c\u788d\u7269\u3002<\/p>\n
\u4e8c\u3001\u7b97\u6cd5\u601d\u8def<\/p>\n
1. \u56de\u6eaf\u6cd5\u5f97\u5230\u8def\u5f84<\/p>\n
(\u5982\u679c\u6709\u8def\u5f84)\u91c7\u7528\u201c\u7ed3\u70b9\u4e0e\u7ed3\u70b9\u7684\u7236\u8282\u70b9\u201d\u7684\u5173\u7cfb\u4ece\u6700\u7ec8\u7ed3\u70b9\u56de\u6eaf\u5230\u8d77\u70b9\uff0c\u5f97\u5230\u8def\u5f84\u3002<\/p>\n
2. \u8def\u5f84\u4ee3\u4ef7\u7684\u4f30\u7b97\uff1aF = G+H<\/p>\n
A\u661f\u7b97\u6cd5\u7684\u4ee3\u4ef7\u8ba1\u7b97\u4f7f\u7528\u4e86\u88ab\u79f0\u4f5c\u662f\u542f\u53d1\u5f0f\u7684\u4ee3\u4ef7\u51fd\u6570\u3002<\/p>\n
\u5148\u8bf4\u660e\u4e00\u4e0b\u5404\u7b26\u53f7\u610f\u4e49\uff1aG\u8868\u793a\u7684\u662f\u4ece\u8d77\u70b9\u5230\u5f53\u524d\u7ed3\u70b9\u7684\u5b9e\u9645\u8def\u5f84\u4ee3\u4ef7(\u4e3a\u5565\u53eb\u5b9e\u9645\uff1f\u5c31\u662f\u5df2\u7ecf\u8d70\u8fc7\u4e86\uff0c\u8fb9\u8d70\u8fb9\u5c06\u4ee3\u4ef7\u8ba1\u7b97\u597d\u4e86)\uff1bH\u8868\u793a\u5f53\u524d\u7ed3\u70b9\u5230\u8fbe\u6700\u7ec8\u7ed3\u70b9\u7684\u4f30\u8ba1\u4ee3\u4ef7(\u4e3a\u5565\u53eb\u4f30\u8ba1\uff1f\u5c31\u662f\u8fd8\u6ca1\u8d70\u8fc7\uff0c\u4e0d\u77e5\u9053\u524d\u9762\u6709\u6ca1\u969c\u788d\u3001\u8def\u901a\u4e0d\u901a\uff0c\u6240\u4ee5\u53ea\u80fd\u7528\u4f30\u8ba1)\uff1bF\u8868\u793a\u5f53\u524d\u7ed3\u70b9\u6240\u5728\u8def\u5f84\u4ece\u8d77\u70b9\u5230\u6700\u7ec8\u70b9\u9884\u4f30\u7684\u603b\u8def\u5f84\u4ee3\u4ef7\u3002<\/p>\n
G\u7684\u8ba1\u7b97\u65b9\u5f0f\uff1a\u8ba1\u7b97\u65b9\u5f0f\u6709\u633a\u591a\u79cd\u7684\uff0c\u8fd9\u91cc\u6211\u4eec\u5c31\u7528\u8fd9\u79cd\u5427\uff0c\u5047\u8bbe\u6bcf\u4e2a\u7ed3\u70b9\u4ee3\u8868\u4e00\u4e2a\u6b63\u65b9\u5f62\uff0c\u6a2a\u7ad6\u79fb\u52a8\u8ddd\u79bb\uff1a\u659c\u79fb\u52a8\u8ddd\u79bb=1\uff1a1.4\uff08\u6839\u53f72\uff09\uff0c\u6211\u4eec\u53d6\u4e2a\u6574\u657010\u548c14\u5427\uff0c\u4e5f\u5c31\u662f\u8bf4\u5f53\u524d\u7ed3\u70b9G\u503c=\u7236\u8282\u70b9\u7684G+\uff0810\u621614\uff09\u3002<\/p>\n
H\u7684\u8ba1\u7b97\u65b9\u5f0f\uff1a\u4f30\u4ef7\u8ba1\u7b97\u4e5f\u6709\u5f88\u591a\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u8fd9\u91cc\u4f7f\u7528\u201c\u66fc\u54c8\u987f\u201d\u6cd5\uff0cH=|\u5f53\u524d\u7ed3\u70b9x\u503c-\u6700\u7ec8\u7ed3\u70b9x\u503c|+|\u5f53\u524d\u7ed3\u70b9y\u503c-\u6700\u7ec8\u7ed3\u70b9y\u503c|\uff08\"||\"\u8868\u793a\u7edd\u5bf9\u503c\uff09\u3002<\/p>\n
\u5982\u4e0b\u56fe\uff08\u56fe\u4e0d\u662f\u81ea\u5df1\u505a\u7684\uff0c\u4ece\u7f51\u4e0a\u501f\u6765\u7684\uff0c\u81ea\u5df1\u753b\u7684\u8bdd~\u2026\u60e8\u4e0d\u5fcd\u7779\uff01\uff09<\/p>\n
3. \u8f85\u52a9\u8868\uff1aOpen\u3001Close\u5217\u8868<\/p>\n
\u5728A\u661f\u7b97\u6cd5\u4e2d\uff0c\u9700\u8981\u4f7f\u7528\u4e24\u4e2a\u8f85\u52a9\u8868\u6765\u8bb0\u5f55\u7ed3\u70b9\u3002<\/p>\n
\u4e00\u4e2a\u7528\u4e8e\u8bb0\u5f55\u53ef\u88ab\u8bbf\u95ee\u7684\u7ed3\u70b9\uff0c\u6210\u4e3aOpen\u8868\uff1b\u4e00\u4e2a\u662f\u8bb0\u5f55\u5df2\u8bbf\u95ee\u8fc7\u7684\u7ed3\u70b9\uff0c\u79f0\u4e3aClose\u8868\u3002<\/p>\n
\u8fd9\u4e24\u4e2a\u8868\u51b3\u5b9a\u4e86\u7b97\u6cd5\u7684\u7ed3\u675f\uff1a\u6761\u4ef6\u662f\u6700\u7ec8\u7ed3\u70b9\u5728Close\u8868\u4e2d(\u627e\u5230\u8def\u5f84)\u6216Open\u8868\u4e3a\u7a7a(\u627e\u4e0d\u5230\u4e86\u8def\u5f84)\u3002<\/p>\n
4. \u79fb\u52a8\u7ed3\u70b9\u3001\u76f8\u90bb\u7ed3\u70b9\u7684\u5904\u7406<\/p>\n
\u4e0a\u9762\u7684\u7406\u89e3\u7684\u8bdd\uff0c\u73b0\u5728\u5c31\u6765\u79fb\u52a8\u5f53\u524d\u7684\u8282\u70b9\uff0c\u5bfb\u627e\u8def\u5f84\u3002<\/p>\n
\u6bcf\u6b21\u4eceOpen\u8868\u4e2d\u53d6\u51faF\u503c\u6700\u5c0f\u7684\u7ed3\u70b9\u51fa\u6765\uff08\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4f18\u5148\u961f\u5217\u6765\u5904\u7406\u6bd4\u8f83\u597d\uff09\uff0c\u4f5c\u4e3a\u5f53\u524d\u7ed3\u70b9\uff1b\u7136\u540e\u5c06\u5f53\u524d\u7ed3\u70b9\u7684\u6240\u6709\u90bb\u7ed3\u70b9\u6309\u7167\u90bb\u7ed3\u70b9\u89c4\u5219\u52a0\u5165\u5230Open\u8868\u4e2d\uff1b\u6700\u540e\u5c06\u5f53\u524d\u7ed3\u70b9\u653e\u5165Close\u8868\u4e2d\uff0c\u8fd9\u91cc\u5c31\u662f\u6bcf\u6b21\u5faa\u73af\u7684\u6267\u884c\u5185\u5bb9\u3002<\/p>\n
\u90bb\u7ed3\u70b9\u89c4\u5219\uff1a<\/p>\n
(1) \u5f53\u90bb\u7ed3\u70b9\u4e0d\u5728\u5730\u56fe\u4e2d\uff0c\u4e0d\u52a0\u5165Open\u8868\uff1b<\/p>\n
(2) \u5f53\u90bb\u7ed3\u70b9\u662f\u969c\u788d\u7269\uff0c\u4e0d\u52a0\u5165Open\u8868\uff1b<\/p>\n
(3) \u5f53\u90bb\u7ed3\u70b9\u5728Close\u8868\u4e2d\uff0c\u4e0d\u52a0\u5165Open\u8868\uff1b<\/p>\n
(4) \u5f53\u90bb\u7ed3\u70b9\u4e0d\u5728Open\u4e2d\uff0c\u52a0\u5165Open\u8868\uff0c\u8bbe\u8be5\u90bb\u7ed3\u70b9\u7684\u7236\u8282\u70b9\u4e3a\u5f53\u524d\u7ed3\u70b9\uff1b<\/p>\n
(5) **\u5f53\u90bb\u7ed3\u70b9\u5728Open\u8868\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u505a\u4e2a\u6bd4\u8f83:\u5982\u679c\u90bb\u7ed3\u70b9\u7684G\u503c>\u5f53\u524d\u7ed3\u70b9\u7684G\u503c+\u5f53\u524d\u7ed3\u70b9\u5230\u8fd9\u4e2a\u90bb\u7ed3\u70b9\u7684\u4ee3\u4ef7\uff0c\u90a3\u4e48\u4fee\u6539\u8be5\u90bb\u7ed3\u70b9\u7684\u7236\u8282\u70b9\u4e3a\u5f53\u524d\u7684\u7ed3\u70b9(\u56e0\u4e3a\u5728Open\u8868\u4e2d\u7684\u7ed3\u70b9\u9664\u4e86\u8d77\u70b9\uff0c\u90fd\u4f1a\u6709\u7236\u8282\u70b9)\uff0c\u4fee\u6539G\u503c=\u5f53\u524d\u7ed3\u70b9\u7684G\u503c+\u5f53\u524d\u7ed3\u70b9\u5230\u8fd9\u4e2a\u90bb\u7ed3\u70b9\u7684\u4ee3\u4ef7 **<\/p>\n
\u84dd\u8272\u6846\u6846\u8868\u793a\u5728Close\u8868\u4e2d\uff0c\u7eff\u8272\u7684\u6846\u6846\u8868\u793a\u5728Open\u8868\u4e2d<\/p>\n
\u6700\u540e\u56de\u6eaf\u5f97\u5230\u8def\u5f84<\/p>\n
\u4e09\u3001\u4ee3\u7801\u5b9e\u73b0(Java)<\/p>\n
1. \u8f93\u5165<\/p>\n
(1) \u4ee3\u8868\u5730\u56fe\u4e8c\u503c\u4e8c\u7ef4\u6570\u7ec4(0\u8868\u793a\u53ef\u901a\u8def\uff0c1\u8868\u793a\u8def\u969c)<\/p>\n
int[][] maps = {
\n <\/p>\n
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },<\/p>\n
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },<\/p>\n
{ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 },<\/p>\n
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },<\/p>\n
{ 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },<\/p>\n
{ 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },<\/p>\n
{ 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }<\/p>\n
};<\/p>\n
(2) \u6309\u7167\u4e8c\u7ef4\u6570\u7ec4\u7684\u7279\u70b9\uff0c\u5750\u6807\u539f\u70b9\u5728\u5de6\u4e0a\u89d2\uff0c\u6240\u4ee5y\u662f\u9ad8\uff0cx\u662f\u5bbd\uff0cy\u5411\u4e0b\u9012\u589e\uff0cx\u5411\u53f3\u9012\u589e\uff0c\u6211\u4eec\u5c06x\u548cy\u5c01\u88c5\u6210\u4e00\u4e2a\u7c7b\uff0c\u597d\u4f20\u53c2\uff0c\u91cd\u5199equals\u65b9\u6cd5\u6bd4\u8f83\u5750\u6807(x,y)\u662f\u4e0d\u662f\u540c\u4e00\u4e2a\u3002<\/p>\n
public class Coord<\/p>\n
{
\n <\/p>\n
public int x;<\/p>\n
public int y;<\/p>\n
public Coord(int x, int y)<\/p>\n
{
\n <\/p>\n
this.x = x;<\/p>\n
this.y = y;<\/p>\n
}<\/p>\n
@Override<\/p>\n
public boolean equals(Object obj)<\/p>\n
{
\n <\/p>\n
if (obj == null) return false;<\/p>\n
if (obj instanceof Coord)<\/p>\n
{
\n <\/p>\n
Coord c = (Coord) obj;<\/p>\n
return x == c.x && y == c.y;<\/p>\n
}<\/p>\n
return false;<\/p>\n
}<\/p>\n
}<\/p>\n
(3) \u5c01\u88c5\u8def\u5f84\u7ed3\u70b9\u7c7b\uff0c\u5b57\u6bb5\u5305\u62ec\uff1a\u5750\u6807\u3001G\u503c\u3001F\u503c\u3001\u7236\u7ed3\u70b9\uff0c\u5b9e\u73b0Comparable\u63a5\u53e3\uff0c\u65b9\u4fbf\u4f18\u5148\u961f\u5217\u6392\u5e8f\u3002<\/p>\n
public class Node implements Comparable<Node><\/p>\n
{
\n <\/p>\n
public Coord coord; \/\/ \u5750\u6807<\/p>\n
public Node parent; \/\/ \u7236\u7ed3\u70b9<\/p>\n
public int G; \/\/ G\uff1a\u662f\u4e2a\u51c6\u786e\u7684\u503c\uff0c\u662f\u8d77\u70b9\u5230\u5f53\u524d\u7ed3\u70b9\u7684\u4ee3\u4ef7<\/p>\n
public int H; \/\/ H\uff1a\u662f\u4e2a\u4f30\u503c\uff0c\u5f53\u524d\u7ed3\u70b9\u5230\u76ee\u7684\u7ed3\u70b9\u7684\u4f30\u8ba1\u4ee3\u4ef7<\/p>\n
public Node(int x, int y)<\/p>\n
{
\n <\/p>\n
this.coord = new Coord(x, y);<\/p>\n
}<\/p>\n
public Node(Coord coord, Node parent, int g, int h)<\/p>\n
{
\n <\/p>\n
this.coord = coord;<\/p>\n
this.parent = parent;<\/p>\n
G = g;<\/p>\n
H = h;<\/p>\n
}<\/p>\n
@Override<\/p>\n
public int compareTo(Node o)<\/p>\n
{
\n <\/p>\n
if (o == null) return -1;<\/p>\n
if (G + H > o.G + o.H)<\/p>\n
return 1;<\/p>\n
else if (G + H < o.G + o.H) return -1;<\/p>\n
return 0;<\/p>\n
}<\/p>\n
}<\/p>\n
(4) \u6700\u540e\u4e00\u4e2a\u6570\u636e\u7ed3\u6784\u662fA\u661f\u7b97\u6cd5\u8f93\u5165\u7684\u6240\u6709\u6570\u636e\uff0c\u5c01\u88c5\u5728\u4e00\u8d77\uff0c\u4f20\u53c2\u65b9\u4fbf\u3002\ud83d\ude01<\/p>\n
public class MapInfo<\/p>\n
{
\n <\/p>\n
public int[][] maps; \/\/ \u4e8c\u7ef4\u6570\u7ec4\u7684\u5730\u56fe<\/p>\n
public int width; \/\/ \u5730\u56fe\u7684\u5bbd<\/p>\n
public int hight; \/\/ \u5730\u56fe\u7684\u9ad8<\/p>\n
public Node start; \/\/ \u8d77\u59cb\u7ed3\u70b9<\/p>\n
public Node end; \/\/ \u6700\u7ec8\u7ed3\u70b9<\/p>\n
public MapInfo(int[][] maps, int width, int hight, Node start, Node end)<\/p>\n
{
\n <\/p>\n
this.maps = maps;<\/p>\n
this.width = width;<\/p>\n
this.hight = hight;<\/p>\n
this.start = start;<\/p>\n
this.end = end;<\/p>\n
}<\/p>\n
}<\/p>\n
2. \u5904\u7406<\/p>\n
(1) \u5728\u7b97\u6cd5\u91cc\u9700\u8981\u5b9a\u4e49\u51e0\u4e2a\u5e38\u91cf\u6765\u786e\u5b9a\uff1a\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u54ea\u4e2a\u503c\u8868\u793a\u969c\u788d\u7269\u3001\u4e8c\u7ef4\u6570\u7ec4\u4e2d\u7ed8\u5236\u8def\u5f84\u7684\u4ee3\u8868\u503c\u3001\u8ba1\u7b97G\u503c\u9700\u8981\u7684\u6a2a\u7eb5\u79fb\u52a8\u4ee3\u4ef7\u548c\u659c\u79fb\u52a8\u4ee3\u4ef7\u3002<\/p>\n
public final static int BAR = 1; \/\/ \u969c\u788d\u503c<\/p>\n
public final static int PATH = 2; \/\/ \u8def\u5f84<\/p>\n
public final static int DIRECT_VALUE = 10; \/\/ \u6a2a\u7ad6\u79fb\u52a8\u4ee3\u4ef7<\/p>\n
public final static int OBLIQUE_VALUE = 14; \/\/ \u659c\u79fb\u52a8\u4ee3\u4ef7<\/p>\n
(2) \u5b9a\u4e49\u4e24\u4e2a\u8f85\u52a9\u8868\uff1aOpen\u8868\u548cClose\u8868\u3002Open\u8868\u7684\u4f7f\u7528\u662f\u9700\u8981\u53d6\u6700\u5c0f\u503c\uff0c\u5728\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528Java\u5de5\u5177\u5305\u4e2d\u7684\u4f18\u5148\u961f\u5217PriorityQueue\uff0cClose\u53ea\u662f\u7528\u6765\u4fdd\u5b58\u7ed3\u70b9\uff0c\u6ca1\u5176\u4ed6\u7279\u6b8a\u7528\u9014\uff0c\u5c31\u7528ArrayList\u3002<\/p>\n
Queue<Node> openList = new PriorityQueue<Node>(); \/\/ \u4f18\u5148\u961f\u5217(\u5347\u5e8f)<\/p>\n
List<Node> closeList = new ArrayList<Node>();<\/p>\n
1<\/p>\n
2<\/p>\n
(3) \u5b9a\u4e49\u51e0\u4e2a\u5e03\u5c14\u5224\u65ad\u65b9\u6cd5\uff1a\u6700\u7ec8\u7ed3\u70b9\u7684\u5224\u65ad\u3001\u7ed3\u70b9\u80fd\u5426\u52a0\u5165open\u8868\u7684\u5224\u65ad\u3001\u7ed3\u70b9\u662f\u5426\u5728Close\u8868\u4e2d\u7684\u5224\u65ad\u3002<\/p>\n
\/**<\/p>\n
* \u5224\u65ad\u7ed3\u70b9\u662f\u5426\u662f\u6700\u7ec8\u7ed3\u70b9<\/p>\n
*\/<\/p>\n
private boolean isEndNode(Coord end,Coord coord)<\/p>\n
{
\n <\/p>\n
return coord != null && end.equals(coord);<\/p>\n
}<\/p>\n
\/**<\/p>\n
* \u5224\u65ad\u7ed3\u70b9\u80fd\u5426\u653e\u5165Open\u5217\u8868<\/p>\n
*\/<\/p>\n
private boolean canAddNodeToOpen(MapInfo mapInfo,int x, int y)<\/p>\n
{
\n <\/p>\n
\/\/ \u662f\u5426\u5728\u5730\u56fe\u4e2d<\/p>\n
if (x < 0 || x >= mapInfo.width || y < 0 || y >= mapInfo.hight) return false;<\/p>\n
\/\/ \u5224\u65ad\u662f\u5426\u662f\u4e0d\u53ef\u901a\u8fc7\u7684\u7ed3\u70b9<\/p>\n
if (mapInfo.maps[y][x] == BAR) return false;<\/p>\n
\/\/ \u5224\u65ad\u7ed3\u70b9\u662f\u5426\u5b58\u5728close\u8868<\/p>\n
if (isCoordInClose(x, y)) return false;<\/p>\n
return true;<\/p>\n
}<\/p>\n
\/**<\/p>\n
* \u5224\u65ad\u5750\u6807\u662f\u5426\u5728close\u8868\u4e2d<\/p>\n
*\/<\/p>\n
private boolean isCoordInClose(Coord coord)<\/p>\n
{
\n <\/p>\n
return coord!=null&&isCoordInClose(coord.x, coord.y);<\/p>\n
}<\/p>\n
\/**<\/p>\n
* \u5224\u65ad\u5750\u6807\u662f\u5426\u5728close\u8868\u4e2d<\/p>\n
*\/<\/p>\n
private boolean isCoordInClose(int x, int y)<\/p>\n
{
\n <\/p>\n
if (closeList.isEmpty()) return false;<\/p>\n
for (Node node : closeList)<\/p>\n
{
\n <\/p>\n
if (node.coord.x == x && node.coord.y == y)<\/p>\n
{
\n <\/p>\n
return true;<\/p>\n
}<\/p>\n
}<\/p>\n
return false;<\/p>\n
}<\/p>\n
(4) \u8ba1\u7b97H\u503c\uff0c\u201c\u66fc\u54c8\u987f\u201d \u6cd5\uff0c\u5750\u6807\u5206\u522b\u53d6\u5dee\u503c\u76f8\u52a0<\/p>\n
private int calcH(Coord end,Coord coord)<\/p>\n
{
\n <\/p>\n
return Math.abs(end.x - coord.x) + Math.abs(end.y - coord.y);<\/p>\n
}<\/p>\n
(5) \u4eceOpen\u5217\u8868\u4e2d\u67e5\u627e\u7ed3\u70b9<\/p>\n
private Node findNodeInOpen(Coord coord)<\/p>\n
{
\n <\/p>\n
if (coord == null || openList.isEmpty()) return null;<\/p>\n
for (Node node : openList)<\/p>\n
{
\n <\/p>\n
if (node.coord.equals(coord))<\/p>\n
{
\n <\/p>\n
return node;<\/p>\n
}<\/p>\n
}<\/p>\n
return null;<\/p>\n
}<\/p>\n
(6) \u6dfb\u52a0\u90bb\u7ed3\u70b9\u5230Open\u8868<\/p>\n
\/**<\/p>\n
* \u6dfb\u52a0\u6240\u6709\u90bb\u7ed3\u70b9\u5230open\u8868<\/p>\n
*\/<\/p>\n
private void addNeighborNodeInOpen(MapInfo mapInfo,Node current)<\/p>\n
{
\n <\/p>\n
int x = current.coord.x;<\/p>\n
int y = current.coord.y;<\/p>\n
\/\/ \u5de6<\/p>\n
addNeighborNodeInOpen(mapInfo,current, x - 1, y, DIRECT_VALUE);<\/p>\n
\/\/ \u4e0a<\/p>\n
addNeighborNodeInOpen(mapInfo,current, x, y - 1, DIRECT_VALUE);<\/p>\n
\/\/ \u53f3<\/p>\n
addNeighborNodeInOpen(mapInfo,current, x + 1, y, DIRECT_VALUE);<\/p>\n
\/\/ \u4e0b<\/p>\n
addNeighborNodeInOpen(mapInfo,current, x, y + 1, DIRECT_VALUE);<\/p>\n
\/\/ \u5de6\u4e0a<\/p>\n
addNeighborNodeInOpen(mapInfo,current, x - 1, y - 1, OBLIQUE_VALUE);<\/p>\n
\/\/ \u53f3\u4e0a<\/p>\n
addNeighborNodeInOpen(mapInfo,current, x + 1, y - 1, OBLIQUE_VALUE);<\/p>\n
\/\/ \u53f3\u4e0b<\/p>\n
addNeighborNodeInOpen(mapInfo,current, x + 1, y + 1, OBLIQUE_VALUE);<\/p>\n
\/\/ \u5de6\u4e0b<\/p>\n
addNeighborNodeInOpen(mapInfo,current, x - 1, y + 1, OBLIQUE_VALUE);<\/p>\n
}<\/p>\n
\/**<\/p>\n
* \u6dfb\u52a0\u4e00\u4e2a\u90bb\u7ed3\u70b9\u5230open\u8868<\/p>\n
*\/<\/p>\n
private void addNeighborNodeInOpen(MapInfo mapInfo,Node current, int x, int y, int value)<\/p>\n
{
\n <\/p>\n
if (canAddNodeToOpen(mapInfo,x, y))<\/p>\n
{
\n <\/p>\n
Node end=mapInfo.end;<\/p>\n
Coord coord = new Coord(x, y);<\/p>\n
int G = current.G + value; \/\/ \u8ba1\u7b97\u90bb\u7ed3\u70b9\u7684G\u503c<\/p>\n
Node child = findNodeInOpen(coord);<\/p>\n
if (child == null)<\/p>\n
{
\n <\/p>\n
int H=calcH(end.coord,coord); \/\/ \u8ba1\u7b97H\u503c<\/p>\n
if(isEndNode(end.coord,coord))<\/p>\n
{
\n <\/p>\n
child=end;<\/p>\n
child.parent=current;<\/p>\n
child.G=G;<\/p>\n
child.H=H;<\/p>\n
}<\/p>\n
else<\/p>\n
{
\n <\/p>\n
child = new Node(coord, current, G, H);<\/p>\n
}<\/p>\n
openList.add(child);<\/p>\n
}<\/p>\n
else if (child.G > G)<\/p>\n
{
\n <\/p>\n
child.G = G;<\/p>\n
child.parent = current;<\/p>\n
\/\/ \u91cd\u65b0\u8c03\u6574\u5806<\/p>\n
openList.add(child);<\/p>\n
}<\/p>\n
}<\/p>\n
}<\/p>\n
(7) \u56de\u6eaf\u6cd5\u7ed8\u5236\u8def\u5f84<\/p>\n
private void drawPath(int[][] maps, Node end)<\/p>\n
{
\n <\/p>\n
if(end==null||maps==null) return;<\/p>\n
System.out.println(\"\u603b\u4ee3\u4ef7\uff1a\" + end.G);<\/p>\n
while (end != null)<\/p>\n
{
\n <\/p>\n
Coord c = end.coord;<\/p>\n
maps[c.y][c.x] = PATH;<\/p>\n
end = end.parent;<\/p>\n
}<\/p>\n
}<\/p>\n
(8) \u5f00\u59cb\u7b97\u6cd5\uff0c\u5faa\u73af\u79fb\u52a8\u7ed3\u70b9\u5bfb\u627e\u8def\u5f84\uff0c\u8bbe\u5b9a\u5faa\u73af\u7ed3\u675f\u6761\u4ef6\uff0cOpen\u8868\u4e3a\u7a7a\u6216\u8005\u6700\u7ec8\u7ed3\u70b9\u5728Close\u8868<\/p>\n
public void start(MapInfo mapInfo)<\/p>\n
{
\n <\/p>\n
if(mapInfo==null) return;<\/p>\n
\/\/ clean<\/p>\n
openList.clear();<\/p>\n
closeList.clear();<\/p>\n
\/\/ \u5f00\u59cb\u641c\u7d22<\/p>\n
openList.add(mapInfo.start);<\/p>\n
moveNodes(mapInfo);<\/p>\n
}<\/p>\n
\/**<\/p>\n
* \u79fb\u52a8\u5f53\u524d\u7ed3\u70b9<\/p>\n
*\/<\/p>\n
private void moveNodes(MapInfo mapInfo)<\/p>\n
{
\n <\/p>\n
while (!openList.isEmpty())<\/p>\n
{
\n <\/p>\n
Node current = openList.poll();<\/p>\n
closeList.add(current);<\/p>\n
addNeighborNodeInOpen(mapInfo,current);<\/p>\n
if (isCoordInClose(mapInfo.end.coord)) \/\/ bug\u4fee\u6b63<\/p>\n
{
\n <\/p>\n
drawPath(mapInfo.maps, mapInfo.end);<\/p>\n
break;<\/p>\n
}<\/p>\n
}<\/p>\n
}<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"A*\u7b97\u6cd5\u4ecb\u7ecdA*\u7b97\u6cd5_a*\u7b97\u6cd5open\u8868\u548cclose\u8868","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/2981"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=2981"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/2981\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=2981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=2981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=2981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}