-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolve.c
executable file
·182 lines (157 loc) · 4.45 KB
/
resolve.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*---------------------------------------------------------------------------
* Copyright (C) 2012, 2013 - Emanuele Bovisio
*
* This file is part of makedoku.
*
* makedoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* makedoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with makedoku. If not, see <http://www.gnu.org/licenses/>.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU Lesser General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*---------------------------------------------------------------------------*/
#ifdef HAVE_OPENSSL
#include <openssl/rand.h>
#endif
#include "resolve.h"
#include "sudo.h"
void print_table(unsigned char *grid, int single_line, int space)
{
int x, y;
for (x = 0; x < dim.grid; x++) {
for (y = 0; y < dim.grid; y++)
printf("%c", GRID(grid,x,y,0) ? (GRID(grid,x,y,0) == 255?'*':INT2ASCII(GRID(grid,x,y,0))) : (space?' ':'0'));
if (!single_line)
printf("\n");
}
printf("\n");
}
void print_grid_table(unsigned char *grid)
{
int x, y, z;
for (x = 0; x < dim.grid; x++) {
for (y = 0; y < dim.grid; y++) {
for (z = 0; z < dim.grid; z++)
printf("%c", GRID(grid,x,y,z) ? (GRID(grid,x,y,z) == 255?'*':INT2ASCII(GRID(grid,x,y,z))) : ' ');
printf(" ");
}
printf("\n");
}
}
// random number between 0 and max (included)
#ifdef HAVE_OPENSSL
int random_int(int max)
{
unsigned char buf[1];
static char seed = 0;
buf[0] = seed++;
RAND_seed(buf, 1);
RAND_bytes(buf, 1);
return (int) ((max + 1) * ((double)buf[0] / 256.0f));
}
#else
int random_int(int max)
{
return (int) ((max + 1) * (rand() / (RAND_MAX + 1.0)));
}
#endif
int random_start_number(int min, int max)
{
return (int)(min + random_int(max-min));
}
int random_number(void)
{
return (int)(1 + random_int(dim.grid-1));
}
int random_index(void)
{
return random_int(dim.grid-1);
}
void random_table(unsigned char *grid, int num_min, int num_max)
{
#define MAX_ATTEMPT 1000
int x, y, val, num, i, try, ko;
int used[MAX_DIM];
if (num_min < 1 || num_max > dim.number || num_max < num_min)
return;
do {
i = 0;
try = 0;
ko = 0;
RESET_GRID(grid, dim.extgrid);
memset(used, 0, (MAX_DIM)*sizeof(int));
num = random_start_number(num_min, num_max);
while (num) {
x = random_index();
y = random_index();
if (!GRID(grid,x,y,0)) {
do {
val = random_number();
} while (used[val-1] && i < dim.grid-1);
if (try++ > MAX_ATTEMPT) {
ko = 1;
break;
}
if (is_valid_number(grid, val, x, y)) {
GRID(grid,x,y,0) = val;
num--;
i++;
used[val-1] = 1;
}
}
}
} while (ko);
}
int get_initial_number(unsigned char *grid)
{
int x, y, count = 0;
for (x = 0; x < dim.grid; x++)
for (y = 0; y < dim.grid; y++)
if (GRID(grid,x,y,0))
count++;
return count;
}
int check_initial_grid(unsigned char *grid)
{
int x, y;
for (x = 0; x < dim.grid; x++)
for (y = 0; y < dim.grid; y++)
if (GRID(grid,x,y,0) && !GRID(grid,x,y,1)
&& !is_valid_number(grid, GRID(grid,x,y,0), x, y)) {
printf("x=%d,y=%d\n", x,y);
return 0;
}
return 1;
}
int check_solution(unsigned char *sol, unsigned char *grid)
{
int x, y;
for (x = 0; x < dim.grid; x++)
for (y = 0; y < dim.grid; y++)
if (!GRID(sol,x,y,0) || GRID(sol,x,y,1)
|| (GRID(grid,x,y,0) && GRID(sol,x,y,0) != GRID(grid,x,y,0))
|| !is_valid_number(sol, GRID(sol,x,y,0), x, y)) {
printf("x=%d,y=%d\n", x,y);
return 0;
}
return 1;
}