C#递归编程:跳到代码的上一个步骤

发布于 2025-01-29 06:06:57 字数 915 浏览 2 评论 0原文

我已经获得了编写一个Sudoku程序的任务,该程序解决了Sudoku Wikipedia示例。 解决Sudoku难题的方法必须是一种递归方法。 它首先检查了Sudoku板是否有效(Sudoku规则:在同一行,列和框中没有两次数字),然后获取可以写入的第一个空字段,并在字段中写入第一个值(数字1)。之后,该方法再次自称(递归)。

我的方法看起来像这样:

private bool SolveSudoku(int[,] board)
    {
        // if board not valid, return false
        if (!CheckBoardValid(board))
        {
            return false;
        }

        // if board full, return true
        var nextField = GetFirstEmpty(board);
        if (nextField is null)
        {
            return true;
        }

        for (int value = 1; value < 10; value++)
        {
            board[nextField.Value.X, nextField.Value.Y] = value;
            var solved = SolveSudoku(board);
            // if solved, do nothing
            // if not solved, undo this step and try writing next number (value++) in field
        }
        return false;
    }

如果这是无效的,我该如何撤消最后一步?

I have been given the task to write a sudoku program, which solves the sudoku wikipedia example.
The method which solves the sudoku puzzle has to be a recursive method.
It first checks if the sudoku board is valid (sudoku rules: no twice occuring numbers in same row, column and box), then gets the first empty field it can write in and writes the first value (number 1) in field. After that, the method calls itself again (recursion).

My method looks like this:

private bool SolveSudoku(int[,] board)
    {
        // if board not valid, return false
        if (!CheckBoardValid(board))
        {
            return false;
        }

        // if board full, return true
        var nextField = GetFirstEmpty(board);
        if (nextField is null)
        {
            return true;
        }

        for (int value = 1; value < 10; value++)
        {
            board[nextField.Value.X, nextField.Value.Y] = value;
            var solved = SolveSudoku(board);
            // if solved, do nothing
            // if not solved, undo this step and try writing next number (value++) in field
        }
        return false;
    }

How can I undo the last step, if it is not valid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2025-02-05 06:06:57

剥离相关部分:

for (int value = 1; value < 10; value++)
{
    board[nextField.Value.X, nextField.Value.Y] = value;
    if(SolveSudoku(board)) return true;
    // ^^ Stop if solved,
    // iteration will increase value if not.
}
// reset and return false if no value for this field leads to a solution.
board[nextField.Value.X, nextField.Value.Y] = 0;
return false;

Snipping out the relevant part:

for (int value = 1; value < 10; value++)
{
    board[nextField.Value.X, nextField.Value.Y] = value;
    if(SolveSudoku(board)) return true;
    // ^^ Stop if solved,
    // iteration will increase value if not.
}
// reset and return false if no value for this field leads to a solution.
board[nextField.Value.X, nextField.Value.Y] = 0;
return false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文