Hidden Text: Show
- Code: Select all
unsigned long long solveSingles(const char* in)
{
game g = defaultGame; //start from a copy of the empty game
g.maxSolutions = 1;
init(g, in); //set all known digits
if(g.mode & MODE_STOP_PROCESSING) //invalid or solved by direct eliminations
return g.nSolutions;
checkForLastOccurenceInGroup(g);
if(g.mode & MODE_STOP_PROCESSING) //invalid or solved by singles
return g.nSolutions;
return 0; //no solution
}
extern int solverBackdoor(char* in, const bool verbose) {
char sol[256];
int found = 0;
if(solve(in, 2, sol) != 1)
return -1;
if(solveSingles(in)) { //solved with singles, no need of backdoor examination
return 0;
}
for(int i = 0; i < 81; i++) {
if(in[i])
continue;
in[i] = sol[i];
if(solveSingles(in)) { //cell at i is a backdoor
found = 1;
goto level1_done;
}
in[i] = 0;
}
level1_done:
if(found)
return 1;
for(int i = 0; i < 80; i++) {
if(in[i])
continue;
in[i] = sol[i];
for(int j = i + 1; j < 81; j++) {
if(in[j])
continue;
in[j] = sol[j];
if(solveSingles(in)) { //cells at i,j are a backdoor
found = 1;
goto level2_done;
}
in[j] = 0;
}
in[i] = 0;
}
level2_done:
if(found)
return 2;
for(int i = 0; i < 79; i++) {
if(in[i])
continue;
in[i] = sol[i];
for(int j = i + 1; j < 80; j++) {
if(in[j])
continue;
in[j] = sol[j];
for(int k = j + 1; k < 81; k++) {
if(in[k])
continue;
in[k] = sol[k];
if(solveSingles(in)) { //cells at i,j,k are a backdoor
found = 1;
goto level3_done;
}
in[k] = 0;
}
in[j] = 0;
}
in[i] = 0;
}
level3_done:
if(found)
return 3;
for(int i = 0; i < 78; i++) {
if(in[i])
continue;
in[i] = sol[i];
for(int j = i + 1; j < 79; j++) {
if(in[j])
continue;
in[j] = sol[j];
for(int k = j + 1; k < 80; k++) {
if(in[k])
continue;
in[k] = sol[k];
for(int l = k + 1; l < 81; l++) {
if(in[l])
continue;
in[l] = sol[l];
if(solveSingles(in)) { //cells at i,j,k,l are a backdoor
found = 1;
//dump the backdoor
if(verbose) {
printf("\n%d\t%d\t%d\t%d",i, j, k, l);
}
else
goto level4_done;
}
in[l] = 0;
}
in[k] = 0;
}
in[j] = 0;
}
in[i] = 0;
}
level4_done:
if(found)
return 4;
return 5;
}
Yes, some functions not included here are used, but they are in the published source of gridchecker.
Function input is array with char(0) for non-givens, char(1) for "1", etc. Note that the input is destroyed so first print the puzzle and then call the function.