Esempio n. 1
0
void
problem_solutions_iterate( Problem *problem, int (*callback)(Solution *s, void *user_data ), void *user_data )
{
  if (!callback) /* no use to iterate without callback */
    return;

  Id solution = 0;
  
  while ((solution = solver_next_solution( problem->solver, problem->id, solution )) != 0)
    {
      Solution *s = solution_new( problem, solution );
      if (callback( s, user_data ))
	break;
    }
}
Esempio n. 2
0
solution_t* solution_cpy(solution_t* dst, solution_t* src) {
      int i,j;

     if (dst!=NULL) {
          solution_free(dst);
          dst=NULL;
     }
          dst=solution_new(src->n);
     dst->matches=src->matches;
     for (i=0;i<dst->n;i++) {
          for (j=0;j<dst->n;j++) {
               dst->mat[i][j]=piece_new(src->mat[i][j]->square_index,src->mat[i][j]->rotation);
          }
     }
     return(dst);
}
Esempio n. 3
0
solution_t* solution_greedy(int n) {
     solution_t *s;
     int i, dim, j=0, r=0, c=0;
     int corner=0, edge=0;

     s=solution_new(n);
     dim=n*n;
     for (i=0; i<dim; i++) {
          if (squares[i]->type==CORNER) {
               switch (corner) {
                    case 0:
                         s->mat[0][0]=piece_new(i, square_rotate_corner(squares[i],UPPER_LEFT));
                         //DEBUG ROTATION
                         /*printf("%d %d\n",squares[i]->colour[ROTATE(s->mat[0][0]->rotation,LEFT)],
                                squares[i]->colour[ROTATE(s->mat[0][0]->rotation,UP)]);*/
                         corner++;
                         break;
                    case 1:
                         s->mat[0][n-1]=piece_new(i, square_rotate_corner(squares[i],UPPER_RIGHT));
                         //DEBUG ROTATION
                         /*printf("%d %d\n",squares[i]->colour[ROTATE(s->mat[0][n-1]->rotation,UP)],
                                squares[i]->colour[ROTATE(s->mat[0][n-1]->rotation,RIGHT)]);*/
                         corner++;
                         break;
                    case 2:
                         s->mat[n-1][0]=piece_new(i, square_rotate_corner(squares[i],LOWER_LEFT));
                         //DEBUG ROTATION
                         /*printf("%d %d\n",squares[i]->colour[ROTATE(s->mat[n-1][0]->rotation,DOWN)],
                                squares[i]->colour[ROTATE(s->mat[n-1][0]->rotation,LEFT)]);*/
                         corner++;
                         break;
                    case 3:
                         s->mat[n-1][n-1]=piece_new(i,square_rotate_corner(squares[i],LOWER_RIGHT));
                         //DEBUG ROTATION
                         /*printf("%d %d\n",squares[i]->colour[ROTATE(s->mat[n-1][n-1]->rotation,RIGHT)],
                                squares[i]->colour[ROTATE(s->mat[n-1][n-1]->rotation,DOWN)]);*/
                         //corner++; //DEBUG
                         break;
               }
          } else  if (squares[i]->type==EDGE) {
               switch (edge) {
                    case 0:
                         //UP EDGE
                         s->mat[0][1+j]=piece_new(i,square_rotate_edge(squares[i],UP));
                         //DEBUG ROTATION
                         //printf("%d\n",squares[i]->colour[ROTATE(s->mat[0][1+j]->rotation,UP)]);
                         j++;
                         if (j==n-2) {
                              j=0;
                              edge++;
                         }
                         break;
                    case 1:
                         //LEFT EDGE
                         s->mat[1+j][0]=piece_new(i,square_rotate_edge(squares[i],LEFT));
                         //DEBUG ROTATION
                         //printf("%d\n",squares[i]->colour[ROTATE(s->mat[1+j][0]->rotation,LEFT)]);
                         j++;
                         if (j==n-2) {
                              j=0;
                              edge++;
                         }
                         break;
                    case 2:
                         //RIGHT EDGE
                         s->mat[1+j][n-1]=piece_new(i,square_rotate_edge(squares[i],RIGHT));
                         //DEBUG ROTATION
                         //printf("%d\n",squares[i]->colour[ROTATE(s->mat[1+j][n-1]->rotation,RIGHT)]);
                         j++;
                         if (j==n-2) {
                              j=0;
                              edge++;
                         }
                         break;
                    case 3:
                         //DOWN EDGE
                         s->mat[n-1][1+j]=piece_new(i,square_rotate_edge(squares[i],DOWN));
                         //DEBUG ROTATION
                         //printf("%d\n",squares[i]->colour[ROTATE(s->mat[n-1][1+j]->rotation,DOWN)]);
                         j++;
                         //DEBUG
                         /*if (j==n-2) {
                              j=0;
                              edge++;
                         }*/
                         break;
               }
          } else {
               //INNER
               s->mat[1+r][1+c]=piece_new(i,0);
               c++;
               if (c==n-2) {
                    r++;
                    c=0;
               }
          }
     }

/*
     //DEBUG FOR MOVE_MATCHES_DIFF
     s->mat[1][2]->rotation=1;
     //pay attention: the swap_rotate do not
     //consider the initial offset of the piece
     //rot=0 corrsponds to the original piece
     //and not this one that is already rotate by 1
     //and is shown in the visualizer
*/
     s->matches=solution_matches(s);


     //DEBUG PIECE COUNT MATCHES
/*
     //CORNER

     printf("The upper-left corner piece gives %d matches\n",solution_piece_matches_corner(s,NULL,0));
     printf("The upper-right corner piece gives %d matches\n",solution_piece_matches_corner(s,NULL,4));
     printf("The lowet-left corner piece gives %d matches\n",solution_piece_matches_corner(s,NULL,20));
     printf("The lower-right corner piece gives %d matches\n",solution_piece_matches_corner(s,NULL,24));
*/
/*
     //0 MATCHES
     piece_t* tmp;
     tmp=s->mat[1][0];
     s->mat[1][0]=s->mat[3][0];
     s->mat[3][0]=tmp;
     printf("The upper-left corner piece gives %d matches\n",solution_piece_matches_corner(s,NULL,0));
*/
/*
     //2 MATCHES
     piece_t* tmp;
     tmp=s->mat[0][1];
     s->mat[0][1]=s->mat[0][2];
     s->mat[0][2]=tmp;
     printf("The upper-left corner piece gives %d matches\n",solution_piece_matches_corner(s,NULL,0));
*/
/*
     //INNER
     printf("The inner piece 6 (1,1) gives %d matches\n",solution_piece_matches_inner(s,NULL,6)); //0
     printf("The inner piece 16 (2,3) gives %d matches\n",solution_piece_matches_inner(s,NULL,13)); //1
     printf("The inner piece 16 (3,1) gives %d matches\n",solution_piece_matches_inner(s,NULL,16)); //2
*/
/*
     //EDGE
     printf("The edge piece 5 (1,0) gives %d matches\n",solution_piece_matches_edge(s,NULL,5)); //1
     printf("The edge piece 14 (2,4) gives %d matches\n",solution_piece_matches_edge(s,NULL,14)); //3
*/
/*
    // DEBUG COUNT MATCHES
    int matches;
    piece_t* tmp;
    tmp=s->mat[3][0];
    s->mat[3][0]=s->mat[2][0];
    s->mat[2][0]=tmp;
    matches=solution_matches(s);
    printf("Current solution: %d\n",matches); //14
*/

     return(s);
}