Advertisement
AmarBiH

Hill-climbing algoritam

May 21st, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include<iostream>
  2. #include <map>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. typedef vector<vector<int>> Matrica;
  7.  
  8. Matrica KreirajMatricu(int red, int kolona)
  9. {
  10.     return Matrica(red, vector<int>(kolona));
  11. }
  12. int broj_redova(Matrica m)
  13. {
  14.     return m.size();
  15. }
  16. int broj_kolona(Matrica m, int red)
  17. {
  18.     return m[red].size();
  19. }
  20.  
  21. pair<int, int> Koordinati(Matrica m, pair<int, int> P)
  22. {
  23.     pair<int, int> koordinati;
  24.     int max = m[P.first][P.second];
  25.     pair<int, int> koordinati_max = P;
  26.     bool breakaj = false;
  27.     int brojac = 0;
  28.  
  29. while(true)
  30. {
  31.    
  32.     for(int i(-1); i<=1; ++i){
  33.         for(int j(-1); j<=1; ++j){
  34.            
  35.             try
  36.             {
  37.                 if(m.at(koordinati_max.first + i).at(koordinati_max.second + j) > m[koordinati_max.first][koordinati_max.second])
  38.                 {
  39.                     koordinati_max.first += i;
  40.                     koordinati_max.second += j;
  41.                     brojac = 1;
  42.                     breakaj = true;
  43.                     break;
  44.                 }  
  45.             }
  46.             catch(...)
  47.             {
  48.                 breakaj = true;
  49.                 break;
  50.             }  
  51.            
  52.         }
  53.         if(breakaj){
  54.             breakaj = false;
  55.             break;
  56.         }
  57.     }
  58.    
  59.     if(brojac == 0)
  60.     {
  61.         break;
  62.     }
  63.     else
  64.     {
  65.         brojac = 0;
  66.         continue;  
  67.     }
  68. }
  69.    
  70.    
  71.    
  72.     return koordinati_max;
  73. }
  74.  
  75. int main()
  76. {
  77.     Matrica m = KreirajMatricu(5, 5);
  78.    
  79.     //unos
  80.     for(int i = 0; i<5; ++i)
  81.     {
  82.         for(int j = 0; j<5; ++j)
  83.         {
  84.             cout << "[" << i << "][" << j << "]: ";
  85.             cin >> m[i][j];
  86.         }
  87.     }
  88.    
  89.     cout << "Unesite koordinate pocetne tacke P: ";
  90.     pair<int, int> par;
  91.     cin >> par.first >> par.second;
  92.    
  93.     pair<int, int> rezultat = Koordinati(m, par);
  94.     cout << "Pozicija lokalnog maksimuma:\n(" << rezultat.first << ", " << rezultat.second << ")\n";
  95.    
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement