Here's the full cpp code:
#include
#include
#include
#include
#include
#include
using namespace std;
/*global variables*/
int cell[9][9];int i,j,k,x,y,a,b,c,d,first,second;
set
set
/*exit the program function*/
void exitme()
{
system ("PAUSE");
}
/*finding combinations in the squares*/
void squareMe(int &b, int &c)
{
if ((cell[c][b]!=0)&&(CellSet.count(cell[c][b])>0)) {
CellSet.erase(cell[c][b]);
changed=true;
}
}
void squareIt(int &b){
for (d=0;d<3;d++) {
if (i==0+3*d) {
for (c=i;c<(i+3);c++) squareMe(b,c);
}
else if (i==1+3*d) {
for (c=i-1;c<(i+2);c++) squareMe(b,c);
}
else if (i==2+3*d) {
for (c=i-2;c<=i;c++) squareMe(b,c);
}
}
}
int main ()
{
/*declare local variables*/
bool solved=true;
char InCell[]="";
/*clear variables*/
i=0;
j=0;
k=0;
x=0;
y=0;
a=0;
b=0;
c=0;
changed=false;
/*read the data*/
cout << "SUDOKU\n" << "Enter the problem:\n";
for (i=0;i<9;++i) {
for (j=0;j<9;++j) {
cout << "cell(" << i+1 << "," << j+1 << ") = ";
cin >> InCell[0];
if (isdigit(InCell[0])) {
cell[i][j]=atoi(InCell);
if (cell[i][j]==0) solved=false;
}
else {
cout << "Wrong data!\n";
exitme();
}
}
}
/*is there anything to solve?*/
if (solved==true) {
cout << "The problem is solved already!\n";
exitme();
}
/*general calcualtions*/
for (k=1;k<10;k++) CellSet.insert(k);
k=0;
assigned=true;
while ((solved==false)&&(assigned==true)) {
assigned=false;
for (i=0;i<9;i++) {
for (j=0;j<9;j++) {
for (k=1;k<10;k++) CellSet.insert(k);
k=0;
again:
if (cell[i][j]==0) {
changed=false;
for (k=0;k<9;k++) {
if ((cell[i][k]!=0)&&(CellSet.count(cell[i][k])>0)) CellSet.erase(cell[i][k]);
if ((cell[k][j]!=0)&&(CellSet.count(cell[k][j])>0)) CellSet.erase(cell[k][j]);
}
if (CellSet.size()==0) {
cout << "Wrong data. Impossible to solve.";
exitme();
}
if (CellSet.size()==1) {
it=CellSet.begin();
cell[i][j]=*it;
changed=false;
assigned=true;
}
for (a=0;a<3;a++) {
if (j==(0+3*a)) {
for (b=j;b<=(j+2);b++) squareIt(b);
}
if (j==(1+3*a)) {
for (b=(j-1);b<=(j+1);b++) squareIt(b);
}
if (j==(2+3*a)) {
for (b=(j-2);b<=j;b++) squareIt(b);
}
}
if (changed==true) goto again;
}
}
}
solved=true;
for (x=0;x<9;x++) {
for (y=0;y<9;y++) {
if (cell[x][y]==0) solved=false;
}
}
}
/*last step-output*/
cout << "SOLUTION:\n";
for (i=0;i<9;i++) {
for (j=0;j<9;j++) {
cout << "cell(" << i+1 << "," << j+1 << ")=" << cell[i][j] << "\n";
}
}
exitme();
}
So the program basically works, it just doesn't know things I don't know.
I am now working on creating windows applications in c++. Just exploring. We'll see where it leads.