繁体   English   中英

遍历2D布尔数组,并基于true / false返回增加的值

[英]Iterate over 2D array of booleans and return an incremented value based on true/false

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

问题:

当矩阵= [[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]]

更新:您的问题尚不清楚,您是否要检查4个方向(例如,北,西,南和东)或8个方向(北,西北,西,西南,南,东南,东和东北)。 我最初的答案是4个方向。 但是,从您的预期结果中我知道您可能想要8个方向,因此我针对该情况重新编写了答案。

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

无论如何,一种解决方法是从与原始矩阵表大小相同的结果表开始,但所有值最初都设置为零。 然后,您可以遍历输入表中的所有单元格,在结果表中输入表中初始对应单元格的右侧,下方,左侧和上方的位置加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, ie 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))); 

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM