-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic_unit.c
73 lines (65 loc) · 1.3 KB
/
logic_unit.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
#include<stdio.h>
int safe(int**inputArray,int row, int col, int n)
{
int r, c, br, bc;
if (inputArray[row][col] == n)
return 1;
if (inputArray[row][col] != 0)
return 0;
for (c = 0; c < 9; c++)
if (inputArray[row][c] == n)
return 0;
for (r = 0; r < 9; r++)
if (inputArray[r][col] == n)
return 0;
br = row / 3;
bc = col / 3;
for (r = br * 3; r < (br + 1) * 3; r++)
for (c = bc * 3; c < (bc + 1) * 3; c++)
if (inputArray[r][c] == n)
return 0;
return 1;
}
int Full(int **inputArray)
{
for(int i=0;i<9;++i)
{
for(int j=0;j<9;++j)
{
if(inputArray[i][j]==0)
{
return(0);
}
}
}
return(1);
}
int**parnianSolve(int** inputArray,int row, int col)
{
//printf("%d %d\n",row,col);
int n, temp;
if (row == 9)
{
return (inputArray);
}
for (n = 1; n <= 9; n++)
if (safe(inputArray,row, col, n))
{
temp = inputArray[row][col];
inputArray[row][col] = n;
if (col == 8)
parnianSolve(inputArray,row + 1, 0);
else
parnianSolve(inputArray,row, col + 1);
if(Full(inputArray)==1)
{
return(inputArray);
}
inputArray[row][col] = temp;
}
}
int** solve(int** inputArray,int n)
{
//printf("injaaa");
return(parnianSolve(inputArray,0,0));
}