Вы находитесь на странице: 1из 9

#include<iostream.

h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
#define UNASSIGNED 0
#define true 1
#define false 0
int findzero(int array[9][9], int &row, int &col);
int issafe(int array[9][9], int row, int col, int number);
int checksol(int array[9][9])
{
int row, col;
if (!findzero(array, row, col))
return 1;
for (int number = 1; number <= 9; number++)
{
if (issafe(array, row, col, number))
{
array[row][col] = number;
if (checksol(array))
return 1;
array[row][col] = UNASSIGNED;
}
}
return 0;
}
int findzero(int array[9][9], int &row, int &col)
{
for (row = 0; row <=8; row++)
for (col = 0; col <=8; col++)
if (array[row][col] == UNASSIGNED)
return 1;
return 0;
}
int foundrow(int array[9][9], int row, int number)
{
for (int col = 0; col <=8; col++)
if(array[row][col] == number)
return 1;
return 0;
}

int foundcol(int array[9][9], int col, int number)


{
for (int row = 0; row <=8; row++)
if (array[row][col] == number)
return 1;
return 0;
}
int foundbox(int array[9][9], int boxrow, int boxcol, int number)
{
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
if (array[row+boxrow][col+boxcol] == number)
return 1;
return 0;
}
int issafe(int array[9][9], int row, int col, int number)
{
return !foundrow(array, row, number) && !foundcol(array, col, number) &&
!foundbox(array, row - row%3 , col - col%3, number);
}
int getkey()
{
int ch;
ch=getch();
if(ch==0)
{
ch=getch();
return ch;
}
return ch;
}
void boxes(int n)
{
{
int row, col;
textcolor(n);
for(col=18; col<=63; col++)
{
for(row=5; row<=23; row+=2)
{
gotoxy(col, row);
switch(row)
{
case 5:

case 11:
case 17:
case 23:
cprintf("%c", 205);
break;
default:
cprintf("%c", 196);
}
}
}
for(row=5; row<=23; row++)
{
for(col=18; col<=63; col+=5)
{
gotoxy(col, row);
switch(col)
{
case 18:
case 33:
case 48:
case 63:
cprintf("%c", 186);
break;
default:
cprintf("%c", 179);
}
}
}
for(row=7; row<=21; row+=2)
{
for(col=23; col<=58; col+=5)
{
gotoxy(col, row);
cprintf("%c", 197);
}
}
for(row=5; row<=23; row+=2)
{
for(col=33; col<=48; col+=15)
{
gotoxy(col, row);
cprintf("%c", 215);
}
}
for(col=18; col<=63; col+=5)
{
for(row=11; row<=17; row+=6)
{

gotoxy(col, row);
cprintf("%c", 216);
}
}
for(col=23; col<=58; col+=5)
{
switch(col)
{
case 33:
case 48:
gotoxy(col, 5);
cprintf("%c", 203);
gotoxy(col, 23);
cprintf("%c", 202);
break;
default:
gotoxy(col, 5);
cprintf("%c", 209);
gotoxy(col, 23);
cprintf("%c", 207);
}
}
for(row=7; row<=21; row+=2)
{
switch(row)
{
case 11:
case 17:
gotoxy(18, row);
cprintf("%c", 204);
gotoxy(63, row);
cprintf("%c", 185);
break;
default:
gotoxy(18, row);
cprintf("%c", 199);
gotoxy(63, row);
cprintf("%c", 182);
}
}
gotoxy(33, 11);
cprintf("%c", 206);
gotoxy(33, 17);
cprintf("%c", 206);

gotoxy(48, 11);
cprintf("%c", 206);
gotoxy(48, 17);
cprintf("%c", 206);
gotoxy(18, 5);
cprintf("%c", 201);
gotoxy(63, 5);
cprintf("%c", 187);
gotoxy(18, 23);
cprintf("%c", 200);
gotoxy(63, 23);
cprintf("%c", 188);
}
}
void getarray(int array[9][9])
{
clrscr();
for(int i=0; i<=8; i++)
{
for(int j=0; j<=8; j++)
{
array[i][j]=0;
}
}
boxes(2);
int x=20, y=6;
gotoxy(x, y);
Starta:
int a;
gotoxy(x, y);
a=getkey();
textcolor(2);
boxes(2);
if(a==72)
{
boxes(9);

if(y==6)
y=22;
else
y-=2;
goto Starta;
}
else if(a==75)
{
boxes(9);
if(x==20)
x=60;
else
x-=5;
goto Starta;
}
else if(a==80)
{
boxes(9);
if(y==22)
y=6;
else
y+=2;
goto Starta;
}
else if(a==77)
{
boxes(9);
if(x==60)
x=20;
else
x+=5;
goto Starta;
}
else if(a>=49 && a<=58)
{
boxes(9);
gotoxy(x, y);

array[(y-6)/2][(x-20)/5]=a-48;
textcolor(14);
cout<<a-48;
goto Starta;
}
else if(a==8)
{
boxes(9);
gotoxy(x, y);
array[(y-6)/2][(x-20)/5]=0;
cout<<" ";
goto Starta;
}
else if(a==13)
{
goto Continue;
}
else
goto Starta;
Continue:
}
void putarray(int array[9][9])
{
for(int i=0; i<=8; i++)
{
for(int j=0; j<=8; j++)
{
gotoxy(20+j*5, 6+i*2);
if(array[i][j]==0)
cout<<" ";
else
cout<<array[i][j];
}
}
}
int isValid(int grid[] [9])
{
int i, j;
int status;
status = true;
for (int column = 0; column < 9; column++)

if (column != j && grid[i] [column] == grid[i] [j])


status = false;
for (int row = 0; row < 9; row++)
if (row != i && grid[row] [j] == grid[i] [j])
status = false;
for (row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
if (row != i && col != j && grid[row] [col] == grid[i] [j])
status = false;
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
if (grid[i][j] != 0)
status = false;
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
if ((grid[i][j] < 0) || (grid[i][j] > 9))
status = false;
return status;
}
void main()
{
int array[9][9];
clrscr();
textcolor(2);
gotoxy(5, 5);
cout<<"Enter the blank sudoku grid into the grid provide";
gotoxy(5, 6);
cout<<"Don't put zeroes for blank spaces.";
gotoxy(5, 7);
cout<<"Scroll in the grid using arrow keys";
gotoxy(5, 8);
textcolor(BLINK);
cout<<"Press any key to continue";
getch();
textcolor(4);
clrscr();
getarray(array);
//to check whether this s valid input

for(int i=0; i<=8; i++)


{
for(int j=0; j<=8; j++)
{
if(array[i][j]!=0)
{
if(isValid(array)==0)
{
clrscr();
gotoxy(35, 13);
cout<<"Invalid Input";
delay(2000);
goto End;
}
}
else
;
}
}
clrscr();
SolutionCheck:
gotoxy(5,8);
cout<<"Checking Solution";
delay(2000);
clrscr();
if (checksol(array) == 1)
{
boxes(2);
putarray(array);
}
else
{
gotoxy(5, 13);
cout<<"No solution exists";
}
End:
getch();
}

Вам также может понравиться