Advertisement
Sanlover

Untitled

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