Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java BFS:优先级队列未变为空_Java_Algorithm_Priority Queue_Breadth First Search_Java 8 - Fatal编程技术网

Java BFS:优先级队列未变为空

Java BFS:优先级队列未变为空,java,algorithm,priority-queue,breadth-first-search,java-8,Java,Algorithm,Priority Queue,Breadth First Search,Java 8,所以我想解决我上一篇文章中描述的问题:。问题的副本如下所示: 我想使用一个非常基本的原则创建一个有山的地形,如高度贴图所示: 0 0 0 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 0 1 2 1 0 0 0 0 0 0 0 1 2 3 2 1 0 0 0 0 0 1 2 3 4 3 2 1 0 0 0 0 0 1 2 3 2 1 0 0 0 0 0 0 0 1 2 1 0 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 0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 2 1 0 0 0 0
0 0 0 1 2 3 2 1 0 0 0
0 0 1 2 3 4 3 2 1 0 0
0 0 0 1 2 3 2 1 0 0 0
0 0 0 0 1 2 1 0 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 0 0 0 0
它从一个高度为4的随机点开始,然后在相邻点上逐渐减小

递归思想很简单,我从一个点开始,用
高度-1
递归到上/下/左/右(在本例中),只有在还没有遇到的情况下,我才设置它们的值

所以我继续用BFS实现了它:

private void createMountain(final float[][] heightMapping, final float startHeight) {
    boolean[][] traversed = new boolean[width][depth];
    boolean positive = (startHeight >= 0f);
    int x = random.nextInt(width);
    int z = random.nextInt(depth);
    PriorityQueue<QueueElement> priorityQueue = new PriorityQueue<>((o1, o2) -> (int)Math.signum(o1.height - o2.height));
    priorityQueue.offer(new QueueElement(x, z, startHeight));
    while (!priorityQueue.isEmpty()) {
        QueueElement current = priorityQueue.poll();
        if (current.x < 0 || current.x >= width || current.z < 0 || current.z >= depth) {
            continue;
        }
        if (traversed[current.x][current.z]) {
            continue;
        }
        if ((positive && current.height <= 0f) || (!positive && current.height >= 0f)) {
            continue;
        }
        heightMapping[x][z] = current.height;
        priorityQueue.offer(new QueueElement(x, z - 1, calculateNewHeight(current.height, positive)));
        priorityQueue.offer(new QueueElement(x, z + 1, calculateNewHeight(current.height, positive)));
        priorityQueue.offer(new QueueElement(x - 1, z, calculateNewHeight(current.height, positive)));
        priorityQueue.offer(new QueueElement(x + 1, z, calculateNewHeight(current.height, positive)));
    }
}

private class QueueElement {
    public int x, z;
    public float height;

    public QueueElement(final int x, final int z, final float height) {
        this.x = x;
        this.z = z;
        this.height = height;
    }
}

private float calculateNewHeight(final float startHeight, final boolean positive) {
    float delta = startHeight / maxDecayFactor;
    return (positive) ? startHeight - delta : startHeight + delta;
}

可能出了什么问题?
PriorityQueue
现在把事情搞砸了吗?

你忘了设置
traversed[x][z]=true轮询新元素时。

确保在查询
if(遍历[current.x][current.z])…


另外,作为补充说明,对于您的实现,我认为-a将是一个更好的主意。(当我编写PriorityQueue时会更好,我想到了多个峰值-这似乎不是你的情况,我认为一个简单的队列会更好)。

你忘记设置
遍历[x][z]=true轮询新元素时。

确保在查询
if(遍历[current.x][current.z])…


另外,作为补充说明,对于您的实现,我认为-a将是一个更好的主意。(当我编写PriorityQueue时会更好,我想到了多个峰值-这似乎不是你的情况,我认为一个简单的队列将是最好的)。

我正在重新定位
遍历的[x][z]=true
,但忘了将其放回去。但实际上更多的事情是错误的,我现在添加了一个epsilon值,并且我需要在大多数情况下使用
current.x
current.z
,而不是最初的
x
z
!我现在唯一的问题是,地形看起来仍然错误,我将向OP添加额外的输出。使用
LinkedBlockingQueue
它现在突然工作了!我还不知道为什么它不能与
PriorityQueue
@skiwi一起工作,你把什么作为优先级队列的比较函数?
PriorityQueue PriorityQueue=new PriorityQueue((o1,o2)->(int)Math.signum(o1.height-o2.height))
,它使用lambdas缩写表示“最大优先”。@skiwi我认为发生这种情况是因为您更改了已在优先级队列(更深)中的对象的
o2.height
,这导致了这种行为,但我可能错了。我重新定位了
遍历的[x][z]=true
,并忘记将其放回。但实际上更多的事情是错误的,我现在添加了一个epsilon值,并且我需要在大多数情况下使用
current.x
current.z
,而不是最初的
x
z
!我现在唯一的问题是,地形看起来仍然错误,我将向OP添加额外的输出。使用
LinkedBlockingQueue
它现在突然工作了!我还不知道为什么它不能与
PriorityQueue
@skiwi一起工作,你把什么作为优先级队列的比较函数?
PriorityQueue PriorityQueue=new PriorityQueue((o1,o2)->(int)Math.signum(o1.height-o2.height))
,它使用lambdas缩写表示“最大优先”。@skiwi我认为发生这种情况是因为您为已在优先级队列中(更深)的对象更改了
o2.height
——这导致了这种行为,但我可能错了。
0.6  0.5  0.4  0.3  0.3  0.2  0.2  0.1  0.1  0.1  0.1  0.1  0.0  0.0  0.0  0.0  
0.8  1.0  1.2  1.6  1.9  2.4  3.0  3.8  4.7  5.9  7.4  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0