-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudoku.cpp
288 lines (267 loc) · 5.26 KB
/
sudoku.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include"sudoku.h"
sudoku::sudoku()
{
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
{
final[i][j] = 0;
rowCount[i+1][j] = 9;
colCount[i+1][j] = 9;
areaCount[i+1][j] = 9;
}
}
}
bool sudoku::setNum(int row, int col, int num)
{
int q = final[row][col];
;
if(final[row][col]!=0 && final[row][col]==num)
return true;
else if(final[row][col] != 0)
return false;
else if(rowCount[num][row]==0 || colCount[num][col]==0 || areaCount[num][row/3 *3+col/3] == 0)
return false;
else
final[row][col] = num;
rowCount[num][row] = 0; // 对该数的行列区的计数置为0
colCount[num][col] = 0;
areaCount[num][row/3 *3+col/3] = 0;
u[row][col].clearEnableNum();
for(int i=0; i<9; i++)
{
disEnable(row, col, i+1); // 删除该单元上所有可填的数
}
for(int i=0; i<9; i++)
{
disEnable(row, i, num);
disEnable(i, col, num);
disEnable(row/3 *3+i/3 , col/3 *3+i%3, num);
}
//this->print();
//cout<<endl;
return true;
}
bool sudoku::disEnable(int row, int col, int num) // 删除一个unit内可填的某个数的操作
{
if(u[row][col].deleteNum(num))
{
if(rowCount[num][row]!=0) // 一个可填数少掉时对行列区的Count操作
{
rowCount[num][row]--;
testRowCount(num, row);
}
if(colCount[num][col]!=0)
{
colCount[num][col]--;
testColCount(num, col);
}
if(areaCount[num][row/3 *3+col/3]!=0)
{
areaCount[num][row/3 *3+col/3]--;
testAreaCount(num, row/3 *3+col/3);
}
if(u[row][col].textEnableNum1())
add.push_back( waitUnit(row, col, u[row][col].findToPush() ) );
return true;
}
else
return false;
}
void sudoku::testRowCount(int num, int row)
{
if(rowCount[num][row] == 1)
{
for(int i=0; i<9; i++)
if( u[row][i].findNum(num) )
{
add.push_back( waitUnit(row, i, num) );
return;
}
}
if(rowCount[num][row] == 2)
{
int col[2]= {0,0};
for(int i=0; i<9&&!col[1]; i++)
{
if(u[row][i].findNum(num))
{
if(col[0])
col[1] = i;
else
col[0] = i;
}
}
if(col[0]/3 == col[1]/3)
{
int i = 0;
while(areaCount[num][row/3 *3+col[0]/3]!=2 && i<9)
{
int r = row/3 *3 + i/3;
int c = col[0]/3 *3 + i%3;
if(u[r][c].findNum(num)&& r!=row )
disEnable(r,c,num);
i++;
}
}
}
}
void sudoku::testColCount(int num, int col)
{
if(colCount[num][col] == 1)
{
for(int i=0; i<9; i++)
if( u[i][col].findNum(num) )
{
add.push_back( waitUnit(i, col, num) );
return;
}
}
if(colCount[num][col] == 2)
{
int row[2]= {0,0};
for(int i=0; i<9&&!row[1]; i++)
{
if(u[i][col].findNum(num))
{
if(row[0])
row[1] = i;
else
row[0] = i;
}
}
if(row[0]/3 == row[1]/3)
{
int i = 0;
while(areaCount[num][row[0]/3 *3+col/3]!=2 && i<9)
{
int r = row[0]/3 *3 + i/3;
int c = col/3 *3 + i%3;
if(u[r][c].findNum(num)&& c!=col )
disEnable(r,c,num);
i++;
}
}
}
}
void sudoku::testAreaCount(int num, int area)
{
if(areaCount[num][area] == 1)
{
int r = area/3 *3;
int c = area%3 *3;
for(int i=0; i<9; i++)
{
if(u[r+i/3][c+i%3].findNum(num))
{
add.push_back( waitUnit(r+i/3, c+i%3, num) );
return;
}
}
}
}
void sudoku::setAdd()
{
list<waitUnit>::iterator first = add.begin();
while(first!=add.end())
{
this->setNum(first->getRow(),first->getCol(), first->getNum());
first++;
}
}
bool sudoku::trySetAdd()
{
list<waitUnit>::iterator first = add.begin();
while(first!=add.end())
{
if( !this->setNum(first->getRow(),first->getCol(), first->getNum()) )
return false;;
first++;
}
return true;
}
void sudoku::print()
{
for(int i=0; i<9; i++)
{
for(int j=0; j<9; j++)
cout<<final[i][j]<<' ';
cout<<endl;
}
}
bool sudoku::isProgramOver()
{
for(int i=0; i<9; i++)
for(int j=0; j<9; j++)
{
if(!final[i][j])
return false;
};
return true;
}
bool sudoku::trial( int ID ) // 尝试
{
int id = ID;
int remainder = 2;
for(int num=1; num<10; num++)
for(int row=0; row<9; row++)
{
if(rowCount[num][row] == remainder)
{
for(int col=0; col<9; col++)
{
if(u[row][col].findNum(num))
if(--id)
{
add.push_back( waitUnit(row, col, num) );
if(this->trySetAdd())
return true;
else
return false;
}
}
}
};
id = ID;
for(int num=1; num<10; num++)
for(int col=0; col<9; col++)
{
if(colCount[num][col] == remainder)
{
for(int row=0; row<9; row++)
{
if(u[row][col].findNum(num))
if(--id)
{
add.push_back( waitUnit(row, col, num) );
if(this->trySetAdd())
return true;
else
return false;
}
}
}
};
for(int num=1; num<10; num++)
for(int area=0; area<9; area++)
{
if(areaCount[num][area] == remainder)
{
for(int i=0; i<9; i++)
{
int row = area/3 *3+i/3;
int col = area%3 *3+i%3;
if(u[row][col].findNum(num))
if(--id)
{
add.push_back( waitUnit(row, col, num) );
if(this->trySetAdd())
return true;
else
return false;
}
}
}
}
return false;
}