Nonogram solver: Difference between revisions

Line 63:
===The Solver===
<lang cpp>
template<uint _N, uint _G> class Nonogram { // _N rows of _G cols
enum class ng_val : char {X='#',B='.',V='?'};
template<uint _NG> struct N {
Line 71:
std::vector<int> ng;
int En, gNG;
void fn (const int n,const int i,const int g,const int e,const int l){ // n=index to ng; i=sum of ng so far; g=start pos for output; e=ng[n];
if (fe(g,l,false) and fe(g+l,e,true)){
if ((n+1) < ng.size()) {if (fe(g+e+l,1,false)) fn(n+1,i-e-1,g+e+l+1,ng[n+1],0);}
Line 82:
if (l<=gNG-g-i-1) fn(n,i,g,e,l+1);
}
void fi (const int n,const bool g) {X.set(n,g); B.set(n, not g);} //setCell
ng_val fg (const int n) const{return (X.test(n))? ng_val::X : (B.test(n))? ng_val::B : ng_val::V;} //cell
inline bool fe (const int n,const int i, const bool g){ //n=start pos, i=num bits, g=set or unset bits
for (int e = n;e<n+i;++e) if ((g and fg(e)==ng_val::B) or (!g and fg(e)==ng_val::X)) return false; else T[e] = g;
2,172

edits