查找距矩阵距离 k 以内的元素

发布于 2024-11-07 03:01:50 字数 391 浏览 0 评论 0原文

给定一个*n矩阵和一个值k,我们如何找到每个元素的所有邻居? 例如:在 4*4 矩阵中,k=2 假设矩阵为:

[ 1  2  3  4
  5  6  7  8
  9 10 11 12
 13 14 15 16]

其中这些值是位置的索引,1 的邻居是 1,2,3,5,6,9 。值 3,6 和 9 出现只是因为 k =2,如果 k = 1,则不会出现。

类似地,6 的邻居将是 1 2 3 5 6 7 8 9 10 11 14

你能帮我写一个c代码来用c++实现这个吗?

这是冯诺依曼邻域问题,请有人用c++实现它。谢谢

Given a n*n matrix and a value k, how do we find all the neighbors for each element?
for example: in a 4*4 matrix, with k=2
say matrix is :

[ 1  2  3  4
  5  6  7  8
  9 10 11 12
 13 14 15 16]

where these values are the indexes of the location, the neighbors for 1 are 1,2,3,5,6,9 . The values 3,6 and 9 come only because k =2 and wouldnt be there if k was = 1.

similarly the neighbors of 6 will be 1 2 3 5 6 7 8 9 10 11 and 14

Can you please help me to write a c code to implement this in c++.

It is the problem of von Neumann neighborhood, please can some one implement it in c++. Thanks

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

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

发布评论

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

评论(2

紫瑟鸿黎 2024-11-14 03:01:50

您的邻居将在您的目标元素周围形成菱形图案。菱形的点距目标元素有 k 跳。因此,顶部将向上 k 行,左侧将向上 k 列,依此类推。当您从一层到另一层时,菱形会均匀扩展。如果您从顶部点开始向下一行(更靠近目标节点),那么您将向每一侧输出 1。它在其他方向上是对称的。换句话说,邻居节点和目标节点之间的x坐标差加上y坐标差将≤k。

因此,只需创建两个嵌套的 for 循环来迭代此菱形即可。外循环遍历行,内循环遍历列。从顶部开始,然后在每次外循环迭代时将菱形扩展 1,直到到达与目标元素相同的行,然后收缩直到到达底部点。
显然,您需要测试离开矩阵的边界条件。

Your neighbors will form a diamond pattern around your target element. The points of the diamond will be k hops away from the target element. So the top will be k rows up, the left will be k columns over, etc. The diamond expands uniformly as you go from level to level. If you start at the top point and go one row down (closer to the target node) then you go out 1 to each side. It's symmetric in the other directions. In other words, the difference in x coordinates between a neighbor and the target node plus the difference in y will be <= k.

So just make two nested for loops that iterate over this diamond. Outer loop iterates over the rows, inner loop over the columns. Start at the top then expand the diamond by 1 at each outer loop iteration until you reach the same row as the target element, then contract until you reach the bottom point.
Obviously you'll need to test boundary conditions for going outside the matrix.

我们的影子 2024-11-14 03:01:50

当 k=1 时,这应该可以解决问题。进行微小的更改以使其适用于所有 k

int width = 4;
int height = 4;
int k = 1;
int value = 2;

bool hasRight = (value % width != 0);
bool hasLeft = (value % width != 1);
bool hasTop = (value > 4);
bool hasBottom = (value < (height * width - width));

cout << value;  // Always itself
if(hasRight == true) {
 cout << value+1 << " ";  // Right
 if(hasTop == true) {
  cout << value-width << " " << value-width+1 << " "; // Top and Top-right
 }
 if(hasBottom == true) {
  cout << value+width << " " << value+width+1; // Bottom and Bottom-right
 }
}

if(hasLeft == true) {
 cout << value-1 << " ";  // Left
 if(hasTop == true) {
  cout << value-width-1 << " ";  // Top-left
 }
 if(hasBottom == true) {
  cout << value+width-1 << " ";  // Bottom-left
 }
}

This should do the trick for k=1. Make minor changes to make it work for all k

int width = 4;
int height = 4;
int k = 1;
int value = 2;

bool hasRight = (value % width != 0);
bool hasLeft = (value % width != 1);
bool hasTop = (value > 4);
bool hasBottom = (value < (height * width - width));

cout << value;  // Always itself
if(hasRight == true) {
 cout << value+1 << " ";  // Right
 if(hasTop == true) {
  cout << value-width << " " << value-width+1 << " "; // Top and Top-right
 }
 if(hasBottom == true) {
  cout << value+width << " " << value+width+1; // Bottom and Bottom-right
 }
}

if(hasLeft == true) {
 cout << value-1 << " ";  // Left
 if(hasTop == true) {
  cout << value-width-1 << " ";  // Top-left
 }
 if(hasBottom == true) {
  cout << value+width-1 << " ";  // Bottom-left
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文