【问题标题】:Iterate over 2D array of booleans and return an incremented value based on true/false迭代二维布尔数组并返回基于真/假的递增值
【发布时间】:2017-06-21 15:23:33
【问题描述】:

我试图返回一个“矩阵”或一个二维数组,其中布尔值根据旁边有多少“真”值变成一个 1-4 的数字。我之前尝试了一种不同的方法,由下面的当前代码表示。

问题:

当matrix = [[true, false, false], [false, true, false],[false, false, false]]时

输出应该是 [[1, 2, 1],[2, 1, 1],[1, 1, 1]]

我的代码:

function minesweeper(matrix) {
    for( var i =0; i < matrix.length; i++){
        for(var j = 0; j < matrix.length; j++){
            if(matrix[i] && matrix[i][j] == true){
                matrix[i][j] = 2;
            }else {
                matrix[i][j] = 1;
            }
        }
    }
    return matrix;
}

我的错误/结果:

输入矩阵:[[true,false,false],[false,true,false],[false,false,false]]

输出:[[2,1,1],[1,2,1],[1,1,1]]

预期输出:[[1,2,1], [2,1,1], [1,1,1]]

输入矩阵:[[false,false,false], [false,false,false]]

输出:[[1,1,1], [1,1,1]]

预期输出:[[0,0,0], [0,0,0]]

输入矩阵:[[true,false,false,true], [false,false,true,false], [true,true,false,true]]

输出:[[2,1,1,2], [1,1,2,1], [2,2,1,2]]

预期输出:[[0,2,2,1], [3,4,3,3], [1,2,3,1]]

【问题讨论】:

  • 您遇到了具体问题吗?你能比“我的代码有什么问题”更具体吗?
  • @Necoras 我只是想弄清楚如何将布尔值转换为数字并根据真/假正确递增它们。我之前的代码与我需要的代码相差无几。
  • 布尔值已经是数字:0 - 假,1 - 真。至于弄清楚如何正确地将附近的真/假值映射到 1-4,我怀疑这是你分配的重点,所以我将把那里的逻辑留给你。但是,我会指出您正在修改初始 matrix 并返回它,而不是将新 (1-4) 值存储在新的矩阵变量中。这永远不会给您正确的值,因为当您尝试计算第二个值时,您已经将初始矩阵更改为与起始条件不同。
  • @Necoras 感谢您的帮助!我知道他们已经有 0 和 1,我不确定是否有额外的步骤来允许递增。

标签: javascript arrays boolean increment


【解决方案1】:

更新:您的问题并不清楚您是要检查 4 个方向(例如北、西、南和东)还是 8 个方向(北、西北、西、西南、南、东南、东)和东北)。我最初的回答是针对 4 个方向。但是,我知道从您的预期结果中可以看出您可能需要 8 个方向,因此我已经针对这种情况重新编写了答案。

您提出问题的方式有问题。您谈论的是更改原始矩阵,而不是例如返回带有结果的新矩阵。如果您在仍在处理它的同时实际更改了矩阵,那么您可能最终会在实际分析它们之前更改一些值。例如,如果您分析左上角的单元格,发现它是真的,然后在同一个原始表格中将单元格向右递增,那么第二个单元格将不再是truefalse 它最初具有的值,但现在将是您分配给该单元格的任何值(??? false 加上 1 ??? 或其他)。因此,您确实应该保持原始矩阵不变,并返回一个 new 表,其中包含分析的加法结果。 (这涉及到数据不变性的问题,但这是另一天的讨论。)

无论如何,解决此问题的一种方法是从一个与原始矩阵表大小相同的结果表开始,但所有值最初都设置为零。然后,您可以遍历输入表中的所有单元格,将结果表中位于输入表中初始相应单元格右侧、下方、左侧和上方的位置加 1。但是,您必须确保您尝试添加的结果表位置实际上在表中,即不在边缘之外(例如,不在左上角单元格的上方或左侧)。

function minesweeper(matrix) {
  const numRows = matrix.length, numCols = matrix[0].length; // determine matrix size
  const dirs = [[1,0],[1,1],[0,1],[-1,1],[-1,0],[-1,-1],[0,-1],[1,-1]];
  // coordinate changes for all 8 directions
  
  const results = matrix.map(row => row.map(cell => 0)); // initiate results table with 0s
  matrix.forEach((rowOfCells, matrixRowNum) => { // for each row
    rowOfCells.forEach((cell, matrixColNum) => { // for cell in each row
      if (cell) { // if that cell contains a true value
        dirs.forEach(dir => { // iterate through all dir'ns
          const resultsRowNum = matrixRowNum + dir[0]; // vertical position in results table
          const resultsColNum = matrixColNum + dir[1]; // horizontal position in results table
          if (
            resultsRowNum >= 0       &&
            resultsRowNum <  numRows &&
            resultsColNum >= 0       &&
            resultsColNum <  numCols
          ) { // if this is a valid position in the results table, i.e. not off the edge
            results[resultsRowNum][resultsColNum] += 1; // then increment the value found there
          }
        });
      }
    });
  });
  return results;
}


let matrix;

matrix = [[true,false,false],[false,true,false],[false,false,false]];
console.log(JSON.stringify(matrix));
console.log(JSON.stringify(minesweeper(matrix)));

console.log('');

matrix = [[false,false,false], [false,false,false]];
console.log(JSON.stringify(matrix));
console.log(JSON.stringify(minesweeper(matrix)));

console.log('');

matrix = [[true,false,false,true], [false,false,true,false], [true,true,false,true]];
console.log(JSON.stringify(matrix));
console.log(JSON.stringify(minesweeper(matrix)));

【讨论】:

    【解决方案2】:
    function minesweeper(matrix) {
    var solution=[];
    for( var i =0; i < matrix.length; i++){
        var inner=[];
        solution.push(inner);
        for(var j = 0; j < matrix[i].length; j++){
            var count=0;
            if(matrix[i] && matrix[i][j]) count++;//at this position
            if(matrix[i] && matrix[i][j-1]) count++;//one left
            if(matrix[i] && matrix[i][j+1]) count++;//one right
            if(matrix[i-1] && matrix[i-1][j]) count++;//one above
            if(matrix[i+1] && matrix[i+1][j]) count++;//one below
            inner.push(count);
        }
    }
    return solution;
    }
    

    您需要创建另一个数组来解析您的值。

    http://jsbin.com/siquxetuho/edit?console

    【讨论】:

    • 谢谢你,这更有意义!我试图通过 matrix[i+1][j+1] 来增加矩阵。我试图理解这个循环..如果 j 是矩阵 [0].length,那不是指 i 吗?我认为这是我的困惑 - 试图了解 j 如何成为第二行,依此类推
    • 顺便说一句,我尝试了这个解决方案,但它仍然抛出错误。输入:矩阵:[[true,false,false], [false,true,false], [false,false,false]] 输出:[[1,3,1], [3,1,2], [1 ,2,1]] 预期输出:[[1,2,1], [2,1,1], [1,1,1]] 输入:矩阵:[[false,false,false], [false, false,false]] 输出:[[1,1,1], [1,1,1]] 预期输出:[[0,0,0], [0,0,0]]
    猜你喜欢
    • 2019-12-24
    • 2016-05-05
    • 2020-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多