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
|
/// solves the puzzle with bruteforce. called recursively
void solve_bruteforce(size_t r, size_t c)
{
//puzzle already solved
if( this->done )
return;
// matrix completely filled. The puzzle is solved
if( r==this->n )
{
this->done=true;
return;
}
debug_out(r,c);
//Move to the next row
if( c==this->n )
{
this->solve_bruteforce(r+1,0);
}
else if( this->array[r][c]==0 )
{ //Empty position
size_t i;
for(i=1; i<=this->n; ++i)
{
//We have already used this number in this row/column/square
if( is_set( row_mask[r], i) ||
is_set( col_mask[c], i) ||
is_set( sqr_mask[c/this->b + this->b*(r/this->b)], i) )
continue;
do_set(row_mask[r], i);
do_set(col_mask[c], i);
do_set(sqr_mask[c/this->b + this->b*(r/this->b)], i);
this->array[r][c]=i;
this->solve_bruteforce(r,c+1);
if( this->done )
return;
this->array[r][c]=0;
do_unset(row_mask[r], i);
do_unset(col_mask[c], i);
do_unset(sqr_mask[c/this->b + this->b*(r/this->b)], i);
}
}
else
{ //Position already filled
this->solve_bruteforce(r,c+1);
}
} |
Partager