Sudoku Naive-solver
You'll be creating a program called sudoku-naive.py
which will be called with either python or python3 (depending upon the "#!"
line) as follows:
python3 sudoku-naive.py <input-filename>
<output-filename> <name-of-sudoku-board>
The input-filename will probably be "Sudoku-boards.txt".
So, for instance, if the command line was:
python3 sudoku-naive.py Sudoku-boards.txt s-1.txt
A2-1,Medium-NYTimes,unsolved
... then you should output to "s-1.txt":
A2-1,Medium-NYTimes,solved
2,5,4,6,9,7,8,3,1
6,8,3,2,5,1,7,4,9
9,7,1,4,8,3,5,6,2
7,6,8,5,3,9,2,1,4
4,2,5,1,6,8,3,9,7
3,1,9,7,4,2,6,5,8
1,3,6,8,7,4,9,2,5
5,4,7,9,2,6,1,8,3
8,9,2,3,1,5,4,7,6
Note that there are no spaces or brackets "[ ]" in the output.
Also note that the word "unsolved" in the command-line
argument, has been changed to "solved" in the output file
Tips:
I strongly suggest copying and pasting into your code the Cliques list and
Neighbors dictionary from Sudoku-Info.html
- The naive solver should do the following (as discussed in class): (Imagine that we're in the middle of the algorithm...)
- go to the next open cell. If there aren't any left, we've found a
solution. Clean up and write out.
- Forced? see if the cell is "forced" into a single number, i.e. its
neighbors have 8 numbers used, and one number unused, and therefore this
cell must have the unused number. If so, use that number and go to #1
- Guess next? try guessing "1" and check if invalid with all of the
cell's neighbors. If invalid, then try "2", etc. Let's say "3"
works. Then record that you've used the "3" and the current state
of the board, and store (probably in a stack), because we may have to
backtrack to here, if the "3" turns out to be a bad guess.
Then go to #1
- If you've tried all numbers from "1" to "9", and none of them work,
then we've reached a contradiction. Backtrack to the most recent
guess, restore its board, and try the next number guess from there.
- Increment a backtracking counter, to keep track of how many times you've
backtracked. This will eventually measure the goodness of your
algorithm.