私はこのアルゴリズムを学び、ゲームでそれを実装しようと大胆な一歩を踏み出しました。実装しようとして、私は終了しない無限ループを続けています。私は何度も何度も何度も何度も何度も何度も何度も何度も練習をしてきましたが、何かを見逃したり、どんな助けもありがとう。Android A *パスが無限にループを見つけることはありますか?
public class PathFinder {
private LinkedList<Node> openList;
private LinkedList<Node> closedList;
public PathFinder() {
openList = new LinkedList<Node>();
closedList = new LinkedList<Node>();
}
private List<Node> buildPath(Node node) {
LinkedList<Node> path = new LinkedList<Node>();
while (node.parentNode != null) {
path.addFirst(node);
node = node.parentNode;
}
return path;
}
public List<Node> findPath(int sx, int sy, int dx, int dy) {
Node startNode = new Node(sx, sy);
Node endNode = new Node(dx, dy);
startNode.costG = 0;
startNode.costH = startNode.getManhattanCost(endNode);
startNode.parentNode = null;
openList.add(startNode);
while (!openList.isEmpty()) {
Node currentNode = (Node) openList.removeFirst();
if (currentNode == endNode) {
Log.d("android", "found path");
return buildPath(endNode);
} else {
currentNode.createNeighbors();
List<Node> neighbors = currentNode.getNeighbors();
for (int i = 0; i < neighbors.size(); i++) {
Node neighborNode = neighbors.get(i);
neighborNode.costG += currentNode.costG;
neighborNode.costH = neighborNode.getManhattanCost(endNode);
neighborNode.costF = neighborNode.costG + neighborNode.costH;
boolean isInOpen = openList.contains(neighborNode);
boolean isInClosed = closedList.contains(neighborNode);
if ((!isInOpen && !isInClosed) || neighborNode.costG < currentNode.costG) {
neighborNode.parentNode = currentNode;
neighborNode.costG += currentNode.costG;
neighborNode.costF = neighborNode.costG + neighborNode.costH;
if (isInClosed) {
closedList.remove(neighborNode);
}
if (!isInOpen) {
openList.add(neighborNode);
}
}
}
closedList.add(currentNode);
}
}
openList.clear();
closedList.clear();
return null;
}
}
public class Node {
public Node parentNode;
public List<Node> neighbors;
public int x;
public int y;
public int costG;
public int costH;
public int costF;
public Node(int x, int y) {
this.x = x;
this.y = y;
neighbors = new ArrayList<Node>();
costG = 10;
}
public void createNeighbors() {
neighbors.add(new Node(x + 1, y));
neighbors.add(new Node(x, y + 1));
neighbors.add(new Node(x - 1, y));
neighbors.add(new Node(x, y - 1));
}
public int getManhattanCost(Node node) {
int i = (int) Math.abs(x - node.x);
int j = (int) Math.abs(y - node.y);
costH = i + j;
return costH;
}
public int getTotalCost() {
return costG + costH;
}
public List<Node> getNeighbors() {
return neighbors;
}
}
SX、SY、DX及びDYは、2Dアレイ内の位置と目標位置を開始しています。sx = 1、sy = 1、dx = 5、dy = 5のテスト用の固定数を渡しました。つまり、文字は(1,1)、タッチポイントは(5,5)です。
私は同じ問題に直面しています。 8パズル問題の無限実行。あなたはこれで私を助けてください - http://stackoverflow.com/questions/13053455/8-puzzle-solution-executes-infinitely – Ashwin