trungore10

sknight_cooVersion

Aug 29th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.45 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define mp make_pair
  3. #define x first
  4. #define y second
  5. using namespace std;
  6. void cass(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  7.     freopen("sknight.inp", "r", stdin);
  8.     freopen("sknight.out", "w", stdout);
  9. }
  10.  
  11. const int B = 1e6 + 1;
  12. const int HB = 1e3 + 1;
  13. const int N = 1001;
  14.  
  15. long long tx[10] = {0, -1, 1, 1, -1, -1, -1, 1, 1};
  16. //int tx[10] = {0, -2, -2, 2, 2, -1, -1, 1, 1};
  17. //int ty[10] = {0, -1, 1, -1, 1, -2, 2, -2, 2};
  18. long long ty[10] = {0, -1, 1, -1, 1, -1, 1, -1, 1};
  19.  
  20. long long k, l;
  21. int xt, yt;
  22. int n;
  23. pair<int, int > ban[N];
  24.  
  25. void init(){
  26.     k--; l--;
  27.     for (int i = 1; i <= 4; i++){
  28.         tx[i] *= k;
  29.         ty[i] *= l;
  30.     }
  31.     for (int i = 5; i <= 8; i++){
  32.         tx[i] *= l;
  33.         ty[i] *= k;
  34.     }
  35.  
  36. }
  37.  
  38. int deg[2 * HB][2 * HB];
  39. int tab[2 * HB][2 * HB];
  40.  
  41. void sub2(){
  42.     for (int i = 1; i <= n; i++) tab[ban[i].x + HB][ban[i].y + HB] = 1;
  43.     queue<pair<int, int > > Q;
  44.     Q.push(mp(xt + HB, yt + HB));
  45.     memset(deg, -1, sizeof(deg));
  46.     deg[xt + HB][yt + HB] = 0;
  47.     while (!Q.empty()){
  48.         pair<int, int > pre = Q.front(); Q.pop();
  49.         for (int t = 1; t <= 8; t++){
  50.             int nx = pre.x + tx[t];
  51.             int ny = pre.y + ty[t];
  52.             if (1 <= nx && nx <= 2001 && 1 <= ny && ny <= 2001 && deg[nx][ny] == -1 && tab[nx][ny] == 0){
  53.                 deg[nx][ny] = deg[pre.x][pre.y] + 1;
  54.                 Q.push(mp(nx, ny));
  55.                 if (nx == HB && ny == HB){
  56.                     cout << deg[nx][ny];
  57.                     exit(0);
  58.                 }
  59.             }
  60.         }
  61.     }
  62.     cout << -1;
  63. }
  64.  
  65. int naive(int fixedx, int fixedy){
  66.  
  67.     queue<pair<int, int > > Q;
  68.     Q.push(mp(fixedx , fixedy ));
  69.     memset(deg, -1, sizeof(deg));
  70.     deg[fixedx ][fixedy ] = 0;
  71.     while (!Q.empty()){
  72.         pair<int, int > pre = Q.front(); Q.pop();
  73.         for (int t = 1; t <= 8; t++){
  74.             int nx = pre.x + tx[t];
  75.             int ny = pre.y + ty[t];
  76.             if (0 <= nx && nx <= 2 * HB && 0 <= ny && ny <= 2 * HB && deg[nx][ny] == -1){
  77.                 deg[nx ][ny] = deg[pre.x][pre.y ] + 1;
  78.                 Q.push(mp(nx, ny));
  79.                 if (nx == HB && ny == HB){
  80.                     return deg[nx][ny];
  81.                 }
  82.             }
  83.         }
  84.     }
  85.     return -1;
  86. }
  87.  
  88. int numPre = 0;
  89.  
  90. pair<int, int > prepare(){
  91.     long long inf = 1e15 + 6;
  92.     // go from xt, yt (haved fixed) to the 1..2001 x 1.. 2001 field
  93.     long long curX = abs(xt), curY = abs(yt);
  94.     while (1){
  95.         if ( - HB <= curX && curX <= HB && - HB <= curY && curY <= HB) return mp(curX + HB, curY + HB);
  96.         long long minDis = inf;
  97.         long long minIdX {}, minIdY{};
  98.         for (int t = 1; t <= 8; t++){
  99.             long long nx = curX + tx[t];
  100.             long long ny = curY + ty[t];
  101.             if ((nx) * (nx) + (ny) * (ny) < minDis){
  102.                 minDis = (nx) * (nx) + (ny) * (ny);
  103.                 minIdX = nx;
  104.                 minIdY = ny;
  105.             }
  106.         }
  107.         curX = minIdX;
  108.         curY = minIdY;
  109.         numPre++;
  110.     }
  111. }
  112.  
  113. void full(){
  114.     pair<int, int > stpoint = prepare();
  115.     int tmp = naive(stpoint.x, stpoint.y);
  116.  
  117.     //if (tmp != -1)
  118.         cout << numPre + tmp;
  119. }
  120.  
  121. int main(){
  122.     cass();
  123.     cin >> k >> l;
  124.     cin >> xt >> yt;
  125.     cin >> n;
  126.     for (int i = 1; i <= n; i++) cin >> ban[i].first >> ban[i].second;
  127.  
  128.     init();
  129.  
  130.     if (n){
  131.         sub2();
  132.     }
  133.     else full();
  134. }
Add Comment
Please, Sign In to add comment