2013-11-04 37 views
1

今天我想寫的是解決了一個數獨的程序。 當我的方法沒有工作時,我訴諸於此處找到的解決方案:http://www.heimetli.ch/ffh/simplifiedsudoku.html異常#1在遞歸

但由於某些原因,我不斷收到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; 
    } 
} 

}

+2

有什麼異常堆棧跟蹤?這應該讓你知道發生了什麼。此外,請考慮調試代碼,以查看程序無法正常工作的位置。 – Servy

回答

3

例如這裏

  return solveSudoku(row++, 0); 

row原始值傳遞給函數solveSudoku(和row後遞增)。將其更改爲row + 1

+0

非常感謝。不幸的是,這總是很小的事情。 – Smoerrebroed

2

的原因StackOverflowException是在下一()方法。更改

return solveSudoku(row, col); 

return solveSudoku(row, ++col); 

這並不至少解決StackOverflowException。無論如何,你需要進一步提升算法,因爲它並沒有解決數獨完全還。