Example #1
0
int main() {

  /* an example cost matrix */
  int r[3*3] =  {20,90,10,60,30,40,90,90, 120};
  std::vector< std::vector<int> > m = array_to_matrix(r,3,3);

  /* initialize the gungarian_problem using the cost matrix*/
  Hungarian hungarian(m , 3,3, HUNGARIAN_MODE_MINIMIZE_COST) ;

  //fprintf(stderr, "assignement matrix has a now a size %d rows and %d columns.\n\n",  hungarian.ro,matrix_size);

  /* some output */
  fprintf(stderr, "cost-matrix:");
  hungarian.print_cost();

  /* solve the assignement problem */
  hungarian.solve();

  /* some output */
  fprintf(stderr, "assignment:");
  hungarian.print_assignment();


  return 0;
}
Example #2
0
/*
resolve o puzzle
leaf = open list
queue = closed list
0 = deu certo
-1 = esgotou todas as possibilidades ou entrada invalida
*/
int solve_puzzle(int **initial_state, int **final_state, heap_node **leaf, node_queue **queue, int dimension) {
    if(initial_state == NULL || final_state == NULL || dimension < 2) return -1;
    if(*initial_state == NULL || *final_state == NULL) return -1;
    if(leaf == NULL || queue == NULL) return -1;

    int *distances = calculate_manhattan_distances(initial_state, final_state, dimension);
    if(distances == NULL) return -1;
    int sum = manhattan_distances_sum(distances, dimension*dimension);
    sum += hamming_heuristic_sum(distances, dimension*dimension);
    free(distances);
    int *array = matrix_to_array(initial_state, dimension);
    if(array == NULL) return -1;
    add_priority_queue(leaf, sum, array, dimension*dimension);

    int *array_aux = (int*) extract_min(leaf);
    if(array_aux == NULL) return -1;
    int **x = array_to_matrix(array_aux, dimension);

    int i=0;
    do {
        add_clildren(x, final_state, leaf, queue, dimension);

        add_closed_set(x, queue, dimension); // rever necessidade da funcao

        if(i > 20000) {
            printf("ERROR\n");
            return -1;
        }

        if(check(x, final_state, dimension) == 0) {
            free_matrix(x, dimension);
            printf("necessary steps: %d\n", i);
            return 0;
        }
        free_matrix(x, dimension);
        x = extract_min_checked(leaf, queue, dimension);
        i++;
    } while(*leaf != NULL);
    free_matrix(x, dimension);

    return -1;
}