Advertisement
deushiro

Untitled

Dec 15th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <algorithm>
  5. #include <queue>
  6. #include <map>
  7. #include <set>
  8. #include <bitset>
  9.        
  10. using namespace std;
  11.  
  12. typedef long long ll;
  13.  
  14. struct bf{
  15.     int v, u, c;
  16. };
  17.  
  18. int main() {
  19.     int a, b, v, u, c;
  20.     cin >> a >> b;
  21.     map<pair<int, int>, int> m;
  22.     queue<bf> q;
  23.     q.push({0, 0, 0});
  24.     vector<pair<int, int>> move = {{-1, 2}, {-1, -2}, {1, 2}, {1, -2},{2, -1}, {2, 1}, {-2, -1}, {-2, 1}};
  25.     while(v != a || u != b){
  26.         v = q.front().v;
  27.         u = q.front().u;
  28.         c = q.front().c;
  29.         q.pop();
  30.         for(int i = 0; i < 8; ++i){
  31.             if(!m.count({v + move[i].first, u + move[i].second})){
  32.                 m[{v + move[i].first, u + move[i].second}] = c;
  33.                 q.push({v + move[i].first, u + move[i].second, c + 1});
  34.             }
  35.         }
  36.     }
  37.     while(a != 0 || b != 0){
  38.         int c = 1e9;
  39.         int x = 0;
  40.         int y = 0;
  41.         for(int i = 0; i < 8; ++i){
  42.             if(m[{a + move[i].first, b + move[i].second}] < c && m[{a + move[i].first, b + move[i].second}] > 0){
  43.                 x = a + move[i].first;
  44.                 y = b + move[i].second;
  45.             }
  46.         }
  47.         a = x;
  48.         b = y;
  49.         cout << a << " " << b << endl;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement