在2d矩阵中捕获2号的最短路径

时间:2017-11-09 08:57:07

标签: javascript

从矩阵中放置1的位置,我需要找到您必须移动到up, left, right, down的空格数(2)。您也可以将矩阵的一侧环绕到另一侧。所以下面的例子应该返回2,因为1需要首先向左移动然后向下移动。我完全陷入困境。

var ARR = [
    [0, 0, 0, 0],
    [1, 0, 0, 0],
    [0, 0, 0, 2],
    [0, 0, 0, 0]
  ],
  ONE, TWO;

for (x in ARR) {
  for (y in ARR[x]) {
    if (ARR[x][y] == 1) {
      ONE = [x, y];
    }
    if (ARR[x][y] == 2) {
      TWO = [x, y];
    }
  }
}

document.body.innerHTML = Math.abs(ONE[0] - TWO[0]) - Math.abs(ONE[1] - TWO[1]);

3 个答案:

答案 0 :(得分:0)

检查以下解决方案。在此解决方案中,无需对任何值进行硬编码。只需你可以改变你的矩阵

Here是玩弄的小提琴。



var ARR = [
    [0, 0, 0, 0],
    [1, 0, 0, 0],
    [0, 0, 0, 2],
    [0, 0, 0, 0]
  ],
  ONE, TWO;

for (x in ARR) {
  for (y in ARR[x]) {
    if (ARR[x][y] == 1) {
      ONE = [x, y];
    }
    if (ARR[x][y] == 2) {
      TWO = [x, y];
    }
  }
}

var width = ARR[0].length;
var height = ARR.length;

//get two methods of reaching via y axis
var upDown1 = Math.abs(ONE[0] - TWO[0]);
var upDown2 = height - Math.abs(ONE[0] - TWO[0]);

//get two methods of reaching via x axis
var leftRight1 = Math.abs(ONE[1] - TWO[1]);
var leftRight2 = width - Math.abs(ONE[1] - TWO[1]);

//get the minimum space counts of above methods in y and x axis
var minUpDown = Math.min(upDown1,upDown2);
var minLeftRight = Math.min(leftRight1,leftRight2);

 
document.body.innerHTML = minUpDown+minLeftRight;




答案 1 :(得分:0)

你在问题​​的介绍中说,董事会本身已经和#34; - 也就是说,你可以到最左边的左边,然后在右边,但你不在你的逻辑中,因为你只是在不考虑包裹的情况下检查距离。

这是一个有效的解决方案:

// Constants for the array, as well as the
// width and height of the array
const arr = [
  [0, 0, 0, 0, 0],
  [1, 0, 0, 0, 0],
  [0, 0, 0, 0, 2],
  [0, 0, 0, 0, 0]
];
const width = arr[0].length,
      height = arr.length;

// Stores the position of the "1" and "2"
let one, two;
// Horizontal and vertical distances between
// the "1" and "2"
let dist_x, dist_y;

// Figure out where the "1" and "2" is
for (let x in arr) {
  for (let y in arr[x]) {
    if (arr[x][y] === 1) {
      one = [x, y];
    } else if (arr[x][y] === 2) {
      two = [x, y];
    }
  } 
}

// Sets the horizontal and vertical distances
// between the two numbers
dist_x = Math.abs(one[1] - two[1]);
dist_y = Math.abs(one[0] - two[0]);

// If the distance is longer than half the size of
// the board in either direction, we go the other way
// instead.
if (dist_x > width / 2) {
  dist_x = width - dist_x;
}

if (dist_y > height / 2) {
  dist_y = height - dist_y;
}

console.log(dist_x + dist_y);

答案 2 :(得分:-2)

如果您首先确定需要向右移动的步骤,那么步骤向下,就会变得更容易:

var ARR = [
    [0, 0, 0, 0],
    [1, 0, 0, 0],
    [0, 0, 0, 2],
    [0, 0, 0, 0]
  ],
  ONE, TWO;

for (x in ARR) {
  for (y in ARR[x]) {
    if (ARR[x][y] == 1) {
      ONE = [x, y];
    }
    if (ARR[x][y] == 2) {
      TWO = [x, y];
    }
  }
}

var xSteps = Math.abs(ONE[0] - TWO[0]);
if (xSteps === 3) // then you can also do it in one by going the other direction
{
    xSteps = 1;
}

var ySteps = Math.abs(ONE[1] - TWO[1]);
if (ySteps === 3) // then you can also do it in one by going the other direction
{
    ySteps = 1;
}


document.body.innerHTML = "X steps: " + xSteps + ", Y steps: " + ySteps + ", total steps:" + (xSteps + ySteps);

如果你的矩阵大小是可变的,并不总是4 x 4,那么3的硬编码检查需要改为(矩阵大小的一半+ 1)......