Code:
# include <stdio.h>
//global declaration
int sudoku[9][9]={
{0,0,0,0,0,0,0,0,0},
{4,5,6,7,8,9,0,0,0},
{7,8,9,0,0,0,0,0,0},
{0,0,4,3,6,5,8,9,7},
{0,6,5,8,9,7,2,0,4},
{8,9,7,2,0,4,0,6,5},
{5,3,0,6,4,0,9,7,8},
{0,0,0,9,7,8,0,0,0},
{0,0,0,0,0,0,0,0,0}
};
int check (int, int, int);
int check_box (int, int, int);
int check_column(int, int, int);
int check_row(int, int, int);
void printIt(void);
void printTest(char [][12]);
int main(void)
{
//local declarations
int TempArr[9][9];
int i,j,n;
//int bki,bkj;
//int walk_j=0,walk_n=1;
//int flag1,flag2;
char testGrid[11][12] = {
{"123|456|789"},
{"456|789|123"},
{"789|123|456"},
{"---+---+---"},
{"214|365|897"},
{"365|897|214"},
{"897|214|365"},
{"---+---+---"},
{"531|642|978"},
{"642|978|531"},
{"978|531|642"}
};
// Input
//printf("Enter Sudoku\n");
for (i=0; i<9; i++)
{
for (j=0; j<9; j++)
{
//scanf("%d",&sudoku[i][j]);
//copy to TempArr
TempArr[i][j] = sudoku[i][j];
}
}
// brute force.
for (i=0; i<9; i++)
{
//walk_j=0;
for (j=0; j<9; j++)
{
if (sudoku[i][j]==0)
{
for (n=1;n<10;n++)
{
//flag1=0;
if ( check(i,j,n) == 1)
{
sudoku[i][j]=n;
//walk_n=1;
break;
}
//else
// flag1 = 1;
} //for n
}//if
//flag2 = 0;
/*
if (flag1==1)
{
for (bki=i;bki>=0;bki--)
{
for (bkj=j-1;bkj>=0;bkj--)
{
if (TempArr[bki][bkj]==0)
{
sudoku[i][j]=0;
sudoku[bki][bkj]=0;
i = bki;
walk_j = bkj;
walk_n = n+1;
flag2 = 1;
break;
}
}// bkj
if (flag2==1)
break;
}//bki
}//flag1
*/
}//for j
}//for i
printTest(testGrid);
printIt();
return 0;
}// main
/* functions ---- */
int check (int i, int j, int n)
{
if (check_row(i,j,n) == 1)
{
if (check_column(i,j,n) == 1)
{
if (check_box(i,j,n) == 1)
{
return 1;
}
}
}
return 0;
}
int check_box (int i, int j, int n)
{ /* avoid <= and >=. Use < next highest number or > next lowest number.
Consider speed which does not diminish clarity, to be a central
part of the design of this program.
*/
int loci, locj;
int lorow, hirow, locol, hicol;
if (i < 3) //start of box 1, 2 & 3
{
lorow = 0; hirow = 3;
if(j < 3) {
locol = 0; hicol = 3;
}
else if(2 < j && j < 6) {
locol = 3; hicol = 6;
}//box2
else { //j must be 6, 7, or 8
locol = 6; hicol = 9;
}
}//end of box 1, 2, & 3
else if (2 < i && i < 6) { //start of box 4, 5, & 6
lorow = 3; hirow = 6;
if (j < 3) {
locol = 0; hicol = 3;
}
else if (2 < j && j < 6) {
locol = 3; hicol = 6;
}//box5
else { //j must be 6, 7, or 8
locol = 6; hicol = 9;
}
}//end of box 4, 5, & 6
else if (5 < i && i < 9) { //start of box 7, 8, & 9
lorow = 6; hirow = 9;
if (j < 3) {
locol = 0; hicol = 3;
}
else if (2 < j && j < 6) {
locol = 3; hicol = 6;
}
else {
locol = 6; locol = 9;
}
}//end of boxes 7, 8 & 9
for (loci=lorow; loci<hirow; loci++)
{
for (locj=locol; locj<hicol; locj++)
{
//if (loci != i)
//{
// if (locj != j)
// {
if (sudoku[loci][locj]==n)
return 0;
//}
//}
}
}
return 1;
}
int check_row(int i, int j, int n)
{
int walkj;
//keep it clear, and minimize your variables and code, or your
//solver will run very slowly.
/* when testing rows, you only test the current digit, against the digits on it's
left. So the checkrow variable decrements, not increments. And you only test
in the one row, not in any other row.
*/
for (walkj=j;walkj>-1;walkj--)
{
if (sudoku[i][walkj]==n) //column varies, not the row
{
return 0;
}
}
return 1;
}
int check_column(int i, int j, int n)
{
int walki;
/* same idea as check row, above. Minimize everything, keep it clear, check ONLY the
digits ABOVE this cell, in this one column.
*/
for (walki=i;walki>-1;walki--)
{
if (sudoku[walki][j]==n) //row varies, not the column
{
return 0;
}
}
return 1;
}
void printIt(void) {
int i, j;
// output
printf("\n+-----+-----+-----+\n");
for (i=0;i<9;i++) {
printf("| ");
for (j=0;j<9;j++) {
printf("%d",sudoku[i][j]);
if(j==2 || j==5 || j==8)
printf(" | ");
}
printf("\n");
if(i==2 || i==5)
printf("+-----+-----+-----+\n");
}
printf("+-----+-----+-----+\n");
}
void printTest(char testGrid[][12]) {
int i;
printf("\nThe Test Grid is:\n");
for(i=0;i<11;i++)
printf("%s\n", testGrid[i]);
printf("\n");
}
Just want to repeat that the above will NOT solve nearly any puzzles yet. Still needs backtracking logic added to it.