-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.c
145 lines (129 loc) · 2.21 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <unistd.h>
#include <stdio.h>
#include <string.h>
char *ft_strncpy(char *s1, const char *s2, size_t n)
{
if (!s1 || !s2)
return (NULL);
while (n > 0)
{
--n;
*(s1 + n) = *(s2 + n);
}
return (s1);
}
void ft_bzero(void *s, size_t n)
{
if (!s)
return ;
while (n > 0)
{
--n;
*((char *)s + n) = '\0';
}
}
void ft_strtabcpy(char (*newtab)[9][9], char **strtab)
{
int i;
i = 0;
while (i < 9)
{
ft_strncpy((*newtab)[i], strtab[i], 9);
++i;
}
}
int ft_checklines(char **lines)
{
char is_used[9];
int i;
int j;
i = 0;
while (i < 9)
{
ft_bzero(is_used, sizeof(char) * 9);
j = 0;
while (j < 9)
{
if (lines[i][j] <= '9' && lines[i][j] > '0')
{
if (!is_used[(int)(lines[i][j] - '1')])
is_used[(int)(lines[i][j] - '1')] = '1';
else
return (-1);
}
else if (lines[i][j] != '.')
return (-2);
++j;
}
++i;
}
return (0);
}
int ft_checknb(char **grid, int pos, char digit)
{
int i;
int xstart;
int ystart;
xstart = ((pos % 9) / 3) * 3;
ystart = ((pos / 9) / 3) * 3;
i = 0;
while (i < 9)
{
if (grid[i][pos % 9] == digit
|| grid[pos / 9][i] == digit
|| grid[ystart + i / 3][xstart + i % 3] == digit)
return (1);
++i;
}
return (0);
}
void ft_putgrid(char grid[9][9])
{
int i;
i = 0;
while (i < 9)
{
write(1, grid[i], 9);
write(1, "\n", 1);
++i;
}
}
int ft_solve(char **grid, int pos, int *solved, char (*res_p)[9][9])
{
char c;
c = '0';
if (pos == 81)
{
++(*solved);
ft_strtabcpy(res_p, grid);
return (1);
}
if (grid[pos / 9][pos % 9] != '.')
return (ft_solve(grid, pos + 1, solved, res_p));
while (++c <= '9')
{
if (!ft_checknb(grid, pos, c))
{
grid[pos / 9][pos % 9] = c;
if (ft_solve(grid, pos + 1, solved, res_p))
{
if (*solved > 1)
return (*solved);
}
grid[pos / 9][pos % 9] = '.';
}
}
return (*solved);
}
int main(int argc, char **argv)
{
char result[9][9];
int i;
i = 0;
if (argc != 10 || ft_checklines(argv + 1)
|| 1 != ft_solve(argv + 1, 0, &i, &result))
write(1, "Erreur\n", 7);
else
ft_putgrid(result);
return (0);
}