Results
It's pretty awesome that we can actually find a solution to Sudoku via a simple backtracking routine. Let's see this routine in action on a simple 4 x 4
board as shown in the figure below. There are three empty cells. We can see that all combinations of numbers are tried.
Once an invalid board configuration is found, the entire branch is abandoned, backtracked, and a new solution is tried. The C++ implementation is provided. You can add your own public function to initialize the board differently.
xxxxxxxxxx
128
// s.print();
class sudoku {
vector<vector<int> > board;
​
void Initialize()
{
int arrBoard[] = {
2,
1,
3,
4,
1,
3,
-1,
2,
-1,
2,
-1,
3,
-1,
-1,
2,
1
};
vector<int> temp;
​
int ind = 0;
for (int i = 0; i < 4; i++) {
temp.clear();
for (int j = 0; j < 4; ++j) {
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment