简单递归算法中终止路径的问题

发布于 2024-09-15 08:39:46 字数 1900 浏览 2 评论 0原文

首先:这不是一项家庭作业,而是我的一个爱好项目。

背景: 对于我的 Java 益智游戏,我使用一个非常简单的递归算法来检查“地图”上的某些空间在放置一块后是否已被隔离。在这种情况下,隔离意味着:不能放置任何棋子。

当前算法:

public int isolatedSpace(Tile currentTile, int currentSpace){
        if(currentTile != null){
            if(currentTile.isOpen()){
                currentTile.flag(); // mark as visited
                currentSpace += 1;
                currentSpace = isolatedSpace(currentTile.rightNeighbor(),currentSpace);
                currentSpace = isolatedSpace(currentTile.underNeighbor(),currentSpace);
                currentSpace = isolatedSpace(currentTile.leftNeighbor(),currentSpace);
                currentSpace = isolatedSpace(currentTile.upperNeighbor(),currentSpace);
                if(currentSpace < 3){currentTile.markAsIsolated();} // <-- the problem
            }
        }
        return currentSpace;
    }

这段代码返回起始图块所在的空白空间的大小。这部分代码按预期工作。但我遇到了一个关于瓷砖标记的问题,这就是这个问题的标题相关的原因;)

问题: 问题是某些图块永远不会被“重新访问”(它们返回一个值并终止,因此永远不会从后来的化身中获得返回值来更新空白空间的大小)。这些“被遗忘”的图块可能是大空间的一部分,但仍被标记为隔离,因为它们是在进程开始时 currentSpace 具有较低值时被访问的。

问题: 如何改进此代码,以便在没有太多开销的情况下为图块设置正确的值?我可以想到丑陋的解决方案,例如重新访问所有标记的图块,以及它们是否具有正确的值,检查邻居是否具有相同的值,如果没有更新等。但我确信 Stack Overflow 上有很多聪明的人有更好的想法; )


更新: 我做了一些改变。

public int isolatedSpace(Tile currentTile, int currentSpace, LinkedList<Tile> visitedTiles){
        if(currentTile != null){
            if(currentTile.isOpen()){
                // do the same as before
                visitedTiles.add(); 
            }
        }
        return currentSpace;
    }

而marktiles函数(仅当返回的spacesize小于给定值时调用)

marktiles(visitedTiles){
       for(Tile t : visitedTiles){
            t.markAsIsolated();
       }
}

这种方法符合Rex Kerr的答案,至少如果我理解他的想法的话。

First of all: this is not a homework assignment, it's for a hobby project of mine.

Background:
For my Java puzzle game I use a very simple recursive algorithm to check if certain spaces on the 'map' have become isolated after a piece is placed. Isolated in this case means: where no pieces can be placed in.

Current Algorithm:

public int isolatedSpace(Tile currentTile, int currentSpace){
        if(currentTile != null){
            if(currentTile.isOpen()){
                currentTile.flag(); // mark as visited
                currentSpace += 1;
                currentSpace = isolatedSpace(currentTile.rightNeighbor(),currentSpace);
                currentSpace = isolatedSpace(currentTile.underNeighbor(),currentSpace);
                currentSpace = isolatedSpace(currentTile.leftNeighbor(),currentSpace);
                currentSpace = isolatedSpace(currentTile.upperNeighbor(),currentSpace);
                if(currentSpace < 3){currentTile.markAsIsolated();} // <-- the problem
            }
        }
        return currentSpace;
    }

This piece of code returns the size of the empty space where the starting tile is part of. That part of the code works as intented. But I came across a problem regarding the marking of the tiles and that is what makes the title of this question relevant ;)

The problem:
The problem is that certain tiles are never 'revisited' (they return a value and terminate, so never get a return value themselves from a later incarnation to update the size of the empty space). These 'forgotten' tiles can be part of a large space but still marked as isolated because they were visited at the beginning of the process when currentSpace had a low value.

Question:
How to improve this code so it sets the correct value to the tiles without too much overhead? I can think of ugly solutions like revisiting all flagged tiles and if they have the proper value check if the neighbors have the same value, if not update etc. But I'm sure there are brilliant people here on Stack Overflow with much better ideas ;)


Update:
I've made some changes.

public int isolatedSpace(Tile currentTile, int currentSpace, LinkedList<Tile> visitedTiles){
        if(currentTile != null){
            if(currentTile.isOpen()){
                // do the same as before
                visitedTiles.add(); 
            }
        }
        return currentSpace;
    }

And the marktiles function (only called when the returned spacesize is smaller than a given value)

marktiles(visitedTiles){
       for(Tile t : visitedTiles){
            t.markAsIsolated();
       }
}

This approach is in line with the answer of Rex Kerr, at least if I understood his idea.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡淡绿茶香 2024-09-22 08:39:46

这不是通用解决方案,但只有当空格出现在两个或更少空格的区域中时,您才将它们标记为隔离的。难道您不能将此测试简化为“一个空间是孤立的,当且仅当(a)它没有开放邻居或(b)恰好有一个开放邻居并且该邻居没有其他开放邻居”。

This isn't a general solution, but you only mark spaces as isolated if they occur in a region of two or fewer spaces. Can't you simplify this test to "a space is isolated iff either (a) it has no open neighbours or (b) precisely one open neighbour and that neighbour has no other open neighbours".

七七 2024-09-22 08:39:46

您需要有一个两步过程:收集有关空间是否隔离的信息,然后单独标记为隔离。因此,您需要首先计算所有空间(使用一个递归函数),然后在标准通过时标记所有连接的空间(使用不同的递归函数)。

You need to have a two-step process: gathering info about whether a space is isolated, and then then marking as isolated separately. So you'll need to first count up all the spaces (using one recursive function) and then mark all connected spaces if the criterion passes (using a different recursive function).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文