// checker.cpp // C++11 testlib special judge for the Slitherlink-like problem. // Fully reproduces behavior of the original Python+Z3 checker. // Requires testlib.h in include path. #include "testlib.h" #include using namespace std; const int N = 12; const vector M = { "? ? ??? ", "?? ?? ? ?", "? ? ? ? ?", "? ? ? ???? ", "? ? ? ? ", "? ? ? ", " ", "? ? ?????", "? ? ? ", "?? ? ? ? ", "? ? ? ? ? ", "? ? ??? ? " }; string valid_char; vector grid; // helper printing to stderr (like original) void eprint(const string &s){ cerr << s << "\n"; } void myAssert(bool cond, const string &msg){ if(!cond) { // In SPJ we usually call quitf for WA, but here follow original: print to stderr and exit quitf(_pe, "Assertion failed: %s", msg.c_str()); } } string clean_line(string line){ const string allowed = " 0123"; while(!line.empty() && allowed.find(line.front())==string::npos) line.erase(line.begin()); while(!line.empty() && allowed.find(line.back())==string::npos) line.pop_back(); return line; } vector readSol(InStream &in){ vector res; for(int i=0;i> adj_cells; // adjacent cells (r,c) vector> adj_points; // endpoints (pi,pj) int score; // heuristic: number of adjacent numbered cells }; vector edges; int Ecnt; // Backtracking state vector edge_val; // -1 unassigned, 0 false, 1 true int cell_assigned_true[N][N]; int cell_unassigned[N][N]; int point_deg[N+1][N+1]; int point_unassigned[N+1][N+1]; struct Change { int type; // 0: edge val, 1: cell_assigned_true, 2: cell_unassigned, 3: point_deg, 4: point_unassigned int a; // encoded index (edge idx or r*100 + c or pi*100 + pj) int old; }; vector changes; void apply_assign(int eidx, int val){ // assume edge_val[eidx] == -1 changes.push_back({0, eidx, edge_val[eidx]}); edge_val[eidx] = val; const Edge &ed = edges[eidx]; // adjacent cells for(size_t k=0;k sz){ Change ch = changes.back(); changes.pop_back(); if(ch.type == 0){ edge_val[ch.a] = ch.old; } else if(ch.type == 1){ int r = ch.a / 100, c = ch.a % 100; cell_assigned_true[r][c] = ch.old; } else if(ch.type == 2){ int r = ch.a / 100, c = ch.a % 100; cell_unassigned[r][c] = ch.old; } else if(ch.type == 3){ int pi = ch.a / 100, pj = ch.a % 100; point_deg[pi][pj] = ch.old; } else if(ch.type == 4){ int pi = ch.a / 100, pj = ch.a % 100; point_unassigned[pi][pj] = ch.old; } } } // Propagation: enforce cell counts and point degree constraints (0 or 2) bool contradiction_check_and_propagate(){ bool changed = true; int iter = 0; while(changed){ changed = false; iter++; if(iter > 300000) break; // safety // cells for(int r=0;r need) return false; if(have + rem < need) return false; if(rem == 0){ if(have != need) return false; } else { if(have == need){ // force remaining adjacent edges false for(int e=0;e all remaining edges adjacent must be false for(int e=0;e best_score){ best_score = edges[e].score; best = e; } } if(best != -1) return best; for(int e=0;e> &H, vector> &V){ H.assign(N+1, vector(N,0)); V.assign(N, vector(N+1,0)); for(int e=0;e > > split_loops_from_assignment(){ vector > H(N+1, vector(N,0)); vector > V(N, vector(N+1,0)); for(int e=0;e > res; if(pi>0 && V[pi-1][pj]) res.push_back(make_tuple('v', pi-1, pj)); if(pi < N && V[pi][pj]) res.push_back(make_tuple('v', pi, pj)); if(pj>0 && H[pi][pj-1]) res.push_back(make_tuple('h', pi, pj-1)); if(pj < N && H[pi][pj]) res.push_back(make_tuple('h', pi, pj)); return res; }; auto remove_edge_mat = [&](char tag, int i, int j){ if(tag=='h') H[i][j] = 0; else V[i][j] = 0; }; vector > > loop_list; while(true){ bool found_edge = false; char start_tag = 0; int start_i=-1, start_j=-1; for(int i=0;i<=N && !found_edge;i++){ for(int j=0;j > loop; char prev_tag = 0; int prev_i = -1, prev_j = -1; int init_pi = cur_pi, init_pj = cur_pj; while(true){ vector< tuple > inc = incident_edges_at_point(cur_pi, cur_pj); bool moved = false; for(size_t k=0;k(inc[k]); int ei = get<1>(inc[k]); int ej = get<2>(inc[k]); if(prev_tag != 0 && t==prev_tag && ei==prev_i && ej==prev_j) continue; loop.push_back(inc[k]); remove_edge_mat(t, ei, ej); // move to other endpoint if(t=='h'){ if(cur_pi==ei && cur_pj==ej){ prev_tag = 'h'; prev_i = ei; prev_j = ej; cur_pi = ei; cur_pj = ej+1; } else { prev_tag = 'h'; prev_i = ei; prev_j = ej; cur_pi = ei; cur_pj = ej; } } else { if(cur_pi==ei && cur_pj==ej){ prev_tag = 'v'; prev_i = ei; prev_j = ej; cur_pi = ei+1; cur_pj = ej; } else { prev_tag = 'v'; prev_i = ei; prev_j = ej; cur_pi = ei; cur_pj = ej; } } moved = true; break; } if(!moved) break; if(cur_pi==init_pi && cur_pj==init_pj && !loop.empty()) break; } if(!loop.empty()) loop_list.push_back(loop); else break; } return loop_list; } // generate hv/vv from one loop and check against grid bool generateSol_from_loop(const vector< tuple > &loop, vector> &Hout, vector> &Vout){ Hout.assign(N+1, vector(N,0)); Vout.assign(N, vector(N+1,0)); vector> cnt(N, vector(N,0)); for(size_t k=0;k(loop[k]); int i = get<1>(loop[k]); int j = get<2>(loop[k]); if(tag=='h'){ Hout[i][j] = 1; if(i < N) cnt[i][j] += 1; if(i > 0) cnt[i-1][j] += 1; } else { Vout[i][j] = 1; if(j < N) cnt[i][j] += 1; if(j > 0) cnt[i][j-1] += 1; } } for(int i=0;i> &H, const vector> &V){ string s; s.reserve((N+1)*N + N*(N+1) + 10); for(int i=0;i<=N;i++){ for(int j=0;j> &H, const vector> &V){ eprint("Sol:"); for(int i=0;i<=N;i++){ string s = " "; for(int k=0;k>, vector>>> sol_list; unordered_set sol_set; // serialized strings for dedup // recursive search (stop when found >= limit_solutions) bool try_search(int limit_solutions){ int unassigned = 0; for(int e=0;e > > loops = split_loops_from_assignment(); for(size_t i=0;i> Hsol, Vsol; if(generateSol_from_loop(loops[i], Hsol, Vsol)){ string key = serialize_solution(Hsol, Vsol); if(sol_set.find(key) == sol_set.end()){ sol_set.insert(key); sol_list.push_back(make_pair(Hsol, Vsol)); if((int)sol_list.size() >= limit_solutions) return true; } } } return false; } int e = pick_next_edge(); if(e == -1) return false; for(int val = 0; val <= 1; ++val){ int save_sz = (int)changes.size(); apply_assign(e, val); bool ok = contradiction_check_and_propagate(); if(ok){ if(try_search(limit_solutions)) return true; } rollback_to(save_sz); } return false; } int main(int argc, char **argv){ registerTestlibCmd(argc, argv); // read type from input file (inf) int w = inf.readInt(); if(w == 0) valid_char = " 0123"; else valid_char = " 123"; // read contestant output (ouf) as grid grid grid = readSol(ouf); // build edge list edges.clear(); // horizontals H[0..12][0..11] for(int i=0;i<=N;i++){ for(int j=0;j0) ed.adj_cells.push_back(make_pair(i-1,j)); if(i0) ed.adj_cells.push_back(make_pair(i,j-1)); if(j=5 -> 0 points) sol_list.clear(); sol_set.clear(); const int LIMIT = 5; try_search(LIMIT); int cnt = (int)sol_list.size(); if(cnt == 0){ quitp(0.0, "There is no valid solution"); } else if(cnt == 1){ // unique -> 100% // print solution to stderr print_solution_to_stderr(sol_list[0].first, sol_list[0].second); quitp(1.0, "Ratio: 1.0 Correct! unique solution"); } else if(cnt == 2){ for(int k=0;k<2;k++) print_solution_to_stderr(sol_list[k].first, sol_list[k].second); quitp(0.80, "Ratio: 0.8 Two valid solutions"); } else if(cnt == 3){ for(int k=0;k<3;k++) print_solution_to_stderr(sol_list[k].first, sol_list[k].second); quitp(0.60, "Ratio: 0.6 Three valid solutions"); } else if(cnt == 4){ for(int k=0;k<4;k++) print_solution_to_stderr(sol_list[k].first, sol_list[k].second); quitp(0.40, "Ratio: 0.4 Four valid solutions"); } else if(cnt == 5){ for(int k=0;k<5;k++) print_solution_to_stderr(sol_list[k].first, sol_list[k].second); quitp(0.20, "Ratio: 0.2 Five valid solutions"); } else { // >=5 for(int k=0;k< (int)min((size_t)6, sol_list.size()); ++k) print_solution_to_stderr(sol_list[k].first, sol_list[k].second); quitp(0.0, "Ratio: 0.0 Six or more valid solutions (or too many)"); } return 0; }