日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

a 寻路算法 java_A*(也叫A star, A星)寻路算法Java版 | 学步园

發布時間:2023/12/4 java 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 a 寻路算法 java_A*(也叫A star, A星)寻路算法Java版 | 学步园 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

尋路

首先要理解什么是A*尋路算法,可以參考這三篇文章:

下面為測試地圖,0表示可以通行,1表示障礙物:

要從點(5, 1)到點(5, 5),通過A*尋路算法找到以路徑為@所示:

在代碼中可以修改障礙物,起點和終點來測試算法。

最后代碼:

import java.util.ArrayList;

import java.util.List;

public class AStar {

public static final int[][] NODES = {

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

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

};

public static final int STEP = 10;

private ArrayList openList = new ArrayList();

private ArrayList closeList = new ArrayList();

public Node findMinFNodeInOpneList() {

Node tempNode = openList.get(0);

for (Node node : openList) {

if (node.F < tempNode.F) {

tempNode = node;

}

}

return tempNode;

}

public ArrayList findNeighborNodes(Node currentNode) {

ArrayList arrayList = new ArrayList();

// 只考慮上下左右,不考慮斜對角

int topX = currentNode.x;

int topY = currentNode.y - 1;

if (canReach(topX, topY) && !exists(closeList, topX, topY)) {

arrayList.add(new Node(topX, topY));

}

int bottomX = currentNode.x;

int bottomY = currentNode.y + 1;

if (canReach(bottomX, bottomY) && !exists(closeList, bottomX, bottomY)) {

arrayList.add(new Node(bottomX, bottomY));

}

int leftX = currentNode.x - 1;

int leftY = currentNode.y;

if (canReach(leftX, leftY) && !exists(closeList, leftX, leftY)) {

arrayList.add(new Node(leftX, leftY));

}

int rightX = currentNode.x + 1;

int rightY = currentNode.y;

if (canReach(rightX, rightY) && !exists(closeList, rightX, rightY)) {

arrayList.add(new Node(rightX, rightY));

}

return arrayList;

}

public boolean canReach(int x, int y) {

if (x >= 0 && x < NODES.length && y >= 0 && y < NODES[0].length) {

return NODES[x][y] == 0;

}

return false;

}

public Node findPath(Node startNode, Node endNode) {

// 把起點加入 open list

openList.add(startNode);

while (openList.size() > 0) {

// 遍歷 open list ,查找 F值最小的節點,把它作為當前要處理的節點

Node currentNode = findMinFNodeInOpneList();

// 從open list中移除

openList.remove(currentNode);

// 把這個節點移到 close list

closeList.add(currentNode);

ArrayList neighborNodes = findNeighborNodes(currentNode);

for (Node node : neighborNodes) {

if (exists(openList, node)) {

foundPoint(currentNode, node);

} else {

notFoundPoint(currentNode, endNode, node);

}

}

if (find(openList, endNode) != null) {

return find(openList, endNode);

}

}

return find(openList, endNode);

}

private void foundPoint(Node tempStart, Node node) {

int G = calcG(tempStart, node);

if (G < node.G) {

node.parent = tempStart;

node.G = G;

node.calcF();

}

}

private void notFoundPoint(Node tempStart, Node end, Node node) {

node.parent = tempStart;

node.G = calcG(tempStart, node);

node.H = calcH(end, node);

node.calcF();

openList.add(node);

}

private int calcG(Node start, Node node) {

int G = STEP;

int parentG = node.parent != null ? node.parent.G : 0;

return G + parentG;

}

private int calcH(Node end, Node node) {

int step = Math.abs(node.x - end.x) + Math.abs(node.y - end.y);

return step * STEP;

}

public static void main(String[] args) {

Node startNode = new Node(5, 1);

Node endNode = new Node(5, 5);

Node parent = new AStar().findPath(startNode, endNode);

for (int i = 0; i < NODES.length; i++) {

for (int j = 0; j < NODES[0].length; j++) {

System.out.print(NODES[i][j] + ", ");

}

System.out.println();

}

ArrayList arrayList = new ArrayList();

while (parent != null) {

// System.out.println(parent.x + ", " + parent.y);

arrayList.add(new Node(parent.x, parent.y));

parent = parent.parent;

}

System.out.println("\n");

for (int i = 0; i < NODES.length; i++) {

for (int j = 0; j < NODES[0].length; j++) {

if (exists(arrayList, i, j)) {

System.out.print("@, ");

} else {

System.out.print(NODES[i][j] + ", ");

}

}

System.out.println();

}

}

public static Node find(List nodes, Node point) {

for (Node n : nodes)

if ((n.x == point.x) && (n.y == point.y)) {

return n;

}

return null;

}

public static boolean exists(List nodes, Node node) {

for (Node n : nodes) {

if ((n.x == node.x) && (n.y == node.y)) {

return true;

}

}

return false;

}

public static boolean exists(List nodes, int x, int y) {

for (Node n : nodes) {

if ((n.x == x) && (n.y == y)) {

return true;

}

}

return false;

}

public static class Node {

public Node(int x, int y) {

this.x = x;

this.y = y;

}

public int x;

public int y;

public int F;

public int G;

public int H;

public void calcF() {

this.F = this.G + this.H;

}

public Node parent;

}

}

總結

以上是生活随笔為你收集整理的a 寻路算法 java_A*(也叫A star, A星)寻路算法Java版 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。