I've said this before and I'll say it again. My favourite SuDoku method - The bool grids solution. Here is a rough outline of how to do it (I put it all in code tags to preserve formatting):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
//Declare the grids.
bool grids[9][9][9]; //You can declare it as a 3d array (I find that a bit confusing)
bool grid1[9][9], grid2[9][9], grid3[9][9] //etc... More typing, less confusing.
//Make sure to set all these grids all equal to zero in every cell!
//TACTIC ONE
//Each grid represent places where that number can and cannot go. Let's take your
//puzzle as an example.
/*
1 - - 4 8 9 - - 6
7 3 - - - - - 4 -
- - - - - 1 2 9 5
- - 7 1 2 - 6 - -
5 - - 7 - 3 - - 8
- - 6 - 9 5 7 - -
9 1 4 6 - - - - -
- 2 - - - - - 3 7
8 - - 5 1 2 - - 4
We then look at each number in turn and mark = 1 on the appropriate grid where the number CANNOT go. Let's look at grid1[9][9]
(I tried to format it nicely so you can see clearly):
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 0 1 0
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1
1 1 0 1 1 1 1 0 0
1 1 1 1 1 1 0 0 0
1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 0 1 1
1 1 1 1 1 1 1 1 1
*/
|
Stage 1: You can mark off where numbers cannot go by searching the grid cell by cell for each number in turn. In this case, we would look for 1s in the grid. Each time a 1 is found, mark the entire row, column and 3x3 block as 1 (full) in the bool grid. If you find another number, mark that individual cell as 1 (full).
Stage 2 of tactic one: Any row, column or 3x3 cell in which only contains one zero (i.e. total of section == 8) can have that missing 0 filled in as the number on the grid. In this case, you can see we can immediately mark cell (3,5) and cell (7,8) as being 1s also.
Stage 3: If any cells were modified during stage 2, go back to stage 1. Keep doing this until either the puzzle is solved or we cannot fill any more squares in like this.
This is not the only method, but I like it (hope it was explained ok). I must admit, I don't understand what tactic 2 is? The way I understand it, it would be covered in tactic 1 anyway... So I must be not understanding it.