-
Notifications
You must be signed in to change notification settings - Fork 0
/
su.c
92 lines (86 loc) · 1.51 KB
/
su.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
#include<conio.h>
#include<graphics.h>
int a[10][10],ans[10][10];
void drawbox();
void fill(int [10][10],int [10][10],int);
void readsudoku()
{
int i,j,k;
char line[25];
FILE *fin=fopen("input.txt","r");
for(i=0;i<9;++i)
{
fgets(line,25,fin);
for(j=0,k=0;j<9;++j)
ans[i][j]=a[i][j]=line[k++]-48;
}
}
int place(int n,int p,int q)
{
int i,j,x,y;
for(i=0;i<9;++i)
{
if(ans[p][i]==n)
return 0;
}
for(i=0;i<9;++i)
{
if(ans[i][q]==n)
return 0;
}
x=(p/3)*3; y=(q/3)*3;
for(i=x;i<x+3;++i)
for(j=y;j<y+3;++j)
if(ans[i][j]==n)
return 0;
return 1;
}
void print_ans()
{
int i,j;
for(i=0;i<9;++i)
{
printf("\n");
for(j=0;j<9;++j)
printf("%d ",ans[i][j]);
}
printf("\n\n");
}
void solve_sudoku(int n)
{
int p,q,i;
if(n==81)
{
print_ans();
fill(a,ans,LIGHTGRAY);
exit(0);
}
p=n/9; q=n%9;
if(ans[p][q]==0)
{
for(i=1;i<=9;++i)
{
if(place(i,p,q))
{
ans[p][q]=i;
solve_sudoku(n+1);
ans[p][q]=0;
}
}
}
else
solve_sudoku(n+1);
}
int main()
{
initwindow(600,600,"SU-DO-KU");
readsudoku();
print_ans();
fill(a,ans,BLACK);
printf("\nThe Solved Su-do-ku:\n");
solve_sudoku(0);
getch();
}