Advertisement
1WaKa_WaKa1

Task_M

May 22nd, 2022
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.59 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <utility>
  6.  
  7. struct Point{
  8.     short x;
  9.     short y;
  10.     short weight;
  11.  
  12.     bool operator==(const Point& item) const{
  13.         return item.x == x && item.y == y;
  14.     }
  15.  
  16.     bool operator!=(const Point& item) const{
  17.         return item.x != x || item.y != y;
  18.     }
  19. };
  20.  
  21. namespace std{
  22.     template <> struct hash<Point> {
  23.         std::size_t operator()(const Point& id) const noexcept {
  24.             return std::hash<int>()(id.x ^ (id.y << 16));
  25.         }
  26.     };
  27. }
  28. std::vector<std::vector<Point>> graph;
  29. short n, m;
  30. short answer = -1;
  31.  
  32.  
  33.  
  34. bool inBounds(short inX, short inY){
  35.     return (0 <= inX) && (inX < n) && (0 <= inY) && (inY < m);
  36. }
  37.  
  38. bool passable(Point point){
  39.     return point.weight != -1;
  40. }
  41.  
  42. std::vector<Point> neighbours(Point point){
  43.     std::vector<Point> result = {};
  44.     int tempX = point.x;
  45.     short tempY = point.y;
  46.     if (inBounds(tempX-1, tempY) && passable(point)){
  47.         result.push_back(graph[tempX-1][tempY]);
  48.     }
  49.     if (inBounds(tempX, tempY-1) && passable(point)){
  50.         result.push_back(graph[point.x][tempY-1]);
  51.     }
  52.     if (inBounds(tempX+1, tempY) && passable(point)){
  53.         result.push_back(graph[tempX+1][tempY]);
  54.     }
  55.     if (inBounds(tempX, tempY+1) && passable(point)){
  56.         result.push_back(graph[tempX][tempY+1]);
  57.     }
  58.     return result;
  59. }
  60.  
  61.  
  62.  
  63. short charToWeight(char input){
  64.     switch (input){
  65.         case '.':
  66.             return 1;
  67.         case 'W':
  68.             return 2;
  69.         case '#':
  70.             return -1;
  71.         default:
  72.             return 0;
  73.     }
  74. }
  75.  
  76. struct customComparator{
  77.     bool operator()(std::pair<short, Point> p1, std::pair<short, Point> p2){
  78.         return p1.first < p2.first;
  79.     }
  80. };
  81.  
  82. template<typename T, typename priority_t>
  83. struct PriorityQueue{
  84.     typedef std::pair<priority_t, T> elWithPrior;
  85.     std::priority_queue<elWithPrior, std::vector<elWithPrior>, customComparator> elements;
  86.  
  87.     inline bool empty(){
  88.         return elements.empty();
  89.     }
  90.  
  91.     inline void put(T item, priority_t prior){
  92.         elements.emplace(prior, item);
  93.     }
  94.     T get(){
  95.         T item = elements.top().second;
  96.         elements.pop();
  97.         return item;
  98.     }
  99. };
  100.  
  101. std::vector<Point> path(Point start, Point end, std::unordered_map<Point, Point> cameFrom){
  102.     std::vector<Point> path;
  103.     Point current = end;
  104.     path.push_back(end);
  105.     while (current != start){
  106.         current = cameFrom[current];
  107.         path.push_back(current);
  108.     }
  109.     std::reverse(path.begin(), path.end());
  110.     return path;
  111. }
  112.  
  113. void coorToOut(Point prev, Point cur){
  114.     if (prev.x - cur.x < 0){
  115.         std::cout << "S";
  116.     } else if (prev.x - cur.x > 0){
  117.         std::cout << "N";
  118.     }
  119.     if (prev.y - cur.y < 0){
  120.         std::cout << "E";
  121.     } else if (prev.y - cur.y > 0){
  122.         std::cout << "W";
  123.     }
  124. }
  125.  
  126. int main() {
  127.     //Ввод данных
  128.     short x1, y1, x2, y2;
  129.     std::cin >> n >> m >> x1 >> y1 >> x2 >> y2;
  130.  
  131.  
  132.     for (short i = 0; i < n; i++){
  133.         std::vector<Point> tempVec = {};
  134.         for (short j = 0; j < m; j++){
  135.             char temp;
  136.             std::cin >> temp;
  137.             tempVec.push_back({i,j, charToWeight(temp)});
  138.         }
  139.         graph.push_back(tempVec);
  140.     }
  141.     Point goal = graph[x2 - 1][y2 - 1];
  142.  
  143.     //Тело программы
  144.     PriorityQueue<Point, short> startPoint;
  145.     startPoint.put(graph[x1-1][y1-1], 0);
  146.     std::unordered_map<Point, Point> cameFrom;
  147.     std::unordered_map<Point, short> costSoFar;
  148.     cameFrom[graph[x1-1][y1-1]] = graph[x1-1][y1-1];
  149.     costSoFar[graph[x1-1][y1-1]] = 0;
  150.  
  151.     while (!startPoint.empty()){
  152.         auto cur = startPoint.get();
  153.  
  154.         if (cur == goal){
  155.             break;
  156.         }
  157.         std::vector<Point> temp = neighbours(cur);
  158.         for (auto next : temp){
  159.             short newCost = costSoFar[cur] + next.weight;
  160.             if (!costSoFar.count(next) || newCost < costSoFar[next]){
  161.                 costSoFar[next] = newCost;
  162.                 cameFrom[next] = cur;
  163.                 startPoint.put(next, newCost);
  164.             }
  165.         }
  166.     }
  167.     std::vector<Point> answerVec = path(graph[x1-1][y1-1], goal, cameFrom);
  168.  
  169.     answer = answerVec.size();
  170.     //Вывод программы
  171.     std::cout << answer << std::endl;
  172.     for (short i = 0; i < answerVec.size(); i++){
  173. //        if (i != 0){
  174. //            coorToOut(answerVec[i-1], answerVec[i]);
  175. //        }
  176.         std::cout << "( " << answerVec[i].x << " , " << answerVec[i].y << ")" << std::endl;
  177.     }
  178.  
  179.     return 0;
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement