将二维数组划分为“框”以及框中的索引元素

发布于 2024-11-28 03:24:19 字数 558 浏览 0 评论 0原文

我有一个大小为 N*N 的网格或二维数组。考虑一个 6*6 数组,如下所示:

......
......
......
......
......
......

我有两个整数,高度和宽度。网格将被划分为该大小的框,例如划分为 3*3 的框,如下所示:

... ...
... ...
... ...

... ...
... ...
... ...

同样的 6*6 网格也可以划分为 2*3 的框,如下所示:

... ...
... ...

... ...
... ...

... ...
... ...

依此类推。如果重要的话,可以假设给定的两个整数总是像上面那样均匀且整齐地划分整个数组。问题是,当我在数组中有一个坐标或索引时,我需要快速索引该特定坐标的邻居。邻居是同一个框中的点。例如,如果盒子的大小为 2*3,则 (0,0) 的邻居将为 {(0,1),(0,2),(1,0),(1,1),(1 ,2)}。这看起来并不太难,但我无法想出任何简单的东西。我正在使用 C++,但这与语言无关。

I have a grid or a 2D array of size N*N. Consider a 6*6 array like the following for example:

......
......
......
......
......
......

I am given two integers, height and width. The grid will be partitioned into boxes of this size, for example into 3*3 boxes like this:

... ...
... ...
... ...

... ...
... ...
... ...

The same 6*6 grid could be also divided into 2*3 boxes like this:

... ...
... ...

... ...
... ...

... ...
... ...

And so on. One can assume that the two integers given always divide the whole array evenly and neatly as above, if it matters. The problem is then that when I have a coordinate or a index into the array, I need to quickly index the neighbours of this particular coordinate. The neighbours are the points in the same box. For example, if the boxes were of size 2*3, the neighbours of (0,0) would be {(0,1),(0,2),(1,0),(1,1),(1,2)}. This doesn't seem too difficult, but I am unable to come up with anything even remotely simple. I am using C++, but this is language-independent.

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

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

发布评论

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

评论(1

ヤ经典坏疍 2024-12-05 03:24:19

无需尝试运行该算法...

在伪代码中:

list_of_points find_neighbours_of(x, y, height_of_box, width_of_box)
{
    corner_x, corner_y = find_top_left_corner_of_box_containing(x, y)
    iterate over corner_x to corner_x + height_of_box
        iterate over corner_y to corner_y + width_of_box
            if current_x and current_y <> x, y add them to list
}

without trying to run this algorithm...

In pseudocode:

list_of_points find_neighbours_of(x, y, height_of_box, width_of_box)
{
    corner_x, corner_y = find_top_left_corner_of_box_containing(x, y)
    iterate over corner_x to corner_x + height_of_box
        iterate over corner_y to corner_y + width_of_box
            if current_x and current_y <> x, y add them to list
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文