将二维数组划分为“框”以及框中的索引元素
我有一个大小为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无需尝试运行该算法...
在伪代码中:
without trying to run this algorithm...
In pseudocode: