- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我已经使用这个伪代码在 Java 中编写了一个 A* 寻路算法。对于某些情况,它工作正常,但对于某些情况,它会永远挂起并向打开列表添加无限数量的节点。这是我执行伪代码的问题,还是伪代码本身的问题?我已经尝试了一堆用于调试的打印语句,但我什至无法确定问题所在。这是我的 A* 代码:
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
public class AStar {
Point[] adjacents = {new Point(0,1),new Point(1,1), new Point(1,0), new Point(1,-1), new Point(0,-1), new Point(-1,-1), new Point(-1,0), new Point(-1,1)};
public Node[] calculatePH(Node startNode, Node endNode, int[][] map) {
// Initialize both open and closed list
ArrayList<Node> open = new ArrayList<Node>();
ArrayList<Node> closed = new ArrayList<Node>();
// Add the start node
open.add(startNode);
// Loop until you find the end
while(!open.isEmpty()) {
// Get the current node
Node currentNode = open.get(0);
for(Node n : open) {
if(n.f<currentNode.f) {
currentNode = n;
}
}
open.remove(currentNode);
closed.add(currentNode);
// Found the goal
if(currentNode.equals(endNode)) {
ArrayList<Node> path = new ArrayList<Node>();
Node current = currentNode;
while(current!=null) {
path.add(current);
current = current.parent;
}
Collections.reverse(path);
return path.toArray(new Node[path.size()]);
}
// Generate children
ArrayList<Node> children = new ArrayList<Node>();
for(Point p : adjacents) {
Point childpos = currentNode.getLocation();
childpos.translate(p.x, p.y);
//Checks to ensure that the node is on the actual field, so we don't ram into a field tech lol
if((childpos.y>(map.length-1)) || (childpos.y<0) || (childpos.x>(map[0].length-1)) || (childpos.x<0)) {
continue;
}
//Checks to ensure that the node is "walkable", or basically just not inside a wall or object.
if(map[childpos.y][childpos.x] != 0) {
continue;
}
Node child = new Node(currentNode,childpos);
children.add(child);
}
for(Node n : children) {
// Child is on the closedList
if(closed.indexOf(n)>-1) {
continue;
}
n.g = currentNode.g + currentNode.distance(n);
n.h = n.distance(endNode);
n.f = n.g+n.h;
boolean inFlag = false;
for(Node m : open) {
if((n==m) && (n.g>m.g)) {
inFlag = true;
}
}
if(!inFlag) {
open.add(n);
}
}
}
return new Node[0];
}
}
还有 Node 类,它是 java.awt.Point 的子类,带有一些额外的东西。
import java.awt.Point;
import java.util.ArrayList;
public class Node extends Point { //Extends Point for conveinince and
//neatness. Maybe just use a has-a relationship if this breaks.
public Node(Node Parent, Point loc) {
super(loc);
this.parent = Parent;
}
public Node(Point loc) {
super(loc);
}
void translate(Point p){
translate(p.x,p.y);
}
public Node parent;
public double f;
public double g;
public double h;
public String toString() {
return "("+x+","+y+")";
}
}
我正在使用一个高 27 宽 54 的迷宫,起点在 (0,0),终点在 (24,24),目前有两个障碍。在这种情况下它工作正常:
但在这种情况下它会永远挂起。唯一的区别是第二个障碍物向下延伸了 1 行。
任何帮助都将不胜感激,如果我应该更改问题或添加更多信息,请告诉我。
编辑:它不是很漂亮,但这里是包含非功能版本的 map 数据的 int[][]。
int[][] map ={
{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,0,0,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,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,1,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,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,1,1,1,1,1,1,1,1,1,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,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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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,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},
{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,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
最佳答案
刚刚弄明白了,感谢 bracco23 为我指出了正确的代码部分。事实证明我犯了一个新手 java 错误并使用 == 而不是 .equals 进行了比较。换掉它解决了这个问题。
关于java - A* 寻路在某些情况下永远运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51771237/
有人可以解释预定义谓词forall如何在列表中找到最小值吗? 最佳答案 对于列表L,您可以使用: member(Min,L), forall(member(N,L), N>=Min). 但是,尽管这是
编辑:澄清一下,我正在搜索的对象数组确实已按搜索变量的字母数字顺序进行了预排序。 我做了一个二分搜索函数并将它嵌套在另一个函数中。出于某种原因,每次我使用二进制搜索都无法找到相关的字符数组。 基本上,
是否可以阻止用户(甚至是管理员)终止我的程序? 或者万一被杀死,它会迅速恢复自身? 更新:澄清一下:我正在编写一个监控程序,类似于家长控制,它记录用户对 PC 的操作。你可以通过查看我最近的其他问题来
我有一个 for 循环,我希望它永远递增。 我的代码: for a in (0...Float::INFINITY).step(2) puts a end 输出: 0.0 2.0 4.0 Et
我很困惑。我有一个运行Ubuntu 14.04的VM。我在这里遵循了以下程序:http://clang.llvm.org/docs/LibASTMatchersTutorial.html,现在正在运行
这是我的代码 #include #include #include #include #include #include #include #include #include usi
我有一个程序会或多或少地通过标准输入使用 COPY FROM 将大量数据复制到 Postgres 9 中。 这目前工作正常,但我正在缓冲数据 block ,然后分批运行 COPY FROM 操作。 我
我想我不小心在某个地方安装了 Foreverjs 并启动了它。每次我杀死这个进程时,另一个进程就会取代它的位置 ] 1 我不知道永远在哪里(或者这实际上是导致它的原因),因为我在本地安装了它。 最佳答
我得到了一个 forever: command not found 当我使用 forever 命令作为 cronjob 运行 nodejs 进程时出现错误(在亚马逊 ec2 机器中):我正在使用的 b
我创建了一些容器,它们还没有准备好使用,总是“重新启动”状态: docker ps CONTAINER ID IMAGE COMMAND
我试图永远重复一个 IO 操作,但是将一个执行的结果输入到下一个执行中。像这样的东西: -- poorly named iterateM :: Monad m => (a -> m a) -> a -
这里的代码样式问题。 我看着this问题,它询问.NET CLR是否真的总是初始化字段值。 (答案是肯定的。)但令我感到惊讶的是,我不确定执行此操作始终是个好主意。我的想法是,如果我看到这样的声明:
美好的一天,我对永久启动\停止脚本有一些问题。 中央操作系统 6.2 内核 2.6.32-220.el6.x86_64 node.js v0.6.19 npm v 1.1.24 永远@0.9.2 我创
我在让管道与 paramiko 一起工作时遇到问题。 这个有效: ssh = paramiko.SSHClient() [...] stdin, stdout, stderr = ssh.exec_c
我希望守护我的 Node.js 应用程序。 Upstart 和永远有什么区别?另外,还有其他我可能想要考虑的软件包吗? 最佳答案 正如评论中指出的,upstart将用于启动 forever脚本,因为
我有以下查询,其中包含在 5 秒内返回数据的选择查询。但是当我在前面添加创建物化 View 命令时,查询需要创建物化 View 。 最佳答案 当您创建物化 View 时,实际上是创建了 Oracle
当我今天访问我的项目的 Google Cloud 控制台并单击“计算引擎”或“云存储”时,它只会永远显示“正在加载”。几天前,我能够看到我的虚拟机和存储桶。有没有办法让控制台再次工作? 谢谢, 麦克风
我编写了一个函数,它当前显示 1000 以下的所有质数。 我可以继续增大 1000 以生成更多数字,但我不知道如何让它在运行后一直持续下去。 func generatePrimes() { l
这是由 another question 触发的. 具体来说,我有一个进程中的 COM 类,它在 CLSID registry 中定义。因为有 ThreadingModel of Both . 我们的
我正在试用新的 React Hooks的 useEffect API,它似乎永远在无限循环中运行!我只希望 useEffect 中的回调运行一次。这是我的引用代码: 单击“运行代码片段”以查看“运行
我是一名优秀的程序员,十分优秀!