-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve.c
148 lines (133 loc) · 4.13 KB
/
solve.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
/*
* sku - analysis tool for Sudoku puzzles
* Copyright (C) 2005 Richard P. Curnow
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include "sku.h"
static int count_marked_cells(struct layout *lay, int *state)/*{{{*/
{
int nc = lay->nc;
int result = 0;
int i;
for (i=0; i<nc; i++) {
if (state[i] == CELL_MARKED) {
result++;
}
}
return result;
}
/*}}}*/
void solve_minimal(struct layout *lay, int *state, const struct constraint *simplify_cons, int options)/*{{{*/
{
int n_solutions;
int *copy, *copy0;
char *barred;
int next_to_bar;
copy = new_array(int, lay->nc);
copy0 = new_array(int, lay->nc);
barred = new_array(char, lay->nc);
memset(barred, 0, lay->nc);
memcpy(copy, state, lay->nc * sizeof(int));
n_solutions = infer(lay, copy, NULL, NULL, simplify_cons, options);
if (n_solutions != 1) {
fprintf(stderr, "Cannot produce minimal solution unless puzzle has a unique solution\n");
goto get_out;
}
memcpy(copy0, state, lay->nc * sizeof(int));
for (next_to_bar = 0; next_to_bar < lay->nc; next_to_bar++) {
if (state[next_to_bar] >= 0) continue;
if (state[next_to_bar] == CELL_MARKED) continue;
memcpy(copy, copy0, lay->nc * sizeof(int));
copy[next_to_bar] = CELL_BARRED;
n_solutions = infer(lay, copy, NULL, NULL, simplify_cons, options);
if (n_solutions == 1) {
barred[next_to_bar] = 1;
copy0[next_to_bar] = CELL_BARRED;
}
}
display(stdout, lay, copy0);
memcpy(state, copy0, lay->nc * sizeof(int));
get_out:
free(copy);
free(copy0);
free(barred);
return;
}
/*}}}*/
void solve(const struct constraint *simplify_cons, int options)/*{{{*/
{
int *state;
int n_solutions;
struct layout *lay;
int n_marked;
read_grid(&lay, &state, options);
n_marked = count_marked_cells(lay, state);
if (n_marked > 0) {
fprintf(stderr, "Found %d marked cell%s to solve for\n",
n_marked,
n_marked==1 ? "" : "s");
options |= OPT_SOLVE_MARKED;
}
setup_terminals(lay);
if (options & OPT_SOLVE_MINIMAL) {
if (n_marked) {
solve_minimal(lay, state, simplify_cons, options);
} else {
fprintf(stderr, "-M specified but puzzle is not marked\n");
exit(1);
}
} else {
n_solutions = infer(lay, state, NULL, NULL, simplify_cons, options);
if (n_solutions == 0) {
fprintf(stderr, "The puzzle had no solutions.\n"
"Showing how far the solver got before becoming stuck.\n");
display(stdout, lay, state);
} else if (n_solutions == 1) {
fprintf(stderr, "The puzzle had precisely 1 solution\n");
display(stdout, lay, state);
} else {
if (options & OPT_SHOW_ALL) {
fprintf(stderr, "The puzzle had %d solutions\n", n_solutions);
} else {
fprintf(stderr, "The puzzle had %d solutions (one is shown)\n", n_solutions);
display(stdout, lay, state);
}
}
}
free(state);
free_layout(lay);
return;
}
/*}}}*/
void solve_any(int options)/*{{{*/
{
int *state;
int n_solutions;
struct layout *lay;
read_grid(&lay, &state, options);
setup_terminals(lay);
n_solutions = infer(lay, state, NULL, NULL, &cons_all, OPT_SPECULATE | OPT_FIRST_ONLY | options);
if (n_solutions == 0) {
fprintf(stderr, "The puzzle had no solutions.\n"
"Showing how far the solver got before becoming stuck.\n");
}
display(stdout, lay, state);
free(state);
free_layout(lay);
return;
}
/*}}}*/
/* ============================================================================ */