chillurbrain

11.2.2. Один конь

May 25th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. using namespace std;
  5. struct horse
  6. {
  7.     int x;
  8.     int y;
  9.     vector< pair< int, int > > v;
  10. };
  11. int px[9] = { 0, 2, 2, 1, 1, -1, -1, -2, -2 };
  12. int py[9] = { 0, 1, -1, 2, -2, 2, -2, 1, -1 };
  13. queue<horse> q;
  14. horse p1, p2;
  15. bool mark[30][30];
  16. vector< pair< int, int> > temp, ans;
  17. int main()
  18. {
  19.     int i, j, n, m, b, c, d, x1, y1, x2, y2;
  20.     cin >> n;
  21.     cin >> y1 >> x1 >> y2 >> x2;
  22.     mark[x1][y1] = true;
  23.     temp.push_back(make_pair(x1, y1));
  24.     p1.x = x1;
  25.     p1.y = y1;
  26.     p1.v = temp;
  27.     q.push(p1);
  28.     while (!q.empty())
  29.     {
  30.         p1 = q.front();
  31.         q.pop();
  32.         if (p1.x == x2 && p1.y == y2)
  33.         {
  34.             ans = p1.v;
  35.             break;
  36.         }
  37.         for (i = 1; i <= 8; ++i)
  38.         {
  39.             int x = p1.x + px[i];
  40.             int y = p1.y + py[i];
  41.             if (x>0 && y>0 && x <= n && y <= n && mark[x][y] == false)
  42.             {
  43.                 mark[x][y] = true;
  44.                 temp = p1.v;
  45.                 temp.push_back(make_pair(x, y));
  46.                 p2.x = x;
  47.                 p2.y = y;
  48.                 p2.v = temp;
  49.                 q.push(p2);
  50.             }
  51.         }
  52.     }
  53.     cout << ans.size() - 1 << endl;
  54.     for (i = 0; i<ans.size(); ++i)
  55.         cout << ans[i].second << " " << ans[i].first << endl;
  56.     return 0;
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment