Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. //hill climbing
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. void MakeInitSol(int cnt, vector<int> &a)
  8. {
  9. int i, randnr;
  10.  
  11. srand(time(NULL));
  12. for(i = 1; i <= cnt; i++)
  13. {
  14. randnr = rand() % 6 + 1;
  15. a.push_back(randnr);
  16. }
  17. }
  18.  
  19. int GetValue(vector<int> a)
  20. {
  21. int i, val = 0, sum = 0;
  22. int count_arr[7] = {0};
  23. vector<int> ::iterator it;
  24.  
  25. for(it = a.begin(); it != a.end() - 1; it++)
  26. {
  27. sum += *it;
  28. if( (*(it + 1) - *it) > 3 ||
  29. (*(it) - *(it + 1)) > 3 )
  30. val += 5;
  31. count_arr[*it] ++;
  32. }
  33.  
  34. sum += *it;
  35. count_arr[*it] ++;
  36.  
  37. val += sum/a.size();
  38. return val;
  39. }
  40.  
  41. int main()
  42. {
  43. int i, index, n;
  44. vector<int> sol_arr, mod_arr;
  45. vector<int> ::iterator it;
  46.  
  47.  
  48. cout << "n=";
  49. cin >> n;
  50.  
  51. MakeInitSol(n, sol_arr);
  52.  
  53. //incerc de 10 ori sa caut o solutie mai buna
  54. //apoi ma opresc
  55. for(i = 1; i <= 10; i++)
  56. {
  57.  
  58. mod_arr = sol_arr;
  59.  
  60. srand(time(NULL)-i);
  61. index = rand() % n + 1;
  62. srand(time(NULL)-i-1);
  63. mod_arr[index] = rand() % 6 + 1;
  64.  
  65. if(GetValue(mod_arr) > GetValue(sol_arr))
  66. sol_arr = mod_arr;
  67.  
  68. }
  69.  
  70.  
  71. for(it = sol_arr.begin(); it != sol_arr.end(); it++)
  72. cout << *it << " ";
  73. cout << endl;
  74. cout << GetValue(sol_arr);
  75.  
  76.  
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement