Programming Design Help - How to Structure a Sudoku Solver program? [closed]

狂风中的少年 提交于 2019-11-30 10:37:11

Don't over-engineer it. It's a 2-D array or maybe a Board class that represents a 2-D array at best. Have functions that calculate a given row/column and functions that let you access each square. Additional methods can be used validate that each sub-3x3 and row/column don't violate the required constraints.

Well, I would use one class for the sudoku itself, with a 9 x 9 array and all the functionality to add numbers and detect errors in the pattern.

Another class will be used to solve the puzzle.

The simplest way to do it is to represent the board by a 2D 9x9 array. You'll want to have references to each row, column and 3x3 box as a separate object, so storing each cell in a String makes more sense (in Java) than using a primitive. With a String you can keep references to the same object in multiple containers.

Do you need to do it in Python or Java? I do a lot of programming in Python, but this can be done much more concisely with integer program using a language like AMPL or GLPK, which I find more elegant (and generally more efficient) for problems like this.

Here it is in AMPL, although I haven't verified how this works: http://taha.ineg.uark.edu/Sudoku.txt

PierrOz

just for fun, here is what is supposed to be the shortest program, in python, that can solve a sudoku grid:

def r(a):i=a.find('0') if i<0:print a [m in[(i-j)%9*(i/9^j/9)*(i/27^j/27|i%9/3^j%9/3)or a[j]for j in range(81)]or r(a[:i]+m+a[i+1:])for m in`14**7*9`]r(raw_input())

hmm ok it's quite cryptic and I don't think it matchs your question so I apologize for this noise :)

Anyway you'll find some explanation of these 173 characters here. There's also an explanation in french here

Maybe a design that had a box per square, and another class to represent the puzzle itself that would have a collection of boxes, contain all the rules for box interactions, and control the overall game would be a good design.

First, it looks like there are two kinds of cells.

  • Known calls; those with a fixed value, no choices.

  • Unknown cells; those with a set of candidate values that reduces down to a single final value.

Second, there are several groups of cells.

  • Horizontal rows and Vertical columns which must have one cell of each value. That constraint is used to remove values from various cells in the row or column.

  • 3x3 blocks which must have one cell of each value. That constraint is used to remove values from various cells in the block.

Finally, there's the overall grid. This has several complementary views.

  • It's 81 cells.

  • The cells are also collected into a 3x3 grid of 3x3 blocks.

  • The cells are also collected into 9 columns.

  • The cells are also collected into 9 rows.

And you have a solver strategy object.

  1. Each Unknown cell it set to having set( range(1,10) ) as the candidate values.

  2. For each row, column and 3x3 block (27 different collections):

    a. For each cell:

    • If it has definite value (Known cells and Unknown cells implement this differently): remove that value from all other cells in this grouping.

The above must be iterated until no changes are found.

At this point, you either have it solved (all cells report a definite value), or, you have some cells with multiple values. Now you have to engage in a sophisticated back-tracking solver to find a combination of the remaining values that "works".

A class containing a 1d array of 81 ints (0 is empty) is sufficient for the rule class. The rule class enforces the rules (no duplicate numbers in each row, column or 3x3 square). It also has an array of 81 bools so it knows which cells are fixed and which need to be solved. The public interface to this class has all the methods you need to manipulate the board:

int getCell(int x, int y);
bool setCell(int x, int y, int value);
bool clearCell(int x, int y);
int[] getRow(int x);
int[] getCol(int y);
int[] getSubBox(int x, int y);
void resetPuzzle();
void loadPuzzle(InputStream stream);

Then your solver uses the public interface to this class to solve the puzzle. The class structure of the solver I presume is the purpose of writing the 5 millionth Sudoku solver. If you are looking for hints, I'll edit this post later.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!