-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathz.cpp
More file actions
65 lines (54 loc) · 1.2 KB
/
z.cpp
File metadata and controls
65 lines (54 loc) · 1.2 KB
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
#include "solver/algorithm/z.h"
#include <algorithm>
#include <functional>
namespace solver {
namespace algorithm {
std::pair<Result, Assignment> Z::Solve() {
std::vector<Lit> cur;
cur.reserve(NumVars());
std::function<bool(int)> Search = [&](int i) {
if (i == 0) {
return Verify(cur);
}
Var x(i);
for (auto lit : {Lit(x), Lit(~x)}) {
cur.push_back(lit);
if (Search(i - 1)) {
return true;
}
cur.pop_back();
}
return false;
};
if (Search(n_)) {
return {Result::kSAT, cur};
}
return {Result::kUNSAT, {}};
}
std::pair<Result, std::vector<Assignment>> Z::SolveAll() {
std::vector<Assignment> all;
std::vector<Lit> cur;
cur.reserve(NumVars());
std::function<void(int)> Search = [&](int i) {
if (i == 0) {
if (Verify(cur)) {
all.push_back(cur);
LOG << "solution = [" << ToString(cur) << "]";
}
return;
}
Var x(i);
for (auto lit : {Lit(x), Lit(~x)}) {
cur.push_back(lit);
Search(i - 1);
cur.pop_back();
}
};
Search(n_);
if (all.empty()) {
return {Result::kUNSAT, {}};
}
return {Result::kSAT, all};
}
} // namespace algorithm
} // namespace solver