BFS:PriorityQueue不会为空

滑雪

所以我想解决我上一篇文章中描述的问题:Terrain / Mountain算法无法按预期工作问题的副本如下:

我想使用一个非常基本的原理创建一个上面有山的地形,此高度图显示了这一点:

0 0 0 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

它从的一个随机点开始height = 4,然后逐渐向邻域减小。

递归的想法很简单,我开始一点,用height - 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.size()始终保持增加3。
有人知道发生了什么吗?

更新:即使修复了问题,它仍然无法按预期工作。

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  

可能是什么问题?PriorityQueue现在搞乱的东西了?

什么

traversed[x][z] = true;在轮询新元素时忘记设置
确保在查询后执行此操作if (traversed[current.x][current.z]) ...

另外,请注意,对于您的实现,Queue我认为a是一个更好的主意。(当我写一个PriorityQueue会更好时,我会想到多个高峰-对于您来说似乎并非如此,我认为一个简单的Queue会是最好的)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

BFS:PriorityQueue不会变空

来自分类Dev

为什么光标永远不会为空?

来自分类Dev

C / C ++链表永远不会为空

来自分类Dev

int 的用户输入不会为空

来自分类Dev

即使我使用Parse SDK登出,getCurrentUser也不会为空

来自分类Dev

数组永远不会为空,但打字稿不知道

来自分类Dev

[FromBody] 会不会总是将我从永远不会为空的参数中拯救出来?

来自分类Dev

Libgdx Scene2d进度栏永远不会为空

来自分类Dev

Stringstream缓冲区在Objective-C ++中不会为空

来自分类Dev

语句<-不会为Maybe编译

来自分类Dev

Struts2会话中使用了拦截器,为什么它永远不会为空?

来自分类Dev

Request.IsAuthenticated永远不会为真

来自分类Dev

syncdb不会为嵌套模型创建表

来自分类Dev

JAXB不会为List创建set方法

来自分类Dev

SLComposeViewController不会为SLServiceTypeFacebook设置initialText

来自分类Dev

useSpring不会为道具更改设置动画

来自分类Dev

syncdb不会为嵌套模型创建表

来自分类Dev

Matlab:函数不会为整个范围计算

来自分类Dev

EndNote不会为某些参考添加页面

来自分类Dev

样式不会为Ubuntu SDK应用加载

来自分类Dev

不会为CheckBox触发ActionListener类

来自分类Dev

ngAnimate不会为过渡设置动画

来自分类Dev

CountDownTimer值永远不会为零

来自分类Dev

JMeter不会为Java请求保存参数

来自分类Dev

SCNShape不会为NSBezierPath绘制形状

来自分类Dev

Dagger不会为Android生成Dagger类

来自分类Dev

AJAX 成功函数永远不会为真

来自分类Dev

EclipseLink 不会为 OraclePlattform 生成终止语句

来自分类Dev

SwingWorker process() 不会为每个 publish() 调用