我正在研究數獨解決方案驗證器演算法的解決方案,并遇到了這個例子。代碼可以作業并驗證,但我不明白為什么在 for 回圈中驗證一個塊有一個 N-2?如果 N = 9 且棋盤為 9 * 9,那么為什么需要將其更改為 7?
當我洗掉 -2 并離開 NI 時,在我的控制臺中看不到任何變化。
這是鏈接https://www.geeksforgeeks.org/check-if-given-sudoku-solution-is-valid-or-not/
謝謝!!
<script>
// JavaScript program to implement
// the above approach
var N = 9;
// Function to check if all elements
// of the board[][] array store
// value in the range[1, 9]
function isinRange(board)
{
// Traverse board[][] array
for(var i = 0; i < N; i )
{
for(var j = 0; j < N; j )
{
// Check if board[i][j]
// lies in the range
if (board[i][j] <= 0 ||
board[i][j] > 9)
{
return false;
}
}
}
return true;
}
// Function to check if the solution
// of sudoku puzzle is valid or not
function isValidSudoku(board)
{
// Check if all elements of board[][]
// stores value in the range[1, 9]
if (isinRange(board) == false)
{
return false;
}
// Stores unique value
// from 1 to N
var unique = Array(N 1).fill(false);
// Traverse each row of
// the given array
for(var i = 0; i < N; i )
{
unique = Array(N 1).fill(false);
// Traverse each column
// of current row
for(var j = 0; j < N; j )
{
// Stores the value
// of board[i][j]
var Z = board[i][j];
// Check if current row
// stores duplicate value
if (unique[Z])
{
return false;
}
unique[Z] = true;
}
}
// Traverse each column of
// the given array
for(var i = 0; i < N; i )
{
// Initialize unique[]
// array to false
unique = Array(N 1).fill(false);
// Traverse each row
// of current column
for(var j = 0; j < N; j )
{
// Stores the value
// of board[j][i]
var Z = board[j][i];
// Check if current column
// stores duplicate value
if (unique[Z])
{
return false;
}
unique[Z] = true;
}
}
// Traverse each block of
// size 3 * 3 in board[][] array
for(var i = 0; i < N - 2; i = 3) //<====== what is the point of N-2? What is it doing?
{
// j stores first column of
// each 3 * 3 block
for(var j = 0; j < N - 2; j = 3) //<====== what is the point of N-2? What is it doing?
{
// Initialize unique[]
// array to false
unique = Array(N 1).fill(false);
// Traverse current block
for(var k = 0; k < 3; k )
{
for(var l = 0; l < 3; l )
{
// Stores row number
// of current block
var X = i k;
// Stores column number
// of current block
var Y = j l;
// Stores the value
// of board[X][Y]
var Z = board[X][Y];
// Check if current block
// stores duplicate value
if (unique[Z])
{
return false;
}
unique[Z] = true;
}
}
}
}
// If all conditions satisfied
return true;
}
// Driver Code
var board = [ [ 7, 9, 2, 1, 5, 4, 3, 8, 6 ],
[ 6, 4, 3, 8, 2, 7, 1, 5, 9 ],
[ 8, 5, 1, 3, 9, 6, 7, 2, 4 ],
[ 2, 6, 5, 9, 7, 3, 8, 4, 1 ],
[ 4, 8, 9, 5, 6, 1, 2, 7, 3 ],
[ 3, 1, 7, 4, 8, 2, 9, 6, 5 ],
[ 1, 3, 6, 7, 4, 8, 5, 9, 2 ],
[ 9, 7, 4, 2, 1, 5, 6, 3, 8 ],
[ 5, 2, 8, 6, 3, 9, 4, 1, 7 ] ];
if (isValidSudoku(board))
{
document.write("Valid");
}
else
{
document.write("Not Valid");
}
</script>
uj5u.com熱心網友回復:

數獨包含子塊,每個子塊都是 3X3
所以代碼回圈遍歷每個子塊中的第一個單元格,然后迭代每個子塊單元格。
代碼的作者添加了 N-2 條件,因此當他迭代子塊單元時 Var X = i k; 他確保他不會訪問超出限制的單元格。
但是,當數獨中的列數和行數是 3 的倍數時,此檢查毫無用處。
這就是為什么當你洗掉 -2 時你看不到任何區別。
uj5u.com熱心網友回復:
這是因為代碼按 3 步運行
for(var i = 0; i < N - 2; i = 3)
你可以看到 i = 3
所以我 = 0, 3, 6
因為 N = 9 -> 9-2 = 7 -> 6 大于 7
稍后在代碼中你可以看到回圈 k & l,這是處理 3x3 矩陣
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/417897.html
標籤:
