在数组中查找对角线,该数组表示二维数组

时间:2021-08-03 21:28:02

I converted my 2d array to a 1d array. For example: (Starts at 0, not 1);
00 01 02 03 04
05 06 07 08 09
10 11 12 13 14
15 16 17 18 19
20 21 22 23 24

我将我的2d数组转换为1d数组。例如:(从0开始,而不是1); 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24


was converted to a 1d array. [0, 1, 2, 3, 4.....23, 24].

被转换为1d数组。 [0,1,2,3,4 ...... 23,24]。

I am now attempting to create a function that finds every spot that is "connected" or next to a certain element in the array. This includes elements that are diagonal from it. So using the above 2d array, if I want an array of elements that are connected to 0, I expect the function to return the array [1, 5, 6].

我现在正在尝试创建一个函数,该函数可以找到“连接”或数组中某个元素旁边的每个点。这包括与其对角的元素。所以使用上面的2d数组,如果我想要一个连接到0的元素数组,我希望该函数返回数组[1,5,6]。

The trouble I am having is finding the diagonals. This is my JS code for the array that should be returned.

我遇到的麻烦就是找到对角线。这是我应该返回的数组的JS代码。

var poss = [Number(num+1),Number(num-1),Number(num+col),Number(num-col),Number((num+col) + 1),Number((num+col) - 1),Number((num-col) + 1),Number((num-col) - 1)];

This returns [1, 5, 6, 4]. I have code that excludes negative numbers. However, 4 should not be there. I realize this is because this is an edge case and it isn't registering as out of bounds because it isn't a negative number. Is there a formula of some sort that will find the elements connected to it diagonally ? Remember I am using a 1d array. This program also run independently of the array size. So this will also have to work for boards that are 4x4 or 5x4. So using the row and num fields is ideal.

这返回[1,5,6,4]。我的代码不包括负数。但是,4不应该存在。我意识到这是因为这是一个边缘情况,它没有注册为超出界限,因为它不是负数。是否有某种公式可以找到与其对角连接的元素?记得我使用的是1d数组。该程序也独立于数组大小运行。因此,这也必须适用于4x4或5x4的电路板。所以使用row和num字段是理想的。

4 个解决方案

#1


1  

I've seemed to have figured it out. At the very least, I've passed all of my test cases. I am sure that this is not the most simply, elegant, or efficient way. More test cases are probably needed. I made sure it is calculated independent of the number of columns and rows. Here is the huge if statement I used.

我似乎已经弄明白了。至少,我已经通过了所有的测试用例。我确信这不是最简单,优雅或有效的方式。可能需要更多测试用例。我确保它的计算与列数和行数无关。这是我使用的巨大if语句。

var x = poss[i]
if((Number(num) % col == 0 && Number(num-1) == Number(x)) || 
 (Number(num+1) % col == 0 && Number(num+1) == Number(x)) || 
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num-col) -1) == Number(x))) || 
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num+col) -1) == Number(x))) ||
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num-col) +1) == Number(x))) || 
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num+col) +1) == Number(x)))) 
{//exclude number from results}

The variable num is the number on the array that you are currently searching neighbors for. Variable x is one of the possible neighbors.

变量num是您当前正在搜索邻居的数组上的数字。变量x是可能的邻居之一。

Feel free to post another idea.

随意发表另一个想法。

#2


1  

This was my solution to this issue, it should be easy to read and understand and I added some comments on it as well.

这是我对这个问题的解决方案,它应该易于阅读和理解,我也添加了一些评论。

var cols = 5;
var rows = 5;

function connectedPoints(point) {
  var connectedPoints = [];

  // First test if the point is on an edge
  var topEdge = point/cols < 1;
  var leftEdge = point%cols == 0;
  var rightEdge = point%cols == cols-1;
  var bottomEdge = point/cols >= rows-1;

  // Add points that are above the point
  if (!topEdge) {
    if (!leftEdge) {
      connectedPoints.push(returnIfNotNegative(point-cols-1));
    }

    connectedPoints.push(returnIfNotNegative(point-cols));

    if (!rightEdge) {
      connectedPoints.push(returnIfNotNegative(point-cols+1));
    }
  }

  // Add points that are to the left or right of the point
  if (!leftEdge) {
    connectedPoints.push(returnIfNotNegative(point-1));
  }
  if (!rightEdge) {
    connectedPoints.push(returnIfNotNegative(point+1));
  }

  // Add points that are below the point
  if (!bottomEdge) {
    if (!leftEdge) {
      connectedPoints.push(returnIfNotNegative(point+cols-1));
    }

    connectedPoints.push(returnIfNotNegative(point+cols));

    if (!rightEdge) {
      connectedPoints.push(returnIfNotNegative(point+cols+1));
    }
  }

  console.log(connectedPoints);
}

function returnIfNotNegative(point) {
  if (point < 0) {
    return null;
  }
  return point;
}

connectedPoints(0);

#3


1  

You can try using position instead of value. This will simplify things.

您可以尝试使用position而不是value。这将简化事情。

You can have another function to get position of value.

您可以使用另一个函数来获取值的位置。

var arr = [
  [00, 01, 02, 03, 04],
  [05, 06, 07, 08, 09],
  [10, 11, 12, 13, 14],
  [15, 16, 17, 18, 19],
  [20, 21, 22, 23, 24]
]

function getNeighbours(x, y) {
  var result = [];
  for (var i = x - 1; i <= x + 1; i++) {
    for (var j = y - 1; j <= y + 1; j++) {
      if (arr[i] && arr[i][j]) {
        if (!(x === i && y === j))
          result.push(arr[i][j]);
      }
    }
  }
  return result;
}

console.log(getNeighbours(0, 0));
console.log(getNeighbours(3, 3));

#4


1  

When working with the arrays it's important to move as much computation outside the inner loop as possible. My approach was to find the start positions of the left column in each row and then iterating over the column range.

使用数组时,尽可能多地在内循环外移动计算很重要。我的方法是在每一行中找到左列的起始位置,然后迭代列范围。

var arr = [
  0, 1, 2, 3, 4,
  5, 6, 7, 8, 9,
  10, 11, 12, 13, 14,
  15, 16, 17, 18, 19,
  20, 21, 22, 23, 24
]

function getNeighbours(col, row)
{
  var w = 5;
  
  var start = (row > 0 ? row * w - w : row * w) + (col > 0 ? col - 1 : col);
  var rowSpan = ((row > 0 ? 2 : 1) + (row < (w - 1) ? 1 : 0)) * w;
  var colSpan = (col > 0 ? 2 : 1) + (col < (w -1) ? 1 : 0);
  
  var center = col + row * w;
  var result = [];
  for (var r = start; r < start + rowSpan; r += w)
    for (var i = r; i < r + colSpan; i++)
      if (!(i === center))
         result.push(arr[i]);
      
  return result;
}


console.log(getNeighbours(0,0));
console.log(getNeighbours(3,3));
console.log(getNeighbours(3,4));
console.log(getNeighbours(4,3));
console.log(getNeighbours(4,4));
 
/*
[1, 5, 6]
[12, 13, 14, 17, 19, 22, 23, 24]
[17, 18, 19, 22, 24]
[13, 14, 18, 23, 24]
[18, 19, 23]
*/

#1


1  

I've seemed to have figured it out. At the very least, I've passed all of my test cases. I am sure that this is not the most simply, elegant, or efficient way. More test cases are probably needed. I made sure it is calculated independent of the number of columns and rows. Here is the huge if statement I used.

我似乎已经弄明白了。至少,我已经通过了所有的测试用例。我确信这不是最简单,优雅或有效的方式。可能需要更多测试用例。我确保它的计算与列数和行数无关。这是我使用的巨大if语句。

var x = poss[i]
if((Number(num) % col == 0 && Number(num-1) == Number(x)) || 
 (Number(num+1) % col == 0 && Number(num+1) == Number(x)) || 
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num-col) -1) == Number(x))) || 
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num+col) -1) == Number(x))) ||
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num-col) +1) == Number(x))) || 
 ((Number(num) % col == 0 || Number(num+1) % col == 0) && (Number((num+col) +1) == Number(x)))) 
{//exclude number from results}

The variable num is the number on the array that you are currently searching neighbors for. Variable x is one of the possible neighbors.

变量num是您当前正在搜索邻居的数组上的数字。变量x是可能的邻居之一。

Feel free to post another idea.

随意发表另一个想法。

#2


1  

This was my solution to this issue, it should be easy to read and understand and I added some comments on it as well.

这是我对这个问题的解决方案,它应该易于阅读和理解,我也添加了一些评论。

var cols = 5;
var rows = 5;

function connectedPoints(point) {
  var connectedPoints = [];

  // First test if the point is on an edge
  var topEdge = point/cols < 1;
  var leftEdge = point%cols == 0;
  var rightEdge = point%cols == cols-1;
  var bottomEdge = point/cols >= rows-1;

  // Add points that are above the point
  if (!topEdge) {
    if (!leftEdge) {
      connectedPoints.push(returnIfNotNegative(point-cols-1));
    }

    connectedPoints.push(returnIfNotNegative(point-cols));

    if (!rightEdge) {
      connectedPoints.push(returnIfNotNegative(point-cols+1));
    }
  }

  // Add points that are to the left or right of the point
  if (!leftEdge) {
    connectedPoints.push(returnIfNotNegative(point-1));
  }
  if (!rightEdge) {
    connectedPoints.push(returnIfNotNegative(point+1));
  }

  // Add points that are below the point
  if (!bottomEdge) {
    if (!leftEdge) {
      connectedPoints.push(returnIfNotNegative(point+cols-1));
    }

    connectedPoints.push(returnIfNotNegative(point+cols));

    if (!rightEdge) {
      connectedPoints.push(returnIfNotNegative(point+cols+1));
    }
  }

  console.log(connectedPoints);
}

function returnIfNotNegative(point) {
  if (point < 0) {
    return null;
  }
  return point;
}

connectedPoints(0);

#3


1  

You can try using position instead of value. This will simplify things.

您可以尝试使用position而不是value。这将简化事情。

You can have another function to get position of value.

您可以使用另一个函数来获取值的位置。

var arr = [
  [00, 01, 02, 03, 04],
  [05, 06, 07, 08, 09],
  [10, 11, 12, 13, 14],
  [15, 16, 17, 18, 19],
  [20, 21, 22, 23, 24]
]

function getNeighbours(x, y) {
  var result = [];
  for (var i = x - 1; i <= x + 1; i++) {
    for (var j = y - 1; j <= y + 1; j++) {
      if (arr[i] && arr[i][j]) {
        if (!(x === i && y === j))
          result.push(arr[i][j]);
      }
    }
  }
  return result;
}

console.log(getNeighbours(0, 0));
console.log(getNeighbours(3, 3));

#4


1  

When working with the arrays it's important to move as much computation outside the inner loop as possible. My approach was to find the start positions of the left column in each row and then iterating over the column range.

使用数组时,尽可能多地在内循环外移动计算很重要。我的方法是在每一行中找到左列的起始位置,然后迭代列范围。

var arr = [
  0, 1, 2, 3, 4,
  5, 6, 7, 8, 9,
  10, 11, 12, 13, 14,
  15, 16, 17, 18, 19,
  20, 21, 22, 23, 24
]

function getNeighbours(col, row)
{
  var w = 5;
  
  var start = (row > 0 ? row * w - w : row * w) + (col > 0 ? col - 1 : col);
  var rowSpan = ((row > 0 ? 2 : 1) + (row < (w - 1) ? 1 : 0)) * w;
  var colSpan = (col > 0 ? 2 : 1) + (col < (w -1) ? 1 : 0);
  
  var center = col + row * w;
  var result = [];
  for (var r = start; r < start + rowSpan; r += w)
    for (var i = r; i < r + colSpan; i++)
      if (!(i === center))
         result.push(arr[i]);
      
  return result;
}


console.log(getNeighbours(0,0));
console.log(getNeighbours(3,3));
console.log(getNeighbours(3,4));
console.log(getNeighbours(4,3));
console.log(getNeighbours(4,4));
 
/*
[1, 5, 6]
[12, 13, 14, 17, 19, 22, 23, 24]
[17, 18, 19, 22, 24]
[13, 14, 18, 23, 24]
[18, 19, 23]
*/