madalinaradu

IA C iterativa

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