Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "heap.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- const int eur_fun[8][9] =
- { {0, 1, 2, 1, 2, 3, 2, 3, 4 },
- {1, 0, 1, 2, 1, 2, 3, 2, 3 },
- {2, 1, 0, 3, 2, 1, 4, 3, 2 },
- {1, 2, 3, 0, 1, 2, 1, 2, 3 },
- {2, 1, 2, 1, 0, 1, 2, 1, 2 },
- {3, 2, 1, 2, 1, 0, 3, 2, 1 },
- {2, 3, 4, 1, 2, 3, 0, 1, 2 },
- {3, 2, 3, 2, 1, 2, 1, 0, 1 } };
- heap_t* queue;
- int euristic(status_t* s) {
- int i, ret;
- ret = 0;
- for (i = 0; i < 9; i++) {
- if (s->conf[i] != 0) {
- ret += eur_fun[s->conf[i]-1][i];
- }
- }
- return ret;
- }
- void print_status(status_t* status) {
- int i = 0;
- printf("%d %d %d\n", status->conf[0], status->conf[1], status->conf[2]);
- printf("%d %d %d\n", status->conf[3], status->conf[4], status->conf[5]);
- printf("%d %d %d\n\n", status->conf[6], status->conf[7], status->conf[8]);
- fflush(stdout);
- }
- int is_obiettivo(status_t* status) {
- int i;
- for (i = 0; i < 8; i++) if (status->conf[i] != i+1) return 0;
- return 1;
- }
- int find_zero (unsigned short* conf) {
- int i;
- for (i = 0; i < 9; i++) if (conf[i] == 0) return i;
- }
- void scambia(unsigned short* array, int a, int b) {
- unsigned short tmp;
- tmp = array[a];
- array[a] = array[b];
- array[b] = tmp;
- }
- status_t* calculate_solution(status_t* status) {
- status_t* s;
- wstatus_t* w;
- int zero;
- unsigned short tmp;
- s = status;
- while (!is_obiettivo(s)) {
- /* print_status(s); */
- /* Calcola alternative */
- /* printf("Calcolo alternative...\n"); */
- zero = find_zero(s->conf);
- switch (zero) {
- case 0:
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 0, 1);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 0, 3);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- break;
- case 1:
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 1, 0);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 1, 2);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 1, 4);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- break;
- case 2:
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 2, 1);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 2, 5);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- break;
- case 3:
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 3, 0);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 3, 4);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 3, 6);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- break;
- case 4:
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 4, 1);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 4, 3);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 4, 5);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 4, 7);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- break;
- case 5:
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 5, 2);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 5, 4);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 5, 8);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- break;
- case 6:
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 6, 3);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 6, 7);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- break;
- case 7:
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 7, 4);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 7, 6);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 7, 8);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- break;
- case 8:
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 8, 5);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- w = (wstatus_t*)malloc(sizeof(wstatus_t));
- w->status = (status_t*)malloc(sizeof(status_t));
- w->status->conf = (unsigned short*)malloc(9*sizeof(unsigned short));
- w->status->distance = s->distance + 1;
- w->status->padre = s;
- memcpy(w->status->conf, s->conf, 9*sizeof(unsigned short));
- scambia(w->status->conf, 8, 7);
- heap_add(queue, w, w->status->distance + euristic(w->status));
- break;
- }
- /* Calcola peso */
- /* Inserisci in coda */
- /* Estrai dalla coda */
- heap_heapify(queue, 0);
- /* heap_print(queue); */
- /* printf("\n"); */
- w = heap_extract(queue);
- s = w->status;
- }
- return s;
- }
- int main (int argc, char* argv[]) {
- status_t* start;
- status_t* sol;
- /* unsigned short conf[9] = {1, 2, 3, 4, 5, 6, 0, 7, 8}; */
- /* unsigned short conf[9] = {4, 7, 8, 3, 2, 1, 5, 6, 0}; */
- /* unsigned short conf[9] = {6, 2, 3, 5, 7, 4, 0, 1, 8}; */
- /* unsigned short conf[9] = {4, 7, 8, 2, 6, 3, 1, 5, 0}; */
- unsigned short conf[9] = {7, 6, 0, 4, 2, 1, 8, 5, 3};
- queue = heap_new();
- start = (status_t*)malloc(sizeof(status_t));
- start->conf = (unsigned short*)conf;
- start->distance = 0;
- start->padre = NULL;
- sol = calculate_solution(start);
- do {
- print_status(sol);
- sol = sol->padre;
- } while (sol != NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement