Advertisement
tuki2501

qbbishop.cpp

Nov 10th, 2021
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 205;
  5. const int INF = 0x3f3f3f3f;
  6.  
  7. struct pos {
  8.   int x, y, d, c;
  9.   pos() {}
  10.   pos(int x, int y, int d, int c): x(x), y(y), d(d), c(c) {}
  11.   bool operator()(pos a, pos b) {
  12.     return a.c > b.c;
  13.   }
  14. };
  15.  
  16. int dx[4] = {-1, -1, +1, +1};
  17. int dy[4] = {-1, +1, -1, +1};
  18.  
  19. int n, m, p, q, s, t;
  20. int vst[N][N][5], dis[N][N][5];
  21.  
  22. int main() {
  23.   cin.tie(0)->sync_with_stdio(0);
  24.   cin >> n >> m >> p >> q >> s >> t;
  25.   for (int i = 0; i < m; i++) {
  26.     int x, y;
  27.     cin >> x >> y;
  28.     for (int j = 0; j < 4; j++) {
  29.       vst[x][y][j] = 1;
  30.     }
  31.   }
  32.   memset(dis, 0x3f, sizeof(dis));
  33.   for (int i = 0; i < 4; i++) {
  34.     vst[p][q][i] = 1;
  35.     dis[p][q][i] = 0;
  36.   }
  37.   priority_queue<pos, vector<pos>, pos> que;
  38.   for (int i = 0; i < 4; i++) {
  39.     que.push(pos(p + dx[i], q + dy[i], i, 1));
  40.   }
  41.   while (que.size()) {
  42.     auto i = que.top(); que.pop();
  43.     int x = i.x, y = i.y, d = i.d, c = i.c;
  44.     if (x < 1 || x > n || y < 1 || y > n) continue;
  45.     if (vst[x][y][d]) continue;
  46.     vst[x][y][d] = 1;
  47.     dis[x][y][d] = c;
  48.     for (int j = 0; j < 4; j++) {
  49.       que.push(pos(x + dx[j], y + dy[j], j, c + (d != j)));
  50.     }
  51.   }
  52.   int ans = INF;
  53.   for (int i = 0; i < 4; i++) {
  54.     ans = min(ans, dis[s][t][i]);
  55.   }
  56.   if (ans == INF) ans = -1;
  57.   cout << ans << '\n';
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement