# Sudoku Solver - C++

Hey guys, I made this Sudoku Solver and… its not working. When I enter a proper Sudoku, It displays 9, and then all the other boxes are blanks.

``````#include <iostream>
using namespace std;

int main() {	//Initiallizing the variables				variable [row-i] [column-j] [possibility-k]
int board[9][9][10];
int x, y, n, z;

//Initiallizing the board
for (int i=0;i<9;i++) {
for (int j=0;j<9;j++) {
for (int k=0;k<10;k++) {
board[i][j][k] = k;
}
}
}
//In the array k[x][y][z], z is the possibles values of the square. We take k[x][y][0] as the entered
//number. If k[x][y][0] == 0, then its a blank.

//Getting the numbers
cout<<"Enter 0 for a blank in the Sudoku "<<endl<<endl;
for ( x = 0; x < 9; x++ ) {
for ( y = 0; y < 9; y++ ) {
do {
cout<<"Enter the value of square number "<<x + 1<<" , "<<y + 1<<" : ";
cin>>board[x][y][0];
} while (board[x][y][0]>10 );
}
}

//Seting all extra variables to zero and defining a few new ones that will be used later.
x = 0;
y = 0;
n = 0;
//	k = 0;
int i = 0;
int j = 0;
int size = 0;
int o;
int count1, count2;
int temp;
int q,k,l;

//ALGORITHIM BEGINS ------------------------------------------------------------------------------------------------------------------------------

//Now to loop onto infinity until all the boxes get filled.
do {

//									----	part.1 - TERMINATING THE VALUES  IN EACH ROW, COLOUMN AND BOX   ----

//Initializing the conters
count1 = 0;
count2 = 0;

//Terminiating possibilities in each row.
for (i=0;i<9;i++) {
for (j=0;j<9;j++) {
for (n=1;n<9;n++) {
//--
if (board[i][j][0] == n) {
//--
for (k=0;k<9;k++) {
for (l=0;l<10;l++) {
if (board[i][k][l] == n) board[i][k][l] = 0;
}
}
//--
}
//--
}
}
}

//Terminiating possibilities in each column.
for (j=0;j<9;j++) {
for (i=0;i<9;i++) {
for (n=1;n<9;n++) {
//--
if (board[i][j][0] == n) {
//--
for (q=0;q<9;q++) {
for (l=0;l<10;l++) {
if (board[q][j][l] == n) board[q][j][l] = 0;
}
//--
}
}
//--
}
}
}

//Terminiating possibilities in each box.
/*
The box pattern is shown below :

1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
*/

//																							q,i - row  ,  k,j - coloumn

//Box no.1
for (i=0;i<3;i++) {
for (j=0;j<3;j++) {
for (n=1;n<10;n++) {
//--
if (board[i][j][0] == n) {
//--
for (q=0;q<3;q++) {
for (k=0;k<3;k++) {
for (l=0;l<10;l++) {
if (board[q][k][l] == n) board[q][k][l] = 0;
}
}
}
//--
}
//--
}
}
}

//Box no.2
for (i=0;i<3;i++) {
for (j=3;j<6;j++) {
for (n=1;n<10;n++) {
//--
if (board[i][j][0] == n) {
for (q=0;q<3;q++) {
for (k=3;k<6;k++) {
for (l=0;l<10;l++) {
if (board[q][k][l] == n) board[q][k][l] = 0;
}
}
}
}
//--
}
}
}

//Box no.3
for (i=0;i<3;i++) {
for (j=6;j<9;j++) {
for (n=1;n<10;n++) {
//--
if (board[i][j][0] == n) {
for (q=0;q<3;q++) {
for (k=6;k<9;k++) {
for (l=0;l<10;l++) {
if (board[q][k][l] == n) board[q][k][l] = 0;
}
}
}
}
//--
}
}
}

//Box no.4
for (i=3;i<6;i++) {
for (j=0;j<3;j++) {
for (n=1;n<10;n++) {
//--
if (board[i][j][0] == n) {
for (q=3;q<6;q++) {
for (k=0;k<3;k++) {
for (l=0;l<10;l++) {
if (board[q][k][l] == n) board[q][k][l] = 0;
}
}
}
}
//--
}
}
}

//Box no.5
for (i=3;i<6;i++) {
for (j=3;j<6;j++) {
for (n=1;n<10;n++) {
//--
if (board[i][j][0] == n) {
for (q=3;q<6;q++) {
for (k=3;k<6;k++) {
for (l=0;l<10;l++) {
if (board[q][k][l] == n) board[q][k][l] = 0;
}
}
}
}
//--
}
}
}

//Box no.6
for (i=3;i<6;i++) {
for (j=6;j<9;j++) {
for (n=1;n<10;n++) {
//--
if (board[i][j][0] == n) {
for (q=3;q<6;q++) {
for (k=6;k<9;k++) {
for (l=0;l<10;l++) {
if (board[q][k][l] == n) board[q][k][l] = 0;
}
}
}
}
//--
}
}
}

//Box no.7
for (i=6;i<9;i++) {
for (j=0;j<3;j++) {
for (n=1;n<10;n++) {
//--
if (board[i][j][0] == n) {
for (q=6;q<9;q++) {
for (k=0;k<3;k++) {
for (l=0;l<10;l++) {
if (board[q][k][l] == n) board[q][k][l] = 0;
}
}
}
}
//--
}
}
}

//Box no.8
for (i=6;i<9;i++) {
for (j=3;j<6;j++) {
for (n=1;n<10;n++) {
//--
if (board[i][j][0] == n) {
for (q=6;q<9;q++) {
for (k=3;k<6;k++) {
for (l=0;l<10;l++) {
if (board[q][k][l] == n) board[q][k][l] = 0;
}
}
}
}
//--
}
}

}

//Box no.9
for (i=6;i<9;i++) {
for (j=6;j<10;j++) {
for (n=1;n<9;n++) {
//--
if (board[i][j][0] == n) {
for (q=6;q<9;q++) {
for (k=6;k<9;k++) {
for (l=0;l<10;l++) {
if (board[q][k][l] == n) board[q][k][l] = 0;
}
}
}
}
//--
}
}
}

//									----	part.2 - ASSIGNING FINAL VALUES TO THE BOX AND DISPLAYING THE BOARD    ----

for ( x = 0; x < 9; x++ ) {
for ( y = 0; y < 9; y++ ) {
for (z = 0; z < 10; z++) {
if (board[x][y][z] == 0) { count1++; }
else {temp = board[x][y][z];}
if(count1==8) { board[x][y][0] = temp; }    //CHANGE
}
}
}

system ("CLS");

for ( x = 0; x < 9; x++ ) {
for ( y = 0; y < 9; y++ ) {
if (board[x][y][0] == 0) {count2++;}
}
}

//Displaying the board
cout<<endl<<endl;
for (i=0;i<9;i++) {
for (j=0;j<9;j++) {
cout << "  " << board[i][j][0] << "  " << "|";
}
cout<<endl;
cout<< "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" << "     " << "|" <<endl;
}
``````

// –

``````  }while(count2 != 0);

cout<<endl<<endl<<endl;
cout<<"		---------------------------------------------"<<endl<<"		THIS SUDOKO SOLVER IS MADE BY GUHAN NARAYANAN"<<endl<<"		---------------------------------------------"<<endl<<endl<<endl;
return 0;
system ("PAUSE");
``````

}

Even if your program, the brute force will take a long time. I wrote similar program for first time and terribly failed on my face

6 nested for loops is wayyy too much. Your code will never finish, even if it were to be correct. I’m not gonna waste any time reading your un-commented code.

I can see this is probably the first time you ever made a Sudoku solver, and you didn’t read any materials on the internet regarding this. That’s fine, it’s part of the learning process.

Now, there are a lot of ways to make a sudoku solver, some of them involve using logic, and some involve plain brute-forcing. The one i made was made using backtracking. So that’s the only place i can help you with.

If you have knowledge about data structures and algorithms, Watch this lecture. http://www.youtube.com/watch?v=p-gpaIGRCQI . This is where i learned how to make the sudoku solver

Otherwise you’ll need to take up some course on data structures and algorithms, i’d recommend the lectures by richard buckland, simply because the guy loves to teach and the lectures are entertaining as well as educational.

Good luck, Keep coding!

ohh its amazing

@equinox_, so you seriously think, people are gonna read this code ?

Please post the code on ideone.com and provide a link to it. It will be more convenient.