# Project Euler 96: Devise an algorithm for solving Su Doku puzzles

I was all excited when I first saw the headline of Problem 96 of Project Euler  since I love solving SuDoku puzzles. And I wasn’t disappointed when I saw the problem description which reads

Su Doku (Japanese meaning number place) is the name given to a popular puzzle concept. Its origin is unclear, but credit must be attributed to Leonhard Euler who invented a similar, and much more difficult, puzzle idea called Latin Squares. The objective of Su Doku puzzles, however, is to replace the blanks (or zeros) in a 9 by 9 grid in such that each row, column, and 3 by 3 box contains each of the digits 1 to 9. Below is an example of a typical starting puzzle grid and its solution grid.

 0 0 3 9 0 0 0 0 1 0 2 0 3 0 5 8 0 6 6 0 0 0 0 1 4 0 0 0 0 8 7 0 0 0 0 6 1 0 2 0 0 0 7 0 8 9 0 0 0 0 8 2 0 0 0 0 2 8 0 0 0 0 5 6 0 9 2 0 3 0 1 0 5 0 0 0 0 9 3 0 0
 4 8 3 9 6 7 2 5 1 9 2 1 3 4 5 8 7 6 6 5 7 8 2 1 4 9 3 5 4 8 7 2 9 1 3 6 1 3 2 5 6 4 7 9 8 9 7 6 1 3 8 2 4 5 3 7 2 8 1 4 6 9 5 6 8 9 2 5 3 4 1 7 5 1 4 7 6 9 3 8 2

A well constructed Su Doku puzzle has a unique solution and can be solved by logic, although it may be necessary to employ “guess and test” methods in order to eliminate options (there is much contested opinion over this). The complexity of the search determines the difficulty of the puzzle; the example above is considered easy because it can be solved by straight forward direct deduction.

The 6K text file, sudoku.txt (right click and ‘Save Link/Target As…’), contains fifty different Su Doku puzzles ranging in difficulty, but all with unique solutions (the first puzzle in the file is the example above).

By solving all fifty puzzles find the sum of the 3-digit numbers found in the top left corner of each solution grid; for example, 483 is the 3-digit number found in the top left corner of the solution grid above.

Ok, I have cheated a bit since I dabbled into making a solver for Sudoku puzzles a while ago, so I decided to reuse it for this solution. I have implemented a brute force solver as well as implemented logic limit the number of solutions.

One of the best places I have found for reading about solving strategies is here. He lists several advanced solving strategies though he misses some of the basic ones which I will use here.  But let’s jump right into the Brute force solution.

## The code

I wont show the code here, as it is pretty long. But I want to give you a short primer to the files, so you can find your way around it.  You can download the source code here.

Grid.cs – Just a simple 9×9 grid of integers which will be used to give the solution

PossibleGrid.cs – A class I have made which makes a 9×9 grid where each cell can hold all the numbers 1-9. There are methods in the file to fix one cell and remove that number as a possible solution for other cells. So if I fix the upper left corner to 3 that is not a possible solution for the first row, column or box so those are removed as possible solutions. This file is mainly good for logic solutions, but speeds up the back tracking algorithm by keeping track of possible solutions.

Backtrack.cs – This contains the algorithm for backtrakcing as described earlier.

Run.cs – This contains the main method which reads the input file, does the timing and so on.

LogicSover.cs – contains one method for solving a sudoku by logic. It wont solve all Sudokus but it does reduce the number of possible solutions for most. You could implement more strategies in this file if you like.

## Brute force solution

Creating a brute force solver for Sudoku is a pretty easy thing. It is usually called the back tracking algorithm. You start at the first cell and fill in the smallest valid number for the first cell. Then move to the next cell and repeat. You do this until you reach a cell with no valid entries.

When you encounter a cell with no valid solution you move back and increase the previous cell to the next lowest valid number. If such a number does not exist you move back until you encounter a cell you can change and then start moving forward again. It is a simple strategy, and often more than fast enough.

I have seen an example where the first row has the solution 9 8 7…. and that means we need to check almost all solutions before finding the correct one. For the average solution this is pretty okay though.

Running the code with only the backtracking algorithm gives us

```The sum of 3 leftmost digits are 24702
Solution took 301,5991 ms
```

So it is fairly okay, but not as good as it could be.

## Adding a bit of logic

I am pretty sure you will laugh when you hear the strategy I have employed to speed up the Sudoku solving. It is really very simple.

The strategy is as follows. I will go through a row to see if a number can only be placed in one cell in that row. There might be multiple valid solutions for each cell, but we can encounter a row where one of the cells has the valid solutions 1,2,3 and 5. But since 5 is not a valid solution for any other cell then we can fix this cell to 5. Once we fix a cell we should remove that option as a possible solution for the box and column.  This can be repeated for columns and boxes.

I told you it was simple, but is is effective. Adding this solution strategy gives us an execution time of

```The sum of 3 leftmost digits are 24702
Solution took 32,6672 ms
```

Or about 10% of the brute force solution. In fact there are  40 of the given Sudokus which are completely solved by this method.

## Wrapping up

The code for this was a reuse of some old code I had, and therefore somewhat longer than the usual solutions. You can download it right here.

The blog image is from Andreas Marx who solved a sudoku while drinking tea and decided to share that under the Creative Commons license.

### Posted by Kristian

Bjarki Ágúst

Your code is longer than mine, but also much faster (and cleaner).
Mine runs in about 1.5 seconds:

```private class Sudoku
{
public int[,] Board { get; set; }

public Sudoku(int[,] board)
{
this.Board = board;
}

private static int[] Numbers = 1.To(9).ToArray();

public void Solve()
{
bool anySolved, anyFound;

do
{
anySolved = anyFound = false;

for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
if (this.Board[x, y] != 0) continue;
anyFound = true;
IEnumerable<int> remaining = this.Remaining(x, y);

int fst = remaining.FirstOrDefault();
if (fst == 0) return;

if (!remaining.Skip(1).Any())
{
this.Board[x, y] = fst;
anySolved = true;
}
}
}
} while (anyFound && anySolved);

if (anyFound)
{
this.SolveBruteForce();
}
}

public IEnumerable<int> Horizontal(int x)
{
for (int y = 0; y < 9; y++) if (this.Board[x, y] != 0) yield return this.Board[x, y];
}

public IEnumerable<int> Vertical(int y)
{
for (int x = 0; x < 9; x++) if (this.Board[x, y] != 0) yield return this.Board[x, y];
}

public IEnumerable<int> Box(int x, int y)
{
x = (x / 3) * 3;
y = (y / 3) * 3;

for (int xd = 0; xd < 3; xd++)
{
for (int yd = 0; yd < 3; yd++)
{
if (this.Board[x + xd, y + yd] != 0) yield return this.Board[x + xd, y + yd];
}
}
}

public bool Solved
{
get
{
for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
if (this.Board[x, y] == 0) return false;
}
}

return true;
}
}

public IEnumerable<int> Remaining(int x, int y)
{
return Numbers.Except(this.Horizontal(x).Concat(this.Vertical(y)).Concat(this.Box(x, y)));
}

public void SolveBruteForce()
{
int min = -1, minX = 0, minY = 0;

for (int x = 0; x < 9; x++)
{
for (int y = 0; y < 9; y++)
{
if (this.Board[x, y] != 0) continue;

var cur = Remaining(x, y);
int count = cur.Count();
if (min == -1 || count < min)
{
minX = x;
minY = y;
min = count;
}

if (min == 2) break;
}

if (min == 2) break;
}

foreach (int rem in Remaining(minX, minY))
{
int[,] cur = (int[,])this.Board.Clone();
this.Board[minX, minY] = rem;
this.Solve();
if (this.Solved) return;
this.Board = cur;
}
}
}

[Problem("96", Description = "Devise an algorithm for solving Su Doku puzzles.")]
public void P96()
{
var lines = File.ReadAllLines("Problem96.txt").Where(l => l[0] != 'G').Skip(0 * 9);
int sum = 0;

while (lines.Any())
{
Sudoku s = new Sudoku(lines.Take(9).Select(l => l.Select(c => c - '0').ToArray()).ToArray().ToMultidimensional());
s.Solve();
sum += s.Board[0, 0] * 100 + s.Board[0, 1] * 10 + s.Board[0, 2];
lines = lines.Skip(9);
}

this.WriteLine(sum);
}```
Kristian

Yep, as I mentioned the main part of the code was written prior to solving this problem. So basically I just reused the code which I had not written with the intention of being small.

Bjarki Ágúst
Jean-Marie Hachey

Hard to find …

A sudoku that cannot be solved with this application:
http://www.solution-sudoku.com/Default.aspx

Christina

One optimization that worked quite well in my implementation was to order the positions by how many possible digits they allow and backtrack over that (the list is recomputed at each backtracking step, when moving forward to the next position to tackle).

Antonio

A further optimization: we are only asked for the first 3 digits (top-left) of the solution. You do not need to solve the entire puzzle, only enough so that you are certain of those three squares.

Carl J Appellof

Once again, I’m late to the game on commenting. I’ve been enjoying playing sudoku for a long time, and have several favorite strategies, which I coded up. Then ran into a paper by J. F. Crook on the topic of using “preemptive sets” to solve more difficult puzzles. http://www.ams.org/notices/200904/rtx090400460p.pdf
Actually, pretty simple to implement once I figured it out. That, combined with a backtracking method and my original methods resulted in a very fast solution:
Took 2.031425 ms
50 wins and 0 losses
Sum = 24702

I think my code does some extra work that’s unnecessary, so the solution might be sped up a little. I only found 4 of the puzzles needed backtracking at all!

Nathan Shurtz

If you ask me it seems that rather than coding the back tracking, you should let the language handle that for you. Prolog is an excellent example of a language that will do this for you. You have to define the rules of sudoku, but those are pretty straight forward. There is a template file hosted here. Biggest down side of this is that it requires you to learn prolog, but I think it is worth it to play around with languages in the different language paradigms (OO, Functional, Logic, etc.) as each has their own benefits.

Biggest challenge I had with using Prolog was actually taking the puzzles provided and turning them into something that I could give my prolog program.
Below is that translation. If you run superTest() you will see all of the solutions print out. Summing the 3 digit numbers and coding the solver are left as an exercise to the reader. (NOTE: I did not use prolog to sum the numbers. I ended up parsing the solved puzzles using regular expressions and my text editor’s multi-line edit functionality to produce a csv of the 3 digit numbers and summed them using excel, but it should be possible to do in prolog.)
``` test11(Board1) :- Board1 = [[_, _, 3, _, 2, _, 6, _, ], [9, _, _, 3, _, 5, _, _, 1], [, _, 1, 8, _, 6, 4, _, ], [, _, 8, 1, _, 2, 9, _, ], [7, _, _, _, _, _, _, _, 8], [, _, 6, 7, _, 8, 2, _, ], [, _, 2, 6, _, 9, 5, _, ], [8, _, _, 2, _, 3, _, _, 9], [, _, 5, _, 1, _, 3, _, _]], solve(Board1), printBoard(Board1).```

``` test12(Board2) :- Board2 = [[2, _, _, _, 8, _, 3, _, ], [, 6, _, _, 7, _, _, 8, 4], [_, 3, _, 5, _, _, 2, _, 9], [_, _, _, 1, _, 5, 4, _, 8], [_, _, _, _, _, _, _, _, ], [4, _, 2, 7, _, 6, _, _, _], [3, _, 1, _, _, 7, _, 4, _], [7, 2, _, _, 4, _, _, 6, _], [, _, 4, _, 1, _, _, _, 3]], solve(Board2), printBoard(Board2). test13(Board3) :- Board3 = [[_, _, _, _, _, _, 9, _, 7], [_, _, _, 4, 2, _, 1, 8, ], [, _, _, 7, _, 5, _, 2, 6], [1, _, _, 9, _, 4, _, _, ], [, 5, _, _, _, _, _, 4, ], [, _, _, 5, _, 7, _, _, 9], [9, 2, _, 1, _, 8, _, _, ], [, 3, 4, _, 5, 9, _, _, _], [5, _, 7, _, _, _, _, _, _]], solve(Board3), printBoard(Board3). test14(Board4) :- Board4 = [[_, 3, _, _, 5, _, _, 4, ], [, _, 8, _, 1, _, 5, _, ], [4, 6, _, _, _, _, _, 1, 2], [, 7, _, 5, _, 2, _, 8, ], [, _, _, 6, _, 3, _, _, ], [, 4, _, 1, _, 9, _, 3, ], [2, 5, _, _, _, _, _, 9, 8], [, _, 1, _, 2, _, 6, _, ], [, 8, _, _, 6, _, _, 2, _]], solve(Board4), printBoard(Board4). test15(Board5) :- Board5 = [[_, 2, _, 8, 1, _, 7, 4, ], [7, _, _, _, _, 3, 1, _, _], [, 9, _, _, _, 2, 8, _, 5], [_, _, 9, _, 4, _, _, 8, 7], [4, _, _, 2, _, 8, _, _, 3], [1, 6, _, _, 3, _, 2, _, ], [3, _, 2, 7, _, _, _, 6, _], [, _, 5, 6, _, _, _, _, 8], [_, 7, 6, _, 5, 1, _, 9, _]], solve(Board5), printBoard(Board5). test16(Board6) :- Board6 = [[1, _, _, 9, 2, _, _, _, ], [5, 2, 4, _, 1, _, _, _, _], [, _, _, _, _, _, _, 7, ], [, 5, _, _, _, 8, 1, _, 2], [_, _, _, _, _, _, _, _, ], [4, _, 2, 7, _, _, _, 9, _], [, 6, _, _, _, _, _, _, ], [, _, _, _, 3, _, 9, 4, 5], [_, _, _, _, 7, 1, _, _, 6]], solve(Board6), printBoard(Board6). test17(Board7) :- Board7 = [[_, 4, 3, _, 8, _, 2, 5, ], [6, _, _, _, _, _, _, _, _], [, _, _, _, _, 1, _, 9, 4], [9, _, _, _, _, 4, _, 7, ], [, _, _, 6, _, 8, _, _, ], [, 1, _, 2, _, _, _, _, 3], [8, 2, _, 5, _, _, _, _, ], [, _, _, _, _, _, _, _, 5], [_, 3, 4, _, 9, _, 7, 1, _]], solve(Board7), printBoard(Board7). test18(Board8) :- Board8 = [[4, 8, _, _, _, 6, 9, _, 2], [_, _, 2, _, _, 8, _, _, 1], [9, _, _, 3, 7, _, _, 6, ], [8, 4, _, _, 1, _, 2, _, _], [, _, 3, 7, _, 4, 1, _, ], [, _, 1, _, 6, _, _, 4, 9], [_, 2, _, _, 8, 5, _, _, 7], [7, _, _, 9, _, _, 6, _, _], [6, _, 9, 2, _, _, _, 1, 8]], solve(Board8), printBoard(Board8). test19(Board9) :- Board9 = [[_, _, _, 9, _, _, _, _, 2], [_, 5, _, 1, 2, 3, 4, _, ], [, 3, _, _, _, _, 1, 6, ], [9, _, 8, _, _, _, _, _, _], [, 7, _, _, _, _, _, 9, ], [, _, _, _, _, _, 2, _, 5], [_, 9, 1, _, _, _, _, 5, ], [, _, 7, 4, 3, 9, _, 2, _], [4, _, _, _, _, 7, _, _, _]], solve(Board9), printBoard(Board9). test110(Board10) :- Board10 = [[_, _, 1, 9, _, _, _, _, 3], [9, _, _, 7, _, _, 1, 6, ], [, 3, _, _, _, 5, _, _, 7], [_, 5, _, _, _, _, _, _, 9], [_, _, 4, 3, _, 2, 6, _, ], [2, _, _, _, _, _, _, 7, _], [6, _, _, 1, _, _, _, 3, _], [, 4, 2, _, _, 7, _, _, 6], [5, _, _, _, _, 6, 8, _, _]], solve(Board10), printBoard(Board10). test111(Board11) :- Board11 = [[_, _, _, 1, 2, 5, 4, _, ], [, _, 8, 4, _, _, _, _, ], [4, 2, _, 8, _, _, _, _, _], [, 3, _, _, _, _, _, 9, 5], [_, 6, _, 9, _, 2, _, 1, ], [5, 1, _, _, _, _, _, 6, _], [, _, _, _, _, 3, _, 4, 9], [_, _, _, _, _, 7, 2, _, ], [, _, 1, 2, 9, 8, _, _, _]], solve(Board11), printBoard(Board11). test112(Board12) :- Board12 = [[_, 6, 2, 3, 4, _, 7, 5, ], [1, _, _, _, _, 5, 6, _, _], [5, 7, _, _, _, _, _, 4, _], [, _, _, _, 9, 4, 8, _, ], [4, _, _, _, _, _, _, _, 6], [, _, 5, 8, 3, _, _, _, ], [, 3, _, _, _, _, _, 9, 1], [_, _, 6, 4, _, _, _, _, 7], [_, 5, 9, _, 8, 3, 2, 6, _]], solve(Board12), printBoard(Board12). test113(Board13) :- Board13 = [[3, _, _, _, _, _, _, _, ], [, _, 5, _, _, 9, _, _, ], [2, _, _, 5, _, 4, _, _, _], [, 2, _, _, _, _, 7, _, ], [1, 6, _, _, _, _, _, 5, 8], [7, _, 4, 3, 1, _, 6, _, _], [, _, _, 8, 9, _, 1, _, ], [, _, _, _, 6, 7, _, 8, ], [, _, _, _, _, 5, 4, 3, 7]], solve(Board13), printBoard(Board13). test114(Board14) :- Board14 = [[6, 3, _, _, _, _, _, _, ], [, _, _, 5, _, _, _, _, 8], [_, _, 5, 6, 7, 4, _, _, ], [, _, _, _, 2, _, _, _, ], [, _, 3, 4, _, 1, _, 2, ], [, _, _, _, _, _, 3, 4, 5], [_, _, _, _, _, 7, _, _, 4], [_, 8, _, 3, _, _, 9, _, 2], [9, 4, 7, 1, _, _, _, 8, _]], solve(Board14), printBoard(Board14). test115(Board15) :- Board15 = [[_, _, _, _, 2, _, _, 4, ], [, _, 8, _, 3, 5, _, _, ], [, _, _, _, 7, _, 6, _, 2], [_, 3, 1, _, 4, 6, 9, 7, ], [2, _, _, _, _, _, _, _, _], [, _, _, 5, _, 1, 2, _, 3], [_, 4, 9, _, _, _, 7, 3, ], [, _, _, _, _, _, _, 1, _], [8, _, _, _, _, 4, _, _, _]], solve(Board15), printBoard(Board15). test116(Board16) :- Board16 = [[3, 6, 1, _, 2, 5, 9, _, ], [, 8, _, 9, 6, _, _, 1, ], [4, _, _, _, _, _, _, 5, 7], [, _, 8, _, _, _, 4, 7, 1], [_, _, _, 6, _, 3, _, _, ], [2, 5, 9, _, _, _, 8, _, _], [7, 4, _, _, _, _, _, _, 5], [, 2, _, _, 1, 8, _, 6, ], [, _, 5, 4, 7, _, 3, 2, 9]], solve(Board16), printBoard(Board16). test117(Board17) :- Board17 = [[_, 5, _, 8, _, 7, _, 2, ], [6, _, _, _, 1, _, _, 9, _], [7, _, 2, 5, 4, _, _, _, 6], [, 7, _, _, 2, _, 3, _, 1], [5, _, 4, _, _, _, 9, _, 8], [1, _, 3, _, 8, _, _, 7, ], [9, _, _, _, 7, 6, 2, _, 5], [, 6, _, _, 9, _, _, _, 3], [_, 8, _, 1, _, 3, _, 4, _]], solve(Board17), printBoard(Board17). test118(Board18) :- Board18 = [[_, 8, _, _, _, 5, _, _, ], [, _, _, _, _, 3, 4, 5, 7], [_, _, _, _, 7, _, 8, _, 9], [_, 6, _, 4, _, _, 9, _, 3], [_, _, 7, _, 1, _, 5, _, ], [4, _, 8, _, _, 7, _, 2, _], [9, _, 1, _, 2, _, _, _, _], [8, 4, 2, 3, _, _, _, _, _], [, _, _, 1, _, _, _, 8, _]], solve(Board18), printBoard(Board18). test119(Board19) :- Board19 = [[_, _, 3, 5, _, 2, 9, _, ], [, _, _, _, 4, _, _, _, ], [1, _, 6, _, _, _, 3, _, 5], [9, _, _, 2, 5, 1, _, _, 8], [, 7, _, 4, _, 8, _, 3, ], [8, _, _, 7, 6, 3, _, _, 1], [3, _, 8, _, _, _, 1, _, 4], [, _, _, _, 2, _, _, _, ], [, _, 5, 1, _, 4, 8, _, _]], solve(Board19), printBoard(Board19). test120(Board20) :- Board20 = [[_, _, _, _, _, _, _, _, ], [, _, 9, 8, _, 5, 1, _, ], [, 5, 1, 9, _, 7, 4, 2, ], [2, 9, _, 4, _, 1, _, 6, 5], [, _, _, _, _, _, _, _, ], [1, 4, _, 5, _, 8, _, 9, 3], [, 2, 6, 7, _, 9, 5, 8, ], [, _, 5, 1, _, 3, 6, _, ], [, _, _, _, _, _, _, _, _]], solve(Board20), printBoard(Board20). test121(Board21) :- Board21 = [[_, 2, _, _, 3, _, _, 9, ], [, _, _, 9, _, 7, _, _, ], [9, _, _, 2, _, 8, _, _, 5], [, _, 4, 8, _, 6, 5, _, ], [6, _, 7, _, _, _, 2, _, 8], [, _, 3, 1, _, 2, 9, _, ], [8, _, _, 6, _, 5, _, _, 7], [, _, _, 3, _, 9, _, _, ], [, 3, _, _, 2, _, _, 5, _]], solve(Board21), printBoard(Board21). test122(Board22) :- Board22 = [[_, _, 5, _, _, _, _, _, 6], [_, 7, _, _, _, 9, _, 2, ], [, _, _, 5, _, _, 1, _, 7], [8, _, 4, 1, 5, _, _, _, ], [, _, _, 8, _, 3, _, _, ], [, _, _, _, 9, 2, 8, _, 5], [9, _, 7, _, _, 6, _, _, ], [, 3, _, 4, _, _, _, 1, _], [2, _, _, _, _, _, 6, _, _]], solve(Board22), printBoard(Board22). test123(Board23) :- Board23 = [[_, 4, _, _, _, _, _, 5, ], [, _, 1, 9, 4, 3, 6, _, ], [, _, 9, _, _, _, 3, _, ], [6, _, _, _, 5, _, _, _, 2], [1, _, 3, _, _, _, 5, _, 6], [8, _, _, _, 2, _, _, _, 7], [, _, 5, _, _, _, 2, _, ], [, _, 2, 4, 3, 6, 7, _, ], [, 3, _, _, _, _, _, 4, _]], solve(Board23), printBoard(Board23). test124(Board24) :- Board24 = [[_, _, 4, _, _, _, _, _, ], [, _, _, _, 3, _, _, _, 2], [3, 9, _, 7, _, _, _, 8, ], [4, _, _, _, _, 9, _, _, 1], [2, _, 9, 8, _, 1, 3, _, 7], [6, _, _, 2, _, _, _, _, 8], [, 1, _, _, _, 8, _, 5, 3], [9, _, _, _, 4, _, _, _, ], [, _, _, _, _, _, 8, _, _]], solve(Board24), printBoard(Board24). test125(Board25) :- Board25 = [[3, 6, _, _, 2, _, _, 8, 9], [_, _, _, 3, 6, 1, _, _, ], [, _, _, _, _, _, _, _, ], [8, _, 3, _, _, _, 6, _, 2], [4, _, _, 6, _, 3, _, _, 7], [6, _, 7, _, _, _, 1, _, 8], [, _, _, _, _, _, _, _, ], [, _, _, 4, 1, 8, _, _, _], [9, 7, _, _, 3, _, _, 1, 4]], solve(Board25), printBoard(Board25). test126(Board26) :- Board26 = [[5, _, _, 4, _, _, _, 6, ], [, _, 9, _, _, _, 8, _, ], [6, 4, _, _, 2, _, _, _, _], [, _, _, _, _, 1, _, _, 8], [2, _, 8, _, _, _, 5, _, 1], [7, _, _, 5, _, _, _, _, ], [, _, _, _, 9, _, _, 8, 4], [_, _, 3, _, _, _, 6, _, ], [, 6, _, _, _, 3, _, _, 2]], solve(Board26), printBoard(Board26). test127(Board27) :- Board27 = [[_, _, 7, 2, 5, 6, 4, _, ], [4, _, _, _, _, _, _, _, 5], [, 1, _, _, 3, _, _, 6, ], [, _, _, 5, _, 8, _, _, ], [, _, 8, _, 6, _, 2, _, ], [, _, _, 1, _, 7, _, _, ], [, 3, _, _, 7, _, _, 9, ], [2, _, _, _, _, _, _, _, 4], [, _, 6, 3, 1, 2, 7, _, _]], solve(Board27), printBoard(Board27). test128(Board28) :- Board28 = [[_, _, _, _, _, _, _, _, ], [, 7, 9, _, 5, _, 1, 8, ], [8, _, _, _, _, _, _, _, 7], [, _, 7, 3, _, 6, 8, _, ], [4, 5, _, 7, _, 8, _, 9, 6], [, _, 3, 5, _, 2, 7, _, ], [7, _, _, _, _, _, _, _, 5], [, 1, 6, _, 3, _, 4, 2, ], [, _, _, _, _, _, _, _, _]], solve(Board28), printBoard(Board28). test129(Board29) :- Board29 = [[_, 3, _, _, _, _, _, 8, ], [, _, 9, _, _, _, 5, _, ], [, _, 7, 5, _, 9, 2, _, ], [7, _, _, 1, _, 5, _, _, 8], [, 2, _, _, 9, _, _, 3, ], [9, _, _, 4, _, 2, _, _, 1], [, _, 4, 2, _, 7, 1, _, ], [, _, 2, _, _, _, 8, _, ], [, 7, _, _, _, _, _, 9, _]], solve(Board29), printBoard(Board29). test130(Board30) :- Board30 = [[2, _, _, 1, 7, _, 6, _, 3], [_, 5, _, _, _, _, 1, _, ], [, _, _, _, _, 6, _, 7, 9], [_, _, _, _, 4, _, 7, _, ], [, _, _, 8, _, 1, _, _, ], [, _, 9, _, 5, _, _, _, ], [3, 1, _, 4, _, _, _, _, _], [, _, 5, _, _, _, _, 6, _], [9, _, 6, _, 3, 7, _, _, 2]], solve(Board30), printBoard(Board30). test131(Board31) :- Board31 = [[_, _, _, _, _, _, _, 8, ], [8, _, _, 7, _, 1, _, 4, _], [, 4, _, _, 2, _, _, 3, ], [3, 7, 4, _, _, _, 9, _, _], [, _, _, _, 3, _, _, _, ], [, _, 5, _, _, _, 3, 2, 1], [_, 1, _, _, 6, _, _, 5, ], [, 5, _, 8, _, 2, _, _, 6], [_, 8, _, _, _, _, _, _, _]], solve(Board31), printBoard(Board31). test132(Board32) :- Board32 = [[_, _, _, _, _, _, _, 8, 5], [_, _, _, 2, 1, _, _, _, 9], [9, 6, _, _, 8, _, 1, _, ], [5, _, _, 8, _, _, _, 1, 6], [, _, _, _, _, _, _, _, ], [8, 9, _, _, _, 6, _, _, 7], [, _, 9, _, 7, _, _, 5, 2], [3, _, _, _, 5, 4, _, _, _], [4, 8, _, _, _, _, _, _, _]], solve(Board32), printBoard(Board32). test133(Board33) :- Board33 = [[6, _, 8, _, 7, _, 5, _, 2], [_, 5, _, 6, _, 8, _, 7, ], [, _, 2, _, _, _, 3, _, ], [5, _, _, _, 9, _, _, _, 6], [, 4, _, 3, _, 2, _, 5, ], [8, _, _, _, 5, _, _, _, 3], [, _, 5, _, _, _, 2, _, ], [, 1, _, 7, _, 4, _, 9, _], [4, _, 9, _, 6, _, 7, _, 1]], solve(Board33), printBoard(Board33). test134(Board34) :- Board34 = [[_, 5, _, _, 1, _, _, 4, ], [1, _, 7, _, _, _, 6, _, 2], [, _, _, 9, _, 5, _, _, ], [2, _, 8, _, 3, _, 5, _, 1], [, 4, _, _, 7, _, _, 2, ], [9, _, 1, _, 8, _, 4, _, 6], [, _, _, 4, _, 1, _, _, ], [3, _, 4, _, _, _, 7, _, 9], [, 2, _, _, 6, _, _, 1, _]], solve(Board34), printBoard(Board34). test135(Board35) :- Board35 = [[_, 5, 3, _, _, _, 7, 9, ], [, _, 9, 7, 5, 3, 4, _, ], [1, _, _, _, _, _, _, _, 2], [, 9, _, _, 8, _, _, 1, ], [, _, _, 9, _, 7, _, _, ], [, 8, _, _, 3, _, _, 7, ], [5, _, _, _, _, _, _, _, 3], [, _, 7, 6, 4, 1, 2, _, ], [, 6, 1, _, _, _, 9, 4, _]], solve(Board35), printBoard(Board35). test136(Board36) :- Board36 = [[_, _, 6, _, 8, _, 3, _, ], [, 4, 9, _, 7, _, 2, 5, ], [, _, _, 4, _, 5, _, _, ], [6, _, _, 3, 1, 7, _, _, 4], [, _, 7, _, _, _, 8, _, ], [1, _, _, 8, 2, 6, _, _, 9], [, _, _, 7, _, 2, _, _, ], [, 7, 5, _, 4, _, 1, 9, ], [, _, 3, _, 9, _, 6, _, _]], solve(Board36), printBoard(Board36). test137(Board37) :- Board37 = [[_, _, 5, _, 8, _, 7, _, ], [7, _, _, 2, _, 4, _, _, 5], [3, 2, _, _, _, _, _, 8, 4], [, 6, _, 1, _, 5, _, 4, ], [, _, 8, _, _, _, 5, _, ], [, 7, _, 8, _, 3, _, 1, ], [4, 5, _, _, _, _, _, 9, 1], [6, _, _, 5, _, 8, _, _, 7], [, _, 3, _, 1, _, 6, _, _]], solve(Board37), printBoard(Board37). test138(Board38) :- Board38 = [[_, _, _, 9, _, _, 8, _, ], [1, 2, 8, _, _, 6, 4, _, _], [, 7, _, 8, _, _, _, 6, ], [8, _, _, 4, 3, _, _, _, 7], [5, _, _, _, _, _, _, _, 9], [6, _, _, _, 7, 9, _, _, 8], [, 9, _, _, _, 4, _, 1, ], [, _, 3, 6, _, _, 2, 8, 4], [_, _, 1, _, _, 7, _, _, _]], solve(Board38), printBoard(Board38). test139(Board39) :- Board39 = [[_, _, _, _, 8, _, _, _, ], [2, 7, _, _, _, _, _, 5, 4], [, 9, 5, _, _, _, 8, 1, ], [, _, 9, 8, _, 6, 4, _, ], [, 2, _, 4, _, 3, _, 6, ], [, _, 6, 9, _, 5, 1, _, ], [, 1, 7, _, _, _, 6, 2, ], [4, 6, _, _, _, _, _, 3, 8], [, _, _, _, 9, _, _, _, _]], solve(Board39), printBoard(Board39). test140(Board40) :- Board40 = [[_, _, _, 6, _, 2, _, _, ], [4, _, _, _, 5, _, _, _, 1], [, 8, 5, _, 1, _, 6, 2, ], [, 3, 8, 2, _, 6, 7, 1, ], [, _, _, _, _, _, _, _, ], [, 1, 9, 4, _, 7, 3, 5, ], [, 2, 6, _, 4, _, 5, 3, ], [9, _, _, _, 2, _, _, _, 7], [, _, _, 8, _, 9, _, _, _]], solve(Board40), printBoard(Board40). test141(Board41) :- Board41 = [[_, _, _, 9, _, _, _, _, 2], [_, 5, _, 1, 2, 3, 4, _, ], [, 3, _, _, _, _, 1, 6, ], [9, _, 8, _, _, _, _, _, _], [, 7, _, _, _, _, _, 9, ], [, _, _, _, _, _, 2, _, 5], [_, 9, 1, _, _, _, _, 5, ], [, _, 7, 4, 3, 9, _, 2, _], [4, _, _, _, _, 7, _, _, _]], solve(Board41), printBoard(Board41). test142(Board42) :- Board42 = [[3, 8, _, _, _, _, _, _, ], [, _, _, 4, _, _, 7, 8, 5], [_, _, 9, _, 2, _, 3, _, ], [, 6, _, _, 9, _, _, _, ], [8, _, _, 3, _, 2, _, _, 9], [, _, _, _, 4, _, _, 7, ], [, _, 1, _, 7, _, 5, _, ], [4, 9, 5, _, _, 6, _, _, _], [, _, _, _, _, _, _, 9, 2]], solve(Board42), printBoard(Board42). test143(Board43) :- Board43 = [[_, _, _, 1, 5, 8, _, _, ], [, _, 2, _, 6, _, 8, _, ], [, 3, _, _, _, _, _, 4, ], [, 2, 7, _, 3, _, 5, 1, ], [, _, _, _, _, _, _, _, ], [, 4, 6, _, 8, _, 7, 9, ], [, 5, _, _, _, _, _, 8, ], [, _, 4, _, 7, _, 1, _, ], [, _, _, 3, 2, 5, _, _, _]], solve(Board43), printBoard(Board43). test144(Board44) :- Board44 = [[_, 1, _, 5, _, _, 2, _, ], [9, _, _, _, _, 1, _, _, _], [, _, 2, _, _, 8, _, 3, ], [5, _, _, _, 3, _, _, _, 7], [, _, 8, _, _, _, 5, _, ], [6, _, _, _, 8, _, _, _, 4], [, 4, _, 1, _, _, 7, _, ], [, _, _, 7, _, _, _, _, 6], [_, _, 3, _, _, 4, _, 5, _]], solve(Board44), printBoard(Board44). test145(Board45) :- Board45 = [[_, 8, _, _, _, _, _, 4, ], [, _, _, 4, 6, 9, _, _, ], [4, _, _, _, _, _, _, _, 7], [, _, 5, 9, _, 4, 6, _, ], [, 7, _, 6, _, 8, _, 3, ], [, _, 8, 5, _, 2, 1, _, ], [9, _, _, _, _, _, _, _, 5], [, _, _, 7, 8, 1, _, _, ], [, 6, _, _, _, _, _, 1, _]], solve(Board45), printBoard(Board45). test146(Board46) :- Board46 = [[9, _, 4, 2, _, _, _, _, 7], [_, 1, _, _, _, _, _, _, ], [, _, _, 7, _, 6, 5, _, ], [, _, _, 8, _, _, _, 9, ], [, 2, _, 9, _, 4, _, 6, ], [, 4, _, _, _, 2, _, _, ], [, _, 1, 6, _, 7, _, _, ], [, _, _, _, _, _, _, 3, _], [3, _, _, _, _, 5, 7, _, 2]], solve(Board46), printBoard(Board46). test147(Board47) :- Board47 = [[_, _, _, 7, _, _, 8, _, ], [, _, 6, _, _, _, _, 3, 1], [_, 4, _, _, _, 2, _, _, ], [, 2, 4, _, 7, _, _, _, ], [, 1, _, _, 3, _, _, 8, ], [, _, _, _, 6, _, 2, 9, ], [, _, _, 8, _, _, _, 7, ], [8, 6, _, _, _, _, 5, _, _], [, _, 2, _, _, 6, _, _, _]], solve(Board47), printBoard(Board47). test148(Board48) :- Board48 = [[_, _, 1, _, _, 7, _, 9, ], [5, 9, _, _, 8, _, _, _, 1], [, 3, _, _, _, _, _, 8, ], [, _, _, _, _, 5, 8, _, ], [, 5, _, _, 6, _, _, 2, ], [, _, 4, 1, _, _, _, _, ], [, 8, _, _, _, _, _, 3, ], [1, _, _, _, 2, _, _, 7, 9], [, 2, _, 7, _, _, 4, _, _]], solve(Board48), printBoard(Board48). test149(Board49) :- Board49 = [[_, _, _, _, _, 3, _, 1, 7], [_, 1, 5, _, _, 9, _, _, 8], [_, 6, _, _, _, _, _, _, ], [1, _, _, _, _, 7, _, _, _], [, _, 9, _, _, _, 2, _, ], [, _, _, 5, _, _, _, _, 4], [_, _, _, _, _, _, _, 2, _], [5, _, _, 6, _, _, 3, 4, _], [3, 4, _, 2, _, _, _, _, _]], solve(Board49), printBoard(Board49). test150(Board50) :- Board50 = [[3, _, _, 2, _, _, _, _, ], [, _, _, 1, _, 7, _, _, ], [7, _, 6, _, 3, _, 5, _, _], [, 7, _, _, _, 9, _, 8, ], [9, _, _, _, 2, _, _, _, 4], [, 1, _, 8, _, _, _, 5, ], [, _, 9, _, 4, _, 3, _, 1], [_, _, _, 7, _, 2, _, _, ], [, _, _, _, _, 8, _, _, 6]], solve(Board50), printBoard(Board50). ```

```superTest() :- test11(), write("\n"), test12(), write("\n"), test13(), write("\n"), test14(), write("\n"), test15(), write("\n"), test16(), write("\n"), test17(), write("\n"), test18(), write("\n"), test19(), write("\n"), test110(), write("\n"), test111(), write("\n"), test112(), write("\n"), test113(), write("\n"), test114(), write("\n"), test115(), write("\n"), test116(), write("\n"), test117(), write("\n"), test118(), write("\n"), test119(), write("\n"), test120(), write("\n"), test121(), write("\n"), test122(), write("\n"), test123(), write("\n"), test124(), write("\n"), test125(), write("\n"), test126(), write("\n"), test127(), write("\n"), test128(), write("\n"), test129(), write("\n"), test130(), write("\n"), test131(), write("\n"), test132(), write("\n"), test133(), write("\n"), test134(), write("\n"), test135(), write("\n"), test136(), write("\n"), test137(), write("\n"), test138(), write("\n"), test139(), write("\n"), test140(), write("\n"), test141(), write("\n"), test142(), write("\n"), test143(), write("\n"), test144(), write("\n"), test145(), write("\n"), test146(), write("\n"), test147(), write("\n"), test148(), write("\n"), test149(), write("\n"), test150(). ```