#include <iostream>
usingnamespace std;
int main() {
//Initiallizing the variables
int board[9][9][10];
int x, y, n;
//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 z;
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 hte 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
*/
//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 EQUINOX"<<endl<<" ---------------------------------------------"<<endl<<endl<<endl;
return 0;
system ("PAUSE");
}