Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. typedef struct uzol{
  2.         int value;
  3.         int x, y;
  4.         char type;
  5.         int f_value;
  6.         int cena_cesty;
  7.         struct uzol *parent;
  8. }UZOL;
  9.  
  10. void graph_search(GRAF* graf, int x, int y, int konc_x, int konc_y){
  11.  
  12.     FRONT* open_list = NULL;
  13.     UZOL *akt, *ls = NULL, *ps = NULL, *hs = NULL, *ds = NULL, *pom = NULL;
  14.     int i = 0;
  15.     akt = graf->uzol[x][y];
  16.     akt->f_value = 0;
  17.     akt->cena_cesty = 0;
  18.     akt->parent = NULL;
  19.     open_list = front_insert(open_list, akt);
  20.  
  21.     while (open_list != NULL){
  22.         akt = front_return (open_list);
  23.         front_remove_min (open_list);
  24.  
  25.         if (akt->x == konc_x && akt->y == konc_y)
  26.             break;
  27.  
  28.         int cena;
  29.  
  30.         if (akt->x - 1 >= 0){                           //lavy sused
  31.  
  32.             ls = graf->uzol[akt->y][akt->x - 1];
  33.             if (ls->type == 'h')
  34.                 cena = akt->cena_cesty + 2;
  35.             else
  36.                 cena = akt->cena_cesty + 1;
  37.  
  38.             ls->cena_cesty = cena;
  39.             ls->parent = akt;
  40.             ls->f_value = cena + abs(ls->x - konc_x) + abs(ls->y - konc_y);
  41.             open_list = front_insert(open_list, ls);
  42.  
  43.         }
  44.         if (akt->x + 1 <= graf->sirka){                         //pravy sused
  45.  
  46.             ps = graf->uzol[akt->y][akt->x + 1];
  47.             if (ps->type == 'h')
  48.                 cena = akt->cena_cesty + 2;
  49.             else
  50.                 cena = akt->cena_cesty + 1;
  51.  
  52.             ps->cena_cesty = cena;
  53.             ps->parent = akt;
  54.             ps->f_value = cena + abs(ps->x - konc_x) + abs(ps->y - konc_y);
  55.             open_list = front_insert(open_list, ps);
  56.         }
  57.         if (akt->y- 1 >= 0){                            //horny sused
  58.  
  59.             hs = graf->uzol[akt->y - 1][akt->x];
  60.             if (hs->type == 'h')
  61.                 cena = akt->cena_cesty + 2;
  62.             else
  63.                 cena = akt->cena_cesty + 1;
  64.  
  65.             hs->cena_cesty = cena;
  66.             hs->parent = akt;
  67.             hs->f_value = cena + abs(hs->x - konc_x) + abs(hs->y - konc_y);
  68.             open_list = front_insert(open_list, hs);
  69.         }
  70.         if (akt->y + 1 >= 0){                           //dolny sused
  71.  
  72.             ds = graf->uzol[akt->y + 1][akt->x];
  73.             if (ds->type == 'h')
  74.                 cena = akt->cena_cesty + 2;
  75.             else
  76.                 cena = akt->cena_cesty + 1;
  77.  
  78.             ds->cena_cesty = cena;
  79.             ds->parent = akt;
  80.             ds->f_value = cena + abs(ds->x - konc_x) + abs(ds->y - konc_y);
  81.             open_list = front_insert(open_list, ds);
  82.         }
  83.  
  84.         akt = graf->uzol[x][y];
  85.         while (akt != NULL) {
  86.             printf("x: %d y: %d\n", akt->x, akt->y);
  87.             akt = akt->parent;
  88.         }
  89.  
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement