我试图返回一个"矩阵"或者一个二维数组,其中布尔值根据多少" true"而变为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;
}
&#13;
我的错误/结果:
输入矩阵:[[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]]
答案 0 :(得分:1)
更新:从您的问题中无法确定是否要检查4个方向(例如北,西,南和东)或8个方向(北,西北,西,西南,南,东南) ,东和东北)。我最初的答案是4个方向。但是,我知道从您的预期结果中可以看出您可能想要8个方向,所以我已经重新编写了我的答案。
您提出问题的方式存在问题。您谈到改变原始矩阵,而不是例如返回带有结果的新矩阵。如果您在处理矩阵时实际更改矩阵,那么在您实际分析它们之前,您可能最终会更改某些值。例如,如果您分析左上角的单元格,找出它是真的,然后将单元格增加到相同原始表格中的右侧 ,那么第二个单元格将不再是它最初具有的true
或false
值,但现在将是您分配给该单元格的任何值(??? 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)));
&#13;
答案 1 :(得分:0)
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;
}
您需要创建另一个数组来解析您的值。