Skip to content

Instantly share code, notes, and snippets.

@WillyPillow
Last active December 12, 2019 10:02
Show Gist options
  • Save WillyPillow/a5b4f12faefd66c7ce42690668c002b5 to your computer and use it in GitHub Desktop.
Save WillyPillow/a5b4f12faefd66c7ce42690668c002b5 to your computer and use it in GitHub Desktop.
NTU ADA2019 HW4 Problem A Library
#ifndef DIMACS
#include <cryptominisat5/cryptominisat.h>
#else
#include <fstream>
#include <vector>
#endif
namespace sat {
#ifndef DIMACS
CMSat::SATSolver solver;
std::vector<CMSat::Lit> lit_buf;
void Init(int n) { solver.new_vars(n + 1); }
void AddClause(std::vector<int> v) {
lit_buf.clear();
lit_buf.reserve(v.size());
for (int x : v) lit_buf.emplace_back(abs(x), x < 0);
solver.add_clause(lit_buf);
}
bool Solve() { return solver.solve() == CMSat::l_True; }
int GetResult(int id) {
auto r = solver.get_model()[id];
return r == CMSat::l_True ? 1 : r == CMSat::l_False ? -1 : 0;
}
#else
std::vector<std::vector<int>> clauses;
int n_vars;
void Init(int n) { n_vars = n; }
void AddClause(std::vector<int> v) { clauses.emplace_back(std::move(v)); }
bool Solve() {
std::fstream fs("out.dimacs", fs.trunc | fs.out);
fs << "p cnf " << n_vars << ' ' << clauses.size() << '\n';
for (auto &v : clauses) {
for (auto x : v) fs << x << ' ';
fs << "0\n";
}
fs.close();
exit(0);
}
int GetResult(int id) { return 0; }
#endif // DIMACS
} // namespace sat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment