-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.c
91 lines (82 loc) · 1.34 KB
/
sudoku.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
/*
** sudoku.c for sudoku in /home/khoual_s/Documents/Rendu/C_Prog_Elem/RUSH/sudoki-bi
**
** Made by Khoualdia Sabri
** Login <khoual_s@pc-sabri-khoualdia>
**
** Started on Sat Feb 27 10:44:21 2016 Khoualdia Sabri
** Last update Sun Feb 28 13:27:14 2016 Khoualdia Sabri
*/
#include <stdio.h>
#include <stdlib.h>
#include "sudo.h"
int missingLine(int k, int **tab, int i)
{
int j;
j = 0;
while (j < 9)
{
if (tab[i][j] == k)
return (false);
j++;
}
return (true);
}
int missingCol(int k, int **tab, int j)
{
int i;
i = 0;
while (i < 9)
{
if (tab[i][j] == k)
return (false);
i++;
}
return (true);
}
int missingBlock(int k, int **tab, int i, int j)
{
int l;
int m;
l = i - (i % 3);
m = j - (j % 3);
i = l;
j = m;
while (i < (l + 3))
{
while (j < (m + 3))
{
if (tab[i][j] == k)
return (false);
j++;
}
i++;
}
return (true);
}
int validate(int **tab, int pos)
{
int i;
int j;
int k;
if (pos == 81)
return (true);
i = pos / 9;
j = pos % 9;
if (tab[i][j])
return (validate(tab, pos + 1));
k = 1;
while (k <= 9)
{
if (missingLine(k, tab, i) && missingCol(k, tab, j)
&& missingBlock(k, tab, i, j))
{
tab[i][j] = k;
if (validate(tab, pos + 1))
return (true);
}
k++;
}
tab[i][j] = 0;
return (false);
}