需要遍历扩展同心正方形中的类矩阵图

发布于 2024-08-24 05:25:11 字数 691 浏览 4 评论 0原文

好的,我有一个由链接节点矩阵组成的数据结构,比如说 10x10。我希望能够任意选择这些节点中的任何一个,并从那里开始按照扩展同心正方形的模式处理许多周围的节点(出于说明目的,我在下面使用了较少的节点)。就具体细节而言,实现将在 Actionscript 中 - 但请随意用另一种语言回答:) - 它是针对扫雷游戏的(具体来说,如果单击一块没有地雷的方块,它们 - 都会出现) - 无论形状如何,都要清除,直到周围的地雷)。它也将是基于事件的 - 为每个遍历的节点调度一个,所以我想象的方式是,当迭代器或任何东西遇到包含地雷的字段时,遍历将停止沿该方向进一步前进,而其余部分将继续。这些节点与其顶部、右侧、底部和左侧邻居进行双重链接,如果有必要,我不会反对添加更多链接。可能还有其他方法可以做到这一点,但我可以想到这种解决方案的多种用途,并且希望将其包含在我正在创建的框架中,因此我想放弃一些可以重用的东西。

第一:
0 0 0 0 0
0 0 0 0 0
0 0 X 0 0
0 0 0 0 0
0 0 0 0 0

第二:
0 0 0 0 0
0 XXX 0
0 X 0 X 0
0 XXX 0
0 0 0 0 0

第三:
XXXXXX
X 0 0 0 X
X 0 0 0 X
X 0 0 0 X
XXXXXX

Ok, I've a data structure that consists of a matrix of linked nodes, lets say 10x10. I would like to be able to choose any of these nodes arbitrarily, and - from there - process a number of the surrounding nodes following the pattern of expanding concentric squares (I used less nodes below for illustration purposes). As far as specifics go, the implementation will be in Actionscript - but feel free to answer in another language :) - and it's for a minesweeper game (specifically the feature where, if clicking on a patch of mine-less squares, they -all- get cleared regardless of irregular shape, up to the surrounding mines). It will also be event based - dispatching one for each node traversed, so the way I imagine it is that when the iterator or whatever comes across a field containing a mine, traversal would stop from going further in that direction while the rest would continue. The nodes are doubly linked to their top, right, bottom, and left neighbors, and I wouldn't be opposed to adding more links if necessary. There's probably some other way to do it, but I can think of a number of uses for this kind of solution and would like to include it in a framework I'm creating, so I'd like to walk away with something I can reuse.

first:
0 0 0 0 0
0 0 0 0 0
0 0 X 0 0
0 0 0 0 0
0 0 0 0 0

second:
0 0 0 0 0
0 X X X 0
0 X 0 X 0
0 X X X 0
0 0 0 0 0

third:
X X X X X
X 0 0 0 X
X 0 0 0 X
X 0 0 0 X
X X X X X

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

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

发布评论

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

评论(1

半世晨晓 2024-08-31 05:25:11
function scan( x, y ){
    queue toDo;
    set queued;

    toDo.push( x, y );
    queued.add( x, y );
    while ( !toDo.empty() ) {
        (x, y) = toDo.removeHead();

        if ( process( x, y ) != stop ) {
            for( xp = -1; xp <= 1; ++xp ) {
                for( yp = -1; yp <= 1; ++yp ) {
                    if ( !queued.contains( x+xp, y+yp ) ) {
                        toDo.push( x+xp, y+yp );
                        queued.add( x+xp, y+yp );
                    }
                }
            }
        }
    }
}
function scan( x, y ){
    queue toDo;
    set queued;

    toDo.push( x, y );
    queued.add( x, y );
    while ( !toDo.empty() ) {
        (x, y) = toDo.removeHead();

        if ( process( x, y ) != stop ) {
            for( xp = -1; xp <= 1; ++xp ) {
                for( yp = -1; yp <= 1; ++yp ) {
                    if ( !queued.contains( x+xp, y+yp ) ) {
                        toDo.push( x+xp, y+yp );
                        queued.add( x+xp, y+yp );
                    }
                }
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文