Sudoku solving algorithm C++

前端 未结 4 1684
没有蜡笔的小新
没有蜡笔的小新 2021-01-31 07:04

I\'m trying to make a Sudoku Solving program for a couple of days but I\'m stuck with the methods. I found this algorithm here but I don\'t really understand it:

相关标签:
4条回答
  • 2021-01-31 07:04

    The following assumes you are trying to solve a given board, not generate a puzzle.

    Basic (simple) approach

    Create a class whose objects can hold a board (here called board_t). This class may internally use array, but must support copying boards.

    Have a function void solve(board_t const& board); which repeats the following for each number n:

    • Copies your input
    • Enters n in the first empty cell of the copied board
    • If the copied board is a solution, print the solution and return.
    • Else If the board is still viable (e.g. no conflicts):
      • call solve(copied_board)

    Performance

    This is a recursive backtracking solution, which performs horribly for hard problems. You can significantly speed it up by proper pruning or deductive steps (e.g. if you end up with 8 numbers in a row after inserting one, you can immediately enter the ninth without any kind of search).

    Reasoning

    While certainly not an impressive technique, it has a high probability of working correctly, since you will only ever be modifying a copy to add a single value. This prevents corruption of your data structures (one problem your idea has is that it will destroy the numbers it finds when backtracking, are not necessarily the ones you just inserted, but may be part of the initial puzzle).

    Improving performance is quite simple, once you start picking more intelligent heuristics (e.g. instead of testing the square in order, you could pick the ones with the fewest remaining moves and try to get them out of the way - or do the reverse...) or start doing a bit of deduction and pruning.

    Note: The Algorithm Design Manual uses a Soduko solver to show the impact of these techniques on backtracking.

    0 讨论(0)
  • 2021-01-31 07:12

    There is one very important modification to recursive algorithms: Use most constrained first approach. This means first to solve a cell with smallest number of possible candidates (when direct row/column/block conflicts are removed).

    Another modification is: Change the board in-place; do not copy it. In each recursive call you modify only one cell on the board, and that cell used to be empty. If that call doesn't end up in a solved board somewhere down the recursive call tree, just clear the cell again before returning - this returns the board into original state.

    You can find a very short and fast solution in C# on address: Sudoku Solver. It solves arbitrary sudoku board in about 100 steps only, all thanks to the most constrained first heuristic.

    0 讨论(0)
  • 2021-01-31 07:25

    Suggested Approach

    1. Implement a generic graph search algorithm
      • could use either IDFS or A* graph search
        • I would prefer the second
      • do this for a general directed graph
        • node type TNode
        • node successor function TNode => vector<TNode>
    2. Define your Sudoku states
      • a state is a 9x9 array with a number 1, 2, ..., or 9 or a blank in each position
    3. Define what a goal Sudoku state is
      • all 81 cells filled in
      • all 9 rows have numbers {1, 2, ..., 9} in them
      • all 9 columns have numbers {1, 2, ..., 9} in them
      • all 9 3x3 squares have numbers {1, 2, ..., 9} in them
    4. Define your valid Sudoku state successor function
      • a state S can have number N added at row I, column J if:
        • cell (I,J) is empty
        • there is no other N in row I
        • there is no other N in column J
        • there is no other N in the 3x3 square containing (I,J)
      • the state successor function maps a state S to the vector of states that satisfy these rules
    5. Apply your generic graph search algorithm (1) to the Sudoku state graph (2-4)
    6. (optional) If you do choose to use A* graph search, you can also define a heuristic on your Sudoku state space to potentially drastically increase performance
      • how to design the heuristic is another whole problem, that's more of an art than a science

    Current Approach

    Your current approach mixes the specification of the graph to be searched and the implementation of the search algorithm. You're going to have a lot of difficulty if you mix those two. This problem naturally separates into two distinct pieces -- the algorithm and the graph -- so you can and should exploit that in your implementation. It will make it much simpler.

    The other benefit you get if you go with this separation is that you will be able to reuse your graph search algorithm on a huge number of problems - very cool!

    0 讨论(0)
  • 2021-01-31 07:26

    This is a classic Constraint Satisfaction Problem. I recommend doing some research on the topic to figure out the successful strategy. You will need to use AC-3 ( Arc Consistency 3) algorithm along with the backtracking techniques to solve the problem.

    0 讨论(0)
提交回复
热议问题