c# - 递归中的 Stackoverflow 异常

标签 c# recursion sudoku stack-overflow

今天我想写一个解决数独的程序。 当我的方法不太奏效时,我求助于此处找到的解决方案:http://www.heimetli.ch/ffh/simplifiedsudoku.html

但出于某种原因,我不断收到 StackOverflow 异常。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace Sudoku_Solver
{
class Program
{
    static void Main(string[] args)
    {
        MainClass _mainClass = new MainClass();

        _mainClass.printSudoku();

        Console.ReadLine();
    }
}

class MainClass
{
    private const int FieldsVertical = 3, FieldsHorizontal = 3;
    private const int RowsInField = 3, ColumnsInField = 3;

    Thread _sudokuSolverThread;

    private int[,] Sudoko = new int[(FieldsVertical * ColumnsInField), (FieldsHorizontal * RowsInField)]{
        { 4, 0, 2, 0, 3, 0, 0, 0, 0},
        { 7, 0, 8, 0, 4, 2, 0, 9, 0},
        { 0, 0, 0, 8, 0, 5, 4, 0, 0},
        { 0, 8, 0, 0, 0, 4, 2, 0, 9},
        { 0, 9, 4, 2, 0, 6, 8, 1, 0},
        { 6, 0, 1, 7, 0, 0, 0, 3, 0},
        { 0, 0, 9, 5, 0, 3, 0, 0, 0},
        { 0, 3, 0, 4, 6, 0, 7, 0, 2},
        { 0, 0, 0, 0, 2, 0, 9, 0, 3},
    };

    public MainClass(){ }

    private void startSudokuSolver()
    {
        solveSudoku(0, 0);
    }

    private bool solveSudoku(int row, int col)
    {
        if (Sudoko[row, col] != 0)
        {
            return next(row, col++);
        }
        else
        {
            for (int i = 0; i < ColumnsInField * RowsInField; i++)
            {
                if (checkColumn(i, row) && checkField(i, row, col) && checkRow(i, col))
                {
                    Sudoko[row, col] = i;

                    //Thread.Sleep(10);

                    return next(row, col++);
                }
            }
            Sudoko[row, col] = 0;

            return false;
        }
    }

    private bool next(int row, int col)
    {
        if (row == 9)
        {
            return false;
        }
        else
        {
            if (col == 9)
            {
                return solveSudoku(row++, 0);
            }
            else
            {
                return solveSudoku(row, col);
            }
        }
    }

    public void printSudoku()
    {
        startSudokuSolver();

        for (int i = 0; i < Sudoko.GetLength(0); i++)
        {
            for (int x = 0; x < Sudoko.GetLength(1); x++)
            {
                Console.Write(Sudoko[i, x] + " ");
            }
            Console.Write(Environment.NewLine);
        }
    }

    private bool checkRow(int number, int col)
    {
        for (int row = 0; row < FieldsVertical * RowsInField; row++)
        {
            if (Sudoko[row, col] == number)
            {
                return false;
            }
        }

        return true;
    }

    private bool checkColumn(int number, int row)
    {
        for (int column = 0; column < FieldsHorizontal * ColumnsInField; column++)
        {
            if (Sudoko[row, column] == number)
            {
                return false;
            }
        }
        return true;
    }

    int _currentFieldRow;
    int _currentFieldColumn;

    private bool checkField(int number, int row, int col)
    {
        _currentFieldRow = (row / RowsInField) * RowsInField;
        _currentFieldColumn = (col / ColumnsInField) * ColumnsInField;

        for (int a = _currentFieldRow; a < _currentFieldRow + RowsInField; a++)
        {
            for (int b = _currentFieldColumn; b < _currentFieldColumn + ColumnsInField; b++)
            {
                if (Sudoko[a, b] == number)
                {
                    return false;
                }
            }
        }
        return true;
    }
}

最佳答案

例如这里

            return solveSudoku(row++, 0);

row 的原始值被传递给函数 solveSudoku(然后 row 递增)。将其更改为 row + 1

关于c# - 递归中的 Stackoverflow 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19771629/

相关文章:

java - 从数独矩阵的每一行和每一列中随机删除一个元素

java - Java问题中的蛮力数独求解器算法

c# - Roslyn - 如何可靠地格式化类的空白

c# - WebForms 中的键盘快捷键

.net - .Net 中的递归/反射 XSLT

c - 不清楚汉诺塔的递归调用

c# - 更新 mdb 数据库表

c# - 使用没有 certificateValidationMode 的证书时出现 WCF SecurityNegotiationException

tsql - 如何使用hierarchyid sql显示所有递归结果

Java Swing 图形