Vermilliest

WayFinder

Feb 10th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.83 KB | None | 0 0
  1. #define ushort unsigned short
  2. #define uint unsigned int
  3.  
  4. #include <iostream>
  5. #include <queue>
  6. #include <stack>
  7.  
  8. const uint MAX_UINT = -1;
  9. const short DX[4] = {1, 0, -1, 0};
  10. const short DY[4] = {0, -1, 0, 1};
  11. const ushort DIRECTIONS_COUNT = 4;
  12. const ushort RIGHT = 0;
  13. const ushort UP = 1;
  14. const ushort LEFT = 2;
  15. const ushort DOWN = 3;
  16.  
  17. struct Point
  18. {
  19.     ushort X;
  20.     ushort Y;
  21.  
  22.   public:
  23.     Point(ushort x, ushort y)
  24.     {
  25.         X = x;
  26.         Y = y;
  27.     }
  28. };
  29.  
  30. inline bool operator==(const Point &point1, const Point &point2) { return point1.X == point2.X && point1.Y == point2.Y; }
  31. inline bool operator!=(const Point &point1, const Point &point2) { return !(point1 == point2); }
  32.  
  33. using namespace std;
  34.  
  35. int main()
  36. {
  37.     ushort M, N, Xs, Ys, Xf, Yf;
  38.     cin >> M >> N >> Xs >> Ys >> Xf >> Yf;
  39.     Xs--, Ys--, Xf--, Yf--;
  40.     Point start = Point(Xs, Ys);
  41.     Point finish = Point(Xf, Yf);
  42.     bool **maze = new bool *[N];
  43.     uint **steps = new uint *[N];
  44.     for (ushort X = 0; X < N; X++)
  45.     {
  46.         maze[X] = new bool[M];
  47.         steps[X] = new uint[M];
  48.     }
  49.     for (ushort Y = 0; Y < M; Y++)
  50.         for (ushort X = 0; X < N; X++)
  51.         {
  52.             ushort cell;
  53.             cin >> cell;
  54.             maze[X][Y] = cell == 1;
  55.             steps[X][Y] = MAX_UINT;
  56.         }
  57.     steps[start.X][start.Y] = 0;
  58.     queue<Point> q;
  59.     q.push(start);
  60.     while (!q.empty() && steps[finish.X][finish.Y] == MAX_UINT)
  61.     {
  62.         Point pos = q.back();
  63.         q.pop();
  64.         uint wayLength = steps[pos.X][pos.Y] + 1;
  65.         for (ushort direction = 0; direction < DIRECTIONS_COUNT; direction++)
  66.         {
  67.             if (
  68.                 (direction == LEFT && pos.X == 0) ||
  69.                 (direction == UP && pos.Y == 0) ||
  70.                 (direction == RIGHT && pos.X + 1 == N) ||
  71.                 (direction == DOWN && pos.Y + 1 == M))
  72.                 continue;
  73.             Point newPos = Point(pos.X + DX[direction], pos.Y + DY[direction]);
  74.             if (maze[newPos.X][newPos.Y] && steps[newPos.X][newPos.Y] > wayLength)
  75.             {
  76.                 steps[newPos.X][newPos.Y] = wayLength;
  77.                 q.push(newPos);
  78.             }
  79.         }
  80.     }
  81.     if (steps[finish.X][finish.Y] == MAX_UINT)
  82.         cout << -1 << endl;
  83.     else
  84.     {
  85.         ushort wayLength = steps[finish.X][finish.Y];
  86.         cout << wayLength << endl;
  87.         stack<Point> result;
  88.         result.push(finish);
  89.         Point pos = finish;
  90.         while (pos != start)
  91.         {
  92.             wayLength--;
  93.             for (ushort direction = 0; direction < DIRECTIONS_COUNT; direction++)
  94.             {
  95.                 if ((direction == LEFT && pos.X == 0) ||
  96.                     (direction == UP && pos.Y == 0) ||
  97.                     (direction == RIGHT && pos.X + 1 == N) ||
  98.                     (direction == DOWN && pos.Y + 1 == M))
  99.                     continue;
  100.                 Point newPos = Point(pos.X + DX[direction], pos.Y + DY[direction]);
  101.                 if (maze[newPos.X][newPos.Y] && steps[newPos.X][newPos.Y] == wayLength)
  102.                 {
  103.                     result.push(pos = newPos);
  104.                     break;
  105.                 }
  106.             }
  107.         }
  108.         while (!result.empty())
  109.         {
  110.             Point cell = result.top();
  111.             result.pop();
  112.             cout << cell.X + 1 << ' ' << cell.Y + 1 << endl;
  113.         }
  114.     }
  115.     delete[] maze;
  116.     delete[] steps;
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment