Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper data structure to represent a Sudoku puzzle?

What would be a smart data structure to use to represent a Sudoku puzzle? I.e. a 9X9 square where each "cell" contains either a number or a blank.

Special considerations include:

  • Ability to compare across row, column, and in 3X3 "group
  • Ease of implementation (specifically in Python)
  • Efficiency (not paramount)

I suppose in a pinch, a 2D array might work but that seems to be a less than elegant solution. I just would like to know if there's a better data structure.

like image 749
Rafe Kettler Avatar asked Nov 01 '10 01:11

Rafe Kettler


People also ask

How do you represent a Sudoku graph?

Each row, column, or block of the Sudoku puzzle forms a clique in the Sudoku graph, whose size equals the number of symbols used to solve the puzzle. A graph coloring of the Sudoku graph using this number of colors (the minimum possible number of colors for this graph) can be interpreted as a solution to the puzzle.

Which algorithm can be used to solve Sudoku?

Stochastic search / optimization methods Sudoku can be solved using stochastic (random-based) algorithms. An example of this method is to: Randomly assign numbers to the blank cells in the grid.

How do you write an algorithm for Sudoku?

Using the backtracking algorithm, we will try to solve the Sudoku problem. When some cell is filled with a digit, it checks whether it is valid or not. When it is not valid, it checks for other numbers. If all numbers are checked from 1-9, and no valid digit found to place, it backtracks to the previous option.

What is the technique in solving a Sudoku puzzle?

There are more than a few techniques to solve a Sudoku puzzle, but per Conceptis Puzzles, the easiest way to a Sudoku solution is to, “Scan rows and columns within each triple-box area, eliminating numbers or squares and finding situations where only a single number can fit into a single square.” If you're looking to ...


2 Answers

Actually, I built such a beast, both a solver and a generator, and I used a 2D array. It worked fine.

You just had to understand the indexes and where they were and that wasn't too difficult to master.

The relative relationships between cells in a row doesn't change depending on the column, same goes for cells in a column, or even cells in a mini-square.

Sometimes, a less "elegant" solution is just fine. Indeed, sometimes, it's preferable :-)


For what it's worth, you may be interested in the algorithms that I used for the solver/generator.

First I wrote the solver part which would first set all cells as being able to be any value then apply all the rules in sequence to see if a individual cell could be solved or otherwise limited, things like:

  • if the cell was a specific value in the clues, set it to that value.
  • if there's only one cell left in a row (or column or mini-square), you can set it to the remaining value.
  • if a cell is marked as being possibly N and N exists in its row/column/mini-square elsewhere, remove that possibility.
  • if there are two cells in the row/column/mini-square and they have the same two possibilities (and no other possibilities), all other cells in that row/column/mini-square should have that possibility removed.

And so on, adding each rule that I use in solving the real puzzles.

For the generator, I started with:

123 456 789
456 789 123
789 123 456

234 567 891
567 891 234
891 234 567

345 678 912
678 912 345
912 345 678

and then, in a loop of varying size (at least 500), proceeded to swap rows and columns in such a way that it would never produce an invalid puzzle. In other words, swap rows or columns with the group they're in (for example, rows 1, 2 and 3 are a group, so are columns 4, 5 and 6).

This shuffled up the cells well enough to produce a decent puzzle.

Then, I started choosing random cells and setting them as unknown. Once a cell was set as unknown, I would pass the whole puzzle into the solver. If it was solvable, I would continue, otherwise I would re-instate the cell and carry on.

This prevented me getting a puzzle that was logically unsolvable.

Once a large number of random cell removals had been done, I would try to remove all the remaining cells in order using the same method. What was left then was the minimum amount of information required to solve the puzzle.

And, so it wasn't a pain to Sudoku beginners, I would allow them to specify a lower difficulty level which would put a certain number of the unnecessary cells back in.

Not a bad scheme, there may be better ones but that one worked fine for me.

Now, if I could only figure out this Kakuro stuff, I could die happy :-)

like image 63
paxdiablo Avatar answered Sep 30 '22 12:09

paxdiablo


Read Peter Norvig's essay Solving Every Sudoku Puzzle. You're unlikely to find a more elegant solution and you'll probably learn some new things about data structures, Python, and performance analysis in the process.

like image 42
Ned Deily Avatar answered Sep 30 '22 11:09

Ned Deily