Advertisement
Sanlover

Untitled

Mar 30th, 2022
1,176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. #include <iomanip>
  2. #include <iostream>
  3. #include <map>
  4. #include <vector>
  5. #include <Windows.h>
  6. using namespace std;
  7.  
  8. using double_vector_t = vector<vector<size_t>>;
  9. using coordinate_t = pair<int, int>;
  10.  
  11.  
  12. // заполняем мапу (дорога - посещали\нет)
  13. void initRoads(map<coordinate_t, bool>& roads, const double_vector_t& matrix)
  14. {
  15.     for (size_t i = 0; i < matrix.size(); i++)
  16.     {
  17.         for (size_t j = 0; j < matrix[i].size(); j++)
  18.         {
  19.             if (matrix[i][j] != 0)
  20.             {
  21.                 roads[coordinate_t(i, j)] = false;
  22.             }
  23.         }
  24.     }
  25. }
  26.  
  27. void initBlocked(map<size_t, map<size_t, size_t>>& blocked, const size_t& lineAmount)
  28. {
  29.     for (size_t i = 0; i < lineAmount; i++)
  30.     {
  31.         for (size_t j = 0; j < lineAmount; j++)
  32.         {
  33.             blocked[i][j] = 0;
  34.         }
  35.     }
  36. }
  37.  
  38. bool isBlocked(map<size_t, map<size_t, size_t>>& blocked, const size_t& line, const size_t& id)
  39. {
  40.     return blocked[line][id] == 1;
  41. }
  42.  
  43. void blockId(map<size_t, map<size_t, size_t>>& blocked, const size_t& line, const size_t& id)
  44. {
  45.     blocked[line][id] = 1;
  46. }
  47.  
  48. void unblockId(map<size_t, map<size_t, size_t>>& blocked, const size_t& line, const size_t& id)
  49. {
  50.     blocked[line][id] = 0;
  51. }
  52.  
  53. void markVisited(map<coordinate_t, bool>& roads, const coordinate_t& road)
  54. {
  55.     if (roads.find(road) != roads.end())
  56.     {
  57.         roads[road] = true;
  58.     }
  59.     if (roads.find(coordinate_t(road.second, road.first)) != roads.end())
  60.     {
  61.         roads[coordinate_t(road.second, road.first)] = true;
  62.     }
  63. }
  64.  
  65. void markUnvisited(map<coordinate_t, bool>& roads, const coordinate_t& road)
  66. {
  67.     if (roads.find(road) != roads.end())
  68.     {
  69.         roads[road] = false;
  70.     }
  71.     if (roads.find(coordinate_t(road.second, road.first)) != roads.end())
  72.     {
  73.         roads[coordinate_t(road.second, road.first)] = false;
  74.     }
  75. }
  76.  
  77. // Проверка на посещение всех
  78. bool isRoadsVisited(const map<coordinate_t, bool>& roads)
  79. {
  80.     for (const auto& it : roads)
  81.     {
  82.         if (it.second != true)
  83.         {
  84.             return false;
  85.         }
  86.     }
  87.     return true;
  88. }
  89.  
  90. // Считает коллизиции для точки
  91. size_t getCollisions(const double_vector_t& matrix, const size_t& line)
  92. {
  93.     size_t counter = 0;
  94.     for (size_t j = 0; j < matrix[line].size(); j++)
  95.     {
  96.         if (matrix[line][j] != 0)
  97.         {
  98.             counter++;
  99.         }
  100.     }
  101.     return counter;
  102. }
  103.  
  104.  
  105. void func(const double_vector_t& matrix,
  106.           map<coordinate_t, bool>& roads,
  107.           map<size_t, map<size_t, size_t>>& blocked,
  108.           const size_t& line,
  109.           size_t& currentLength,
  110.           size_t*& bestLength,
  111.           vector<size_t>& currentWay,
  112.           vector<size_t>& bestWay)
  113. {
  114.     currentWay.push_back(line);
  115.     if (isRoadsVisited(roads))
  116.     {
  117.         if (bestLength != nullptr)
  118.         {
  119.             if (*bestLength > currentLength)
  120.             {
  121.                 bestWay = currentWay;
  122.                 bestLength = new size_t(currentLength);
  123.             }
  124.         }
  125.         else
  126.         {
  127.             bestWay = currentWay;
  128.             bestLength = new size_t(currentLength);
  129.         }
  130.     }
  131.     else
  132.     {
  133.         for (size_t j = 0; j < matrix[line].size(); j++)
  134.         {
  135.             if (matrix[line][j] != 0 && !isBlocked(blocked, line, j))
  136.             {
  137.                 markVisited(roads, coordinate_t(line, j));
  138.                 blockId(blocked, line, j);
  139.                 size_t moveCost = currentLength + matrix[line][j] + getCollisions(matrix, line);
  140.                 func(matrix, roads, blocked, j, moveCost, bestLength, currentWay, bestWay);
  141.  
  142.                 markUnvisited(roads, coordinate_t(line, j));
  143.                 unblockId(blocked, line, j);
  144.             }
  145.         }
  146.     }
  147.     if (!currentWay.empty())
  148.     {
  149.         currentWay.pop_back();
  150.     }
  151. }
  152.  
  153. void printMatrix(const double_vector_t& matrix)
  154. {
  155.     cout << endl << "Matrix:" << endl;
  156.     for (const auto& it : matrix)
  157.     {
  158.         for (const auto& jt : it)
  159.         {
  160.             cout << setw(3) << jt;
  161.         }
  162.         cout << endl;
  163.     }
  164. }
  165.  
  166. int main()
  167. {
  168.     map<coordinate_t, bool> roads;
  169.     map<size_t, map<size_t, size_t>> blocked;
  170.     // та не рабочая
  171.     double_vector_t matrix__
  172.     {
  173.         {0, 10, 4, 8, 0, 0, 0, 0, 0, 0, 0},
  174.         {0, 0, 8, 0, 6, 0, 0, 0, 0, 0, 0},
  175.         {0, 0, 0, 4, 7, 0, 0, 0, 0, 0, 0},
  176.         {0, 0, 0, 0, 0, 10, 7, 7, 0, 0, 13},
  177.         {0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0},
  178.         {0, 0, 0, 8, 0, 0, 0, 0, 4, 0, 5},
  179.         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  180.         {0, 0, 0, 0, 0, 7, 0, 0, 0, 6, 0},
  181.         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5},
  182.         {0, 0, 0, 0, 0, 11, 0, 0, 12, 0, 0},
  183.         {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  184.     };
  185.  
  186.     double_vector_t matrix{
  187.         {0, 10, 4, 8, 0},
  188.         {10, 0, 8, 0, 6},
  189.         {4, 8, 0, 4, 7},
  190.         {8, 0, 4, 0, 8},
  191.         {0, 6, 7, 8, 0}
  192.  
  193.     };
  194.     size_t currentLength = 0;
  195.     size_t* bestLength = nullptr;
  196.     //para - (line, blocked j's)
  197.     vector<size_t> currentWay;
  198.     vector<size_t> bestWay;
  199.     printMatrix(matrix);
  200.  
  201.     initRoads(roads, matrix);
  202.     initBlocked(blocked, matrix.size());
  203.  
  204.     for (size_t i = 0; i < matrix.size(); i++)
  205.     {
  206.         func(matrix, roads, blocked, 0, currentLength, bestLength, currentWay, bestWay);
  207.     }
  208.     cout << "Best length: " << (bestLength != nullptr ? *bestLength : 0) << endl;
  209.     for (size_t i = 0; i < bestWay.size(); i++)
  210.     {
  211.         cout << bestWay[i] + 1 << (i + 1 != bestWay.size() ? " -> " : ".");
  212.     }
  213.     return 0;
  214. }
  215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement