2017-06-21 103 views
0

我試圖返回一個「矩陣」或二維數組,其中布爾值將變成1-4的數字,這取決於多少「真值「就在它旁邊。我之前嘗試了一種不同的方法,用以下代碼表示。迭代二維布爾數組並根據真/假返回一個遞增值

問題:

當矩陣= [[真,假假],[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]]

+0

有你有一個具體的問題?你能比「我的代碼有什麼問題嗎?」更具體嗎? – Necoras

+0

@Necoras我只是想弄清楚如何將布爾值轉換爲數字,並增加他們妥善基於真/假。我之前的代碼是沒有的 - 幾乎接近我需要的地方。 –

+0

布爾值已經是數字:0 - 假,1 - 真。至於搞清楚如何正確地附近的真/假值映射到1-4,我懷疑這是你的作業點,所以我會離開那裏搞清楚邏輯給你。但是,我會指出你正在修改初始的'矩陣'並返回它,而不是將你的新(1-4)值存儲在一個新的矩陣變量中。這永遠不會給你正確的值,因爲當你試圖計算第二個數值時,你已經改變了初始矩陣與起始條件不同。 – Necoras

回答

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; 
} 

您需要創建另一個數組來解析您的值。

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

+0

非常感謝!我試圖通過矩陣[i + 1] [j + 1]增加矩陣。我試圖理解這個循環..如果j是矩陣[0] .length,是不是指我?我認爲這是我的困惑 - 試圖瞭解j如何成爲第二排,等等 –

+0

順便說一句,我試過這個解決方案,它仍然拋出一個錯誤。輸入: 矩陣:[[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]] [012] ,0,0], [0,0,0]] –

0

更新:這是不是從你的問題清楚你是否想檢查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, 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)));

相關問題