-
Notifications
You must be signed in to change notification settings - Fork 1
/
puzzle.cpp
187 lines (175 loc) · 4.88 KB
/
puzzle.cpp
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
182
183
184
185
186
187
/****************************************************************************
*
* This file may be used under the terms of the GNU General Public
* License version 2.0 as published by the Free Software Foundation
* and appearing in the file LICENSE.GPL included in the packaging of
* this file.
*
* author: Andreas Bricelj
* history: initial version 1.0 09/21/2005
* several updates 1.1 09/12/2006
*
* ****************************************************************************/
#include "puzzle.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <bitset>
#include <vector>
#include <QCoreApplication>
using namespace std;
Puzzle::Puzzle() {
for (int j=0; j<9; j++)
for (int i=0; i<9; i++)
field[i][j]=0;
}
int Puzzle::get(int row, int column) {
return field[row][column];
}
void Puzzle::set(int row, int column, int value) {
field[row][column]=value;
}
int Puzzle::getChoices(int i, int j) {
int si=3*(i/3);
int sj=3*(j/3);
int v;
int res=0x1ff;
for (int t=0; t<9; t++) {
if ((v=field[i][t])) res&=~(1<<(v-1));
if ((v=field[t][j])) res&=~(1<<(v-1));
if ((v=field[si+t/3][sj+t%3])) res&=~(1<<(v-1));
if (!res) break;
}
return res;
}
bool Puzzle::getNextChoices(int& ri, int& rj, int& rchoices) {
int minNrChoices=0;
rchoices=0;
for (int i=0; i<9; i++)
for (int j=0; j<9; j++)
if (!field[i][j]) {
int choices=getChoices(i,j);
int nrChoices=0;
if (!choices) {
rchoices=0;
return true;
}
for (int b=0; b<9; b++)
if (choices&(1<<b))
nrChoices++;
if (rchoices==0) {
ri=i; rj=j;
rchoices=choices;
minNrChoices=nrChoices;
} else {
if (minNrChoices>nrChoices) {
ri=i; rj=j;
rchoices=choices;
minNrChoices=nrChoices;
}
}
if (minNrChoices==1) {
return true;
}
}
return minNrChoices>0;
}
int Puzzle::solve(bool countOnly,bool start) {
if (start) {
int v;
for (int i=0; i<9; i++)
for (int j=0; j<9; j++)
if ((v=field[i][j])) {
field[i][j]=0;
int choices=getChoices(i,j);
field[i][j]=v;
if (!(choices&(1<<(v-1))))
return 0;
}
}
int i,j,choices;
if (getNextChoices(i,j,choices)) {
if (!choices)
return 0;
int solcount=0;
int solution=0;
for (int s=1; (s<10) && (choices!=0); s++)
if (choices&(1<<(s-1))) {
choices&=~(1<<(s-1));
field[i][j]=s;
int numsolutions=solve(true,false);
if (numsolutions==1)
solution=s;
solcount+=numsolutions;
field[i][j]=0;
if (solcount>1) {
return solcount;
}
}
if ((solcount==1) && !countOnly) {
field[i][j]=solution;
solve(false,false);
}
return solcount;
}
return 1;
}
void Puzzle::generate(int nr)
{
// nr=13946;
typedef bitset<9> BS;
BS rows[9], cols[9], quad[9];
srand(nr);
start:
//reset field
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
for (int j=0; j<9; j++)
for (int i=0; i<9; i++) {
field[i][j]=0;
cols[i].reset();
rows[j].reset();
quad[i/3+3*(j/3)].reset();
}
//try to generate a valid solution
for (int j=0; j<9; j++)
for (int i=0; i<9; i++) {
BS rest=~(cols[i] | rows[j] | quad[i/3+3*(j/3)]);
int c=rest.count();
if (c==0) {
// this does not lead to something solvable, restart
goto start;
}
int cnt= rand()%c +1;
int idx=0;
while (cnt>0 && idx<9) {
if (rest[idx]) {
cnt--;
if (cnt==0) {
cols[i][idx]=1;
rows[j][idx]=1;
quad[i/3+3*(j/3)][idx]=1;
break;
}
}
idx++;
}
field[i][j]=++idx;
}
QCoreApplication::processEvents(QEventLoop::ExcludeUserInputEvents);
vector<int> rest;
for (int i=0; i<9; i++)
for (int j=0; j<9; j++)
if (field[i][j])
rest.push_back((i<<8)+j);
while (rest.size()>0) {
int r=rand()%rest.size();
int i=rest.at(r)>>8;
int j=rest.at(r)&0xff;
rest.erase(rest.begin()+r);
int s=field[i][j];
field[i][j]=0;
int solutions=solve(true);
if (solutions>1)
field[i][j]=s;
}
}