Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include "Brute_Force_method.h"
  3.  
  4. //int counter = 0;
  5.  
  6. void Brute_Force(int n, matrix Matrix) {
  7.     vector <int> min_way, cur_way;
  8.     int min_length, cur_length;
  9.  
  10.     for (int i = 0; i < n; i++) {     //Çàïîëíåíèå âåêòîðà ïóòè             f=1+3n+
  11.         cur_way.push_back(i + 1);
  12.         //counter += 3;
  13.     }                                        // íåì õðàíÿòñÿ ïîñåùåííûå òî÷êè     +n*3=1+6n
  14.     cur_way.push_back(cur_way[0]);       //Çàìûêàåì ïóòü,äåëàÿ öèêë           f=3
  15.     //counter++;
  16.  
  17.     min_way = cur_way; //f=1
  18.     //counter++;
  19.     min_length = Count_Length(Matrix, cur_way, n);  //f=2+12n+1
  20.     Print(Matrix, cur_way, n + 1);
  21.     //F1=6+12n
  22.  
  23.     while (NextSet(cur_way, n)) {    //×èñëî ïîâòîðåíèé=n!-1
  24.         cur_way[n] = cur_way[0]; //f=3
  25.         //counter += 3;
  26.         Print(Matrix, cur_way, n + 1);
  27.         cur_length = Count_Length(Matrix, cur_way, n); //f=1+2+12n
  28.         if (cur_length < min_length) {
  29.             min_length = cur_length;
  30.             min_way = cur_way;
  31.             //counter += 3;
  32.         }
  33.     }
  34.     cout << "Min_way is ";
  35.     for (int i = 0; i <= n; i++) {
  36.         cout << min_way[i] << " ";
  37.     }
  38.     cout << endl;
  39.     Print(Matrix, min_way, n + 1);
  40.     cout << "Length is " << min_length << endl;
  41.     //cout << "Counter's time is " << counter / 455546.0 << endl;
  42. }
  43.  
  44. int Count_Length(matrix Matrix, vector<int> way, int n) {
  45.     int length = 0;
  46.     //counter++;
  47.     for (int i = 0; i < n; i++) {
  48.         length += Matrix[way[i] - 1][way[i + 1] - 1];
  49.         //counter += 9;
  50.     }
  51.     return length;
  52. }
  53.  
  54. void Vector_Swap(vector<int> &a, int i, int j) {
  55.     int k = a[i];
  56.     a[i] = a[j];
  57.     a[j] = k;
  58.     //counter += 7;
  59. }
  60.  
  61. bool NextSet(vector <int> &a, int n) {
  62.     int j = n - 2;
  63.     //counter += 2;
  64.     while ((j != -1) && (a[j] >= a[j + 1])) {
  65.         j--;
  66.         //counter += 7;
  67.     }
  68.     if (j == -1) {
  69.         //counter++;
  70.         return false;
  71.     }
  72.     int k = n - 1;
  73.     //counter += 2;
  74.     while (a[j] >= a[k]) {
  75.         k--;
  76.         //counter += 5;
  77.     }
  78.     Vector_Swap(a, j, k);
  79.     int l = j + 1, r = n - 1;
  80.     //counter += 4;
  81.     while (l < r) {
  82.         //counter++;
  83.         Vector_Swap(a, l++, r--);
  84.     }
  85.     return true;
  86. }
  87.  
  88. void Print(matrix Matrix, vector <int> a, int n) {
  89.     /*static int num = 1;
  90.     cout << "#" << num << ": ";
  91.     for (int i = 0; i < n; i++) {
  92.         cout << a[i] << "  ";
  93.     }
  94.     cout << "Length=" << Count_Length(Matrix, a, n - 1) << endl;
  95.     num++;*/
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement