首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >N-2在Sudoku循环中做什么来验证块JavaScript

N-2在Sudoku循环中做什么来验证块JavaScript
EN

Stack Overflow用户
提问于 2022-01-21 17:07:01
回答 2查看 103关注 0票数 0

我当时正在研究Sudoku解决方案Validator算法的解决方案,并看到了这个例子。代码工作和验证,但我不明白的是,为什么在for循环中获得验证块,有一个N-2?如果N=9,董事会是9*9,那么为什么需要将其改为7呢?

当我移除-2并离开N时,我看不到控制台中的任何更改。

这是链接https://www.geeksforgeeks.org/check-if-given-sudoku-solution-is-valid-or-not/

谢谢你!!

代码语言:javascript
复制
<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>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-01-21 17:28:17

Sudoku包含子块,每个子块是3X3。

因此,代码在每个子块中的第一个单元上循环,然后在每个子块单元上迭代。

代码的作者添加了N-2条件,所以当他迭代子块单元时,Var X= i+k;他确保没有访问绑定单元格。

然而,当sudoku中的列数和行数是3的倍数时,这个检查是无用的。

这就是为什么当你删除-2的时候,你看不出有什么不同。

票数 0
EN

Stack Overflow用户

发布于 2022-01-21 17:22:39

这是因为代码正在按3的步骤工作。

代码语言:javascript
复制
for(var i = 0; i < N - 2; i += 3)

你可以看到我+= 3

所以i= 0,3,6

当N=9 -> 9-2 =7 -> 6大于7

在后面的代码中,您可以看到循环k& l,它处理的是3x3矩阵。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70805033

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档