I have learnt C++ of late and I'm trying to contrive a Sudoku - Solving algorithm. I developed this algorithm but when I run the program, the computer takes input values from me but then doesn't give any output. Any help would be appreciated.
Kindly tell me if the program is too lengthy to execute and how can I improve on that.
#include <iostream>
#include <math.h>
using namespace std;
int main() {
int a[9][9], r[9][9][9], pv[9][9], super[9][9], i, j, k, ij, tv, m, n, g1, g2, g, c, power, p, argh;
{
cout<<"PROGRAM TO SOLVE A 9 X 9 SUDOKU: "<<endl<<endl<<"Enter sudoku matrix columnwise with rowwise entries in each column: "<<endl;
for (i=0, j=0, ij = 0 ; (i<9)&&(j<9) ; i=(i<8)?i++:0, j=ij/9, ij++) {
cin>>a[i][j];
while ((a[i][j]<0)||(a[i][j]>9)) {
cout<<"wrong input for c["<<i+1<<"] and r["<<j+1<<"]"<<endl<<"Enter again: ";
cin>>a[i][j];
}
}
}
for (i=0, j=0, ij=0 ; (i<9)&&(j<9) ; i=(i<8)?i++:0, j=ij/9, ij++) {
if (a[i][j]==0) {
for (k=0 ; k<9 ; k++) {
r[i][j][k]=k+1;
}
}
else {
for (k=0 ; k<9 ; k++) {
r[i][j][k]=a[i][j];
}
}
}
for (c=1, tv = 1 ; ; c++) {
for (i=0, j=0, ij = 0; ij<80 ; i=(i<8)?i+1:0, j = ij/9, ij++) {
argh = i + 9*j;
super[i][j]=9^argh;
pv[i][j] = (c/super[i][j])%9;
for (m=0 ; m<9 ; m=(m==i)?m+2:m+1) {
tv = tv && (r[m][j][(pv[m][j])]!=r[i][j][(pv[i][j])]);
}
for (n=0 ; n<9 ; n=(n==j)?(n+2):(n+1)) {
tv = tv && (r[m][j][(pv[m][j])]!=r[i][j][(pv[i][j])]);
}
for (g1=0, g2=0, g=0 ; (g1<8) ; g++, g1=g%3, g2=g/3) {
tv = tv && (r[3*(i/3)+g1][3*(j/3)+g2][(pv[3*(i/3)+g1][3*(j/3)+g2])]!=r[i][j][(pv[i][j])]);
}
}
if (tv==1) {
for (i=0, j=0, ij=0 ; ij<80 ; ij++, i=ij%9, j=ij/9) {
cout<<a[i][j]<<" ";
if (i=8*i/8) {
cout<<endl;
}
}
}
else {
cout<<"sudoku can not be solved exactly: ";
}
return 0;
}
}