-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimpl.cpp
219 lines (202 loc) · 5.61 KB
/
impl.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
/**
* Copyright (c) 2024 Zhongxian Li
* quick sudoku is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*
*/
#include "impl.h"
#include "common.h"
#include "dfs.h"
namespace li {
namespace {
int count_notes(int r, int c, const Array9i board[]) {
if (board[0](r, c) > 0) return 0;
int res = 0;
for (int i = 1; i < 10; ++i) {
res += board[i](r, c);
}
return res;
}
void update_diff(std::vector<Weight> &samp, const Array9i &ans) {
Array9i bak;
Array9i board[10];
bak.fill(0);
for (auto &ele : samp) {
bak(ele.r, ele.c) = ans(ele.r, ele.c);
}
for (int i = samp.size() - 1; i >= 0; i--)
if (samp[i].w > 0) {
board[0] = bak;
int cur_r = samp[i].r;
int cur_c = samp[i].c;
board[0](cur_r, cur_c) = 0;
init_note(board);
fill_all_single(board);
samp[i].hash = rand();
if (!board[0](cur_r, cur_c)) {
samp[i].w = 2;
for (int j = 1; j < 10; ++j)
if (board[j](cur_r, cur_c) && j != ans(cur_r, cur_c)) {
bool isFind = false;
dfsDeduce(cur_r, cur_c, j, board, isFind);
if (isFind) {
samp[i].w = -1;
break;
}
}
}
}
}
void erase_easy(std::vector<Weight> &samp, const Array9i &ans) {
Array9i bak;
Array9i board[10];
bak.fill(0);
for (auto &ele : samp) {
bak(ele.r, ele.c) = ans(ele.r, ele.c);
}
for (int i = samp.size() - 1; i >= 0; i--)
if (samp[i].w > 0) {
board[0] = bak;
int cur_r = samp[i].r;
int cur_c = samp[i].c;
board[0](cur_r, cur_c) = 0;
init_note(board);
fill_all_single(board);
if (board[0](cur_r, cur_c)) {
bak(cur_r, cur_c) = 0;
samp.erase(samp.begin() + i);
}
}
}
struct Point {
int r;
int c;
};
std::vector<Point> note_remove(const Array9i &asp, bool once, bool cons_block = true) {
std::vector<Point> vec;
Array9i cur;
int r, c;
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
if (asp(i, j) == 1) {
cur = asp;
r = i;
c = j;
do {
cur.col(c) = 0;
cur.row(r) = 0;
if (cons_block) {
cur.block<3, 3>(r / 3 * 3, c / 3 * 3) = 0;
}
cur(r, c) = 10;
if (once) break;
} while (row_single(r, c, cur) || col_single(r, c, cur) || (cons_block && block_single(r, c, cur)));
bool modify = false;
if (cons_block) {
modify |= block_sum_0(cur);
}
if ((cur.rowwise().sum() == 0).any() || (cur.colwise().sum() == 0).any() || modify) {
vec.push_back({i, j});
}
}
}
}
return vec;
}
} // namespace
bool filter_notes(const Array9i board[], std::vector<Weight> &vec) {
vec.clear();
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
int n = board[0](i, j);
if (n <= 0) {
vec.push_back({i, j, count_notes(i, j, board), rand()});
}
}
}
return !vec.empty();
}
void always_easy(std::vector<Weight> &samp, const Array9i &ans) { erase_easy(samp, ans); }
void often_medium(std::vector<Weight> &samp, const Array9i &ans) {
while (std::any_of(samp.begin(), samp.end(), [](const Weight &wt) { return wt.w > 0; })) {
update_diff(samp, ans);
auto it = max_element(samp.begin(), samp.end());
int w = it->w;
samp.erase(it);
if (w > 1) {
break;
}
}
erase_easy(samp, ans);
}
void usually_hard(std::vector<Weight> &samp, const Array9i &ans) {
while (std::any_of(samp.begin(), samp.end(), [](const Weight &wt) { return wt.w > 0; })) {
update_diff(samp, ans);
samp.erase(max_element(samp.begin(), samp.end()));
}
}
void col_swap_block(int &r, int &c) {
int a = r / 3;
int b = c / 3;
int d = r % 3;
int e = c % 3;
c = b * 3 + a;
r = e * 3 + d;
}
bool _remove(Array9i board[], bool once) {
bool modify = false;
std::vector<Point> vec;
Array9i asp;
for (int n = 1; n < 10; n++) {
asp = (board[0] == n).cast<int>() * 10 + board[n];
vec = note_remove(asp, once);
if (!vec.empty()) modify = true;
for (auto &ele : vec) {
board[n](ele.r, ele.c) = 0;
}
}
return modify;
}
bool col_circle_remove(Array9i board[], void (*fun)(int &, int &)) {
bool modify = false;
Array9i asp;
int r, c;
std::vector<Point> vec;
for (int tid = 0; tid < 9; ++tid) {
asp.fill(0);
for (int idx = 0; idx < 9; ++idx) {
// asp(i,j) == board_view[n](r, c) == board[n](R, C)
// i = idx, j = n-1
// r = idx, c = tid
// [R, C] = fun(r,c)
r = idx;
c = tid;
if (fun) fun(r, c);
if (board[0](r, c)) {
asp(idx, board[0](r, c) - 1) = 10;
} else {
for (int n = 1; n < 10; n++) {
if (board[n](r, c)) {
asp(idx, n - 1) = 1;
}
}
}
}
vec = note_remove(asp, false, false);
if (!vec.empty()) modify = true;
for (auto &ele : vec) {
r = ele.r;
c = tid;
if (fun) fun(r, c);
board[ele.c + 1](r, c) = 0;
}
}
return modify;
}
} // namespace li