Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. #include <queue>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8. using ss = short;
  9.  
  10. const int N = 1000;
  11.  
  12. int n, x[N], y[N];
  13. std::map<std::pair<int, int>, ss> vv;
  14. bool u[N][N];
  15. pair<ss, ss> p[N][N];
  16. int dx[9] = { -1, -1, -1,  0, 0, 0,  1, 1, 1 };
  17. int dy[9] = { -1,  0,  1, -1, 0, 1, -1, 0, 1 };
  18.  
  19. void aiok(std::queue<pair<ss, ss>>& q, ss i, ss v, int x, int y) {
  20.     if(vv.count({x, y})) {
  21.         ss z = vv[{x, y}];
  22.         if(!u[v][z]) p[v][z] = {i, v}, u[v][z] = 1, q.push({v, z});
  23.     }
  24. }
  25.  
  26. int main() {
  27.     std::cin >> n;
  28.     for(int i = 0; i < n; ++i) std::cin >> x[i] >> y[i];
  29.     for(int i = 0; i < n; ++i) vv[{x[i], y[i]}] = i;
  30.     std::queue<pair<ss, ss>> q; q.push({(ss)0, (ss)0}); u[0][0] = 1;
  31.     while(!q.empty()) {
  32.         auto v = q.front(); q.pop();
  33.         const int pv = v.first, cv = v.second;
  34.         const int mx = 2*x[cv]-x[pv], my = 2*y[cv]-y[pv];
  35.         //std::cout << pv << "; " << cv << endl;
  36.  
  37.         if(cv == n-1) {
  38.             vector<int> res;
  39.             for(int i=pv, j=cv; ; ) {
  40.                 int a = i, b = j;
  41.                 res.push_back(j);
  42.                 if(i == 0) break;
  43.                 i = p[a][b].first;
  44.                 j = p[a][b].second;
  45.             }
  46.  
  47.             reverse(res.begin(), res.end());
  48.             std::cout << res.size() << std::endl << "1 ";
  49.             for(int v : res) cout << v+1 << ' ';
  50.             return 0;
  51.         }
  52.  
  53.         for(int i = 0; i < 9; ++i)  {
  54.             const int ax=mx+dx[i], ay=my+dy[i];
  55.             aiok(q, pv, cv, ax, ay);
  56.         }
  57.     }
  58.    
  59.     std::cout << -1;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement