gpt4 book ai didi

javascript - 迭代 boolean 值的二维数组并返回基于 true/false 的递增值

转载 作者:行者123 更新时间:2023-12-03 04:02:03 25 4
gpt4 key购买 nike

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

问题:

When matrix = [[true, false, false], [false, true, false],[false, false, false]]

the output should be [[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;
}

我的错误/结果:

Input matrix: [[true,false,false],[false,true,false],[false,false,false]]

Output: [[2,1,1], [1,2,1], [1,1,1]]

Expected Output: [[1,2,1], [2,1,1], [1,1,1]]

Input matrix: [[false,false,false], [false,false,false]]

Output:[[1,1,1], [1,1,1]]

Expected Output:[[0,0,0], [0,0,0]]

Input matrix: [[true,false,false,true], [false,false,true,false], [true,true,false,true]]

Output:[[2,1,1,2], [1,1,2,1], [2,2,1,2]]

Expected Output:[[0,2,2,1], [3,4,3,3], [1,2,3,1]]

最佳答案

更新:从您的问题中并不清楚您是要检查 4 个方向(例如北、西、南和东)还是 8 个方向(北、西北、西、西南、南、东南、东)和东北)。我最初的答案是4个方向。不过,我知道从您的预期结果来看,您可能需要 8 个方向,因此我针对该场景重写了答案。

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

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

关于javascript - 迭代 boolean 值的二维数组并返回基于 true/false 的递增值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44680252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com