Advertisement
madalinaradu

IA C Astar

May 30th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.79 KB | None | 0 0
  1.  
  2. int cautareAStar(int start, int stop, int A[20][20], char *nume[20]) {
  3.  
  4.  
  5.     int cost[20];
  6.     cost[start]=0;
  7.     int nrNoduri = 0, contorViz = 0;
  8.  
  9.     int noduri[40];//orasele in asteptare
  10.     int viz[20];//orase deja vizitate
  11.     int parinte[20];//parinte[i]=j =>j este parintele lui i
  12.     int gasit = 0;
  13.     for (int i = 0; i < 20; i++)
  14.         viz[i] = 0;
  15.     viz[start] = 1;
  16.     noduri[0] = start;
  17.     nrNoduri++;
  18.     int contorpas = 0;
  19.  
  20.  
  21.     cout << "Pasul " << contorpas++ << ": ";
  22.     for (int i = 0; i < nrNoduri; i++)
  23.         cout << nume[noduri[i]] << " ";
  24.     cout << endl;
  25.  
  26.  
  27.     while (gasit == 0 && nrNoduri > 0) {
  28.         int nod = noduri[0];
  29.         for (int i = 0; i < nrNoduri - 1; i++)
  30.             noduri[i] = noduri[i + 1];
  31.         nrNoduri--;
  32.         if (nod == stop)
  33.             gasit = 1;
  34.         else
  35.             for (int i = 0; i<20; i++)
  36.                 if ((A[nod][i] != 0) ) { //
  37.                     int costNou=cost[nod]+A[nod][i];
  38.                     if(viz[i]==0 ||(cost[i]>costNou)) {
  39.                         viz[i] = 1;
  40.                         parinte[i] = nod;
  41.                         cost[i]=cost[nod]+A[nod][i];
  42.                         int poz=0;
  43.                         while((poz<nrNoduri)&&((cost[i]+H[i])>(cost[noduri[poz]]+H[noduri[poz]])))
  44.                             poz++;
  45.                         cout<<poz<<endl;
  46.  
  47.                         for(int j=nrNoduri; j>poz; j--) {
  48.                             noduri[j]=noduri[j-1];
  49.                         }
  50.                         noduri[poz]=i;
  51.  
  52.                         nrNoduri++;
  53.                     }
  54.  
  55.  
  56.                 }
  57.         cout << "Pasul " << contorpas++ << ": ";
  58.         for (int i = 0; i < nrNoduri; i++)
  59.             cout << nume[noduri[i]] << " ";
  60.         cout << endl;
  61.  
  62.     }
  63.     cout << endl;
  64.     int temp = stop;
  65.     int contorTraseu = 0;
  66.     int traseu[20];
  67.     while (parinte[temp] != start) {
  68.         traseu[contorTraseu++] = temp;
  69.         temp = parinte[temp];
  70.     }
  71.     traseu[contorTraseu++] = temp;
  72.     traseu[contorTraseu++] = parinte[temp];
  73.     for (int i = contorTraseu - 1; i >= 0; i--) {
  74.         cout << nume[traseu[i]] << " "<<cost[traseu[i]]<<", ";
  75.     }
  76.  
  77.     return 0;
  78. }
  79. int H[20];
  80. int CreareMatrice(int A[20][20]) {
  81.  
  82.     A[0][1] = 75;
  83.     A[1][0] = 75;
  84.  
  85.     A[0][2] = 140;
  86.     A[2][0] = 140;
  87.  
  88.     A[0][3] = 118;
  89.     A[3][0] = 118;
  90.  
  91.     A[1][4] = 71;
  92.     A[4][1] = 71;
  93.  
  94.     A[4][2] = 151;
  95.     A[2][4] = 151;
  96.  
  97.     A[3][7] = 111;
  98.     A[7][3] = 111;
  99.  
  100.     A[7][10] = 70;
  101.     A[10][7] = 70;
  102.  
  103.     A[10][11] = 75;
  104.     A[11][10] = 75;
  105.  
  106.     A[11][12] = 120;
  107.     A[12][11] = 120;
  108.  
  109.     A[2][5] = 99;
  110.     A[5][2] = 99;
  111.  
  112.     A[2][6] = 80;
  113.     A[6][2] = 80;
  114.  
  115.     A[6][12] = 146;
  116.     A[12][6] = 146;
  117.  
  118.     A[6][9] = 97;
  119.     A[9][6] = 97;
  120.  
  121.     A[12][9] = 138;
  122.     A[9][12] = 138;
  123.  
  124.     A[5][8] = 211;
  125.     A[8][5] = 211;
  126.  
  127.     A[9][8] = 101;
  128.     A[8][9] = 101;
  129.  
  130.     A[8][13] = 90;
  131.     A[13][8] = 90;
  132.  
  133.     A[8][14] = 85;
  134.     A[14][8] = 85;
  135.  
  136.     A[14][15] = 98;
  137.     A[15][14] = 98;
  138.  
  139.     A[15][16] = 86;
  140.     A[16][15] = 86;
  141.  
  142.     A[14][17] = 142;
  143.     A[17][14] = 142;
  144.  
  145.     A[17][18] = 92;
  146.     A[18][17] = 92;
  147.  
  148.     A[18][19] = 87;
  149.     A[19][18] = 87;
  150.  
  151.  
  152.     return 0;
  153. }
  154.  
  155. //creare H - se refera la distanta pana la Bucuresti
  156. int CreareVector(int H[20]) {
  157.     H[0]=366; //Arad
  158.     H[1]=374;//Zerind
  159.     H[2]=253;//Sibiu
  160.     H[3]=329;//Timisoara
  161.     H[4]=380;//Oradea
  162.     H[5]=176;//Fagaras
  163.     H[6]=193;//RV
  164.     H[7]=244;//Lugoj
  165.     H[8]=0;//Bucuresti
  166.     H[9]=101;//Pitesti
  167.     H[10]=241;//Mehadia
  168.     H[11]=242;//Drobeta
  169.     H[12]=160;//Craiova
  170.     H[13]=77;//Giurgiu
  171.     H[14]=80;//Urziceni
  172.     H[15]=151;//Hirsova
  173.     H[16]=161;//Efoerie
  174.     H[17]=199;//Vaslui
  175.     H[18]=226;//Iasi
  176.     H[19]=234;//Neamt
  177.     return 0;
  178. }
  179.  
  180. int main() {
  181.     char *nume[20] = { "Arad", "Zerind", "Sibiu", "Timisoara", "Oradea", "Fagaras", "Ramnicu Valcea", "Lugoj", "Bucuresti", "Pitesti", "Mehadia", "Drobeta", "Craiova", "Giurgiu", "Urziceni", "Hirsova", "Eforie", "Vaslui", "Iasi", "Neamt" };
  182.     //                   0         1        2          3          4          5              6            7          8           9         10          11        12         13         14          15         16        17       18      19
  183.     for(int i=0; i<20; i++)}
  184.         for(j=0; j<20; j++){
  185.             A[i][j]=0;
  186.         }
  187.         }
  188.  
  189.  
  190.     CreareMatrice(A);
  191.     for(int i=0;i<20;i++){
  192.         for(int j=i+1;j<20;j++){
  193.             if(A[i][j]!=0){
  194.                 printf("%5d%5d%5d\n",i,j,A[i][j]);
  195.             }
  196.         }
  197.     }
  198.     cout<<"-----------"<<endl;
  199.     CreareVector(H);
  200.     int start = 0, stop = 8;
  201.     cout << endl << endl<< "Cautare A*:" << endl;
  202.     cautareAStar(start,stop,A,nume);
  203.     _getch();
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement