Hi guys,
I have been working on a sudoku solver, that finds solution by brute forcing during my school days.... But due to the huge number of loops i couldn't do it at then.... SO decided to complete it now....
So here the algorithm:
1. Get the number of fixed values from the user.
2. Populate those values..
3. Check if the input sudoku is valid.
4. If valid, fill each posistion by checking numbers from 1 to 9, if they are valid at the posistion...
5. Done....
Well! In my sudoku i have only checked for rows and colomns..... i.e I don't care if a number repeats in 3*3 matrics.... I will work on that bit, later....
Now the prob is, i cannot get the bruteforcing part correctly.... Even the check for a valid input sudoku is not working......
SO please help me guys...... No syntax error.... Some logical one.....
Thanks in advance.....
// Gigantic Addtion.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
void display(int sudoku[9][9])
{
for(int i=0;i<9;i++)
{
cout<<endl<<endl;
for(int j=0;j<9;j++)
{
cout<<sudoku[i][j]<<" ";
}
}
cout<<endl;
}
void fix(int sudoku[9][9])
{
int num, flag=0;
do
{
cout<<"Enter the number of fixed values you want place in Sudoku"<<endl;
cin>>num;
}while(num<0&&num>9);
for(int k=0;k<num;k++)
{
int i,j,val;
do
{
cout<<"Please enter the cordinates of the number(Row*coloumn)"<<endl;
cin>>i>>j;
do
{
cout<<"Enter the value"<<endl;
cin>>val;
}while(val<1&&val>9);
if(sudoku[i-1][j-1]==10)
{
sudoku[i-1][j-1]=val;
system("CLS");
display(sudoku);
flag=0;
}
else
{
cout<<"Value already fixed at the point"<<endl;
flag=1;
}
}while(flag==1);
}
}
int check(int sudoku[9][9])
{
int flag=0;
cout<<endl<<"Checking"<<endl;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(sudoku[i][j]!=10)
{
for(int k=0;k<9;k++)
{
for(int p=0;p<9;)
{
if(i!=k||p!=j)
{
if(sudoku[i][j]==sudoku[k][j])
{
cout<<"Invalid fixed numbers"<<endl;
flag=1;
break;
}
else
{
break;
}
}
}
}
for(int s=0;s<9;s++)
{
for(int g=0;g<9;)
{
if(i!=g||j!=s)
{
if(sudoku[i][j]==sudoku[g][s])
{
cout<<"Invalid fixed numbers"<<endl;
flag=1;
break;
}
else
{
break;
}
}
}
}
}
}
}
return flag;
}
void fill(int sudoku[9][9])
{
int flag1=1, flag2=1;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
for(int k=1;k<10;k++)
{
if(sudoku[i][i]==10)
{
for(int l=0;l<9;l++)
{
for(int p=0;p<9;)
{
if(sudoku[l][p]!=k)
{
flag1=0;
}
else
{
flag1=1;
}
}
}
for(int q=0;q<9;q++)
{
for(int v=0;v<9;)
{
if(sudoku[v][q]!=k)
{
flag2=0;
}
else
{
flag2=1;
}
}
}
if(flag1==0&&flag2==0)
{
sudoku[i][j]=k;
system("CLS");
display(sudoku);
}
}
}
}
}
}
void main()
{
int sudoku[9][9];
int test;
int enter;
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
sudoku[i][j]=10;
}
}
display(sudoku);
fix(sudoku);
test=check(sudoku);
if(test==1)
{
cout<<"Your Sudoku input is wrong, Hence cannot be solved"<<endl;
display(sudoku);
}
else
{
fill(sudoku);
}
display(sudoku);
cin>>enter;
}