Advertisement
pb_jiang

ABC339D

Feb 15th, 2024 (edited)
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <assert.h>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #ifndef __DEBUG__
  5. #define dbg(...) 42
  6. #endif
  7. template <class T> using mpq = priority_queue<T, vector<T>, greater<T>>;
  8.  
  9. using ll = long long;
  10. using pii = pair<int, int>;
  11. using pll = pair<ll, ll>;
  12. using vl = vector<ll>;
  13. using vi = vector<int>;
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.     ll n;
  18.     cin >> n;
  19.     vector<string> g(n);
  20.     for (auto &s : g)
  21.         cin >> s;
  22.     using a2l = array<ll, 2>;
  23.     a2l p1{-1, -1}, p2{-1, -1};
  24.     for (ll i = 0; i < n; ++i)
  25.         for (ll j = 0; j < n; ++j)
  26.             if (g[i][j] == 'P')
  27.                 if (p1 == a2l{-1, -1})
  28.                     p1 = {i, j};
  29.                 else
  30.                     p2 = {i, j};
  31.     using a5l = array<ll, 5>; // dist, x, y, p2x, p2y
  32.     using a4l = array<ll, 4>;
  33.     ll ans = LLONG_MAX;
  34.     ll ds[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
  35.     /*
  36.     map<a4l, ll> dist;
  37.     mpq<a5l> q;
  38.     q.push({0, p1[0], p1[1], p2[0], p2[1]});
  39.     while (!q.empty()) {
  40.         // dbg(q.top());
  41.         auto [d, p1x, p1y, p2x, p2y] = q.top();
  42.         if (p1x == 2 && p1y == 0)
  43.             dbg(q.top());
  44.         q.pop();
  45.         auto p1 = a4l{p1x, p1y, p2x, p2y};
  46.         if (dist.count(p1) != 0 && dist[p1] < d)
  47.             continue;
  48.         dist[p1] = d;
  49.         if (p1x == p2x && p1y == p2y)
  50.             ans = min(ans, d);
  51.  
  52.         for (ll i = 0; i < 4; ++i) {
  53.             ll dx = ds[i][0], dy = ds[i][1];
  54.             ll n1x = min(max(p1x + dx, 0ll), n - 1), n2x = min(max(p2x + dx, 0ll), n - 1);
  55.             ll n1y = min(max(p1y + dy, 0ll), n - 1), n2y = min(max(p2y + dy, 0ll), n - 1);
  56.             if (g[n1x][n1y] == '#')
  57.                 n1x = p1x, n1y = p1y;
  58.             if (g[n2x][n2y] == '#')
  59.                 n2x = p2x, n2y = p2y;
  60.             auto p2 = a4l{n1x, n1y, n2x, n2y};
  61.             if (p1 == p2)
  62.                 continue;
  63.  
  64.             if (dist.count(p2) == 0 || dist[p2] > d + 1) {
  65.                 dist[p2] = d + 1;
  66.                 q.push({d + 1, n1x, n1y, n2x, n2y});
  67.             }
  68.         }
  69.     }
  70.     */
  71.     using vb = vector<bool>;
  72.     using vvb = vector<vb>;
  73.     using vvvb = vector<vvb>;
  74.     using vvvvb = vector<vvvb>;
  75.     vvvvb inqueue(n, vvvb(n, vvb(n, vb(n))));
  76.     queue<a5l> q;
  77.     q.push({0, p1[0], p1[1], p2[0], p2[1]});
  78.     while (!q.empty()) {
  79.         auto [d, p1x, p1y, p2x, p2y] = q.front();
  80.         q.pop();
  81.         auto p1 = a4l{p1x, p1y, p2x, p2y};
  82.         if (p1x == p2x && p1y == p2y)
  83.             ans = min(ans, d);
  84.         for (ll i = 0; i < 4; ++i) {
  85.             ll dx = ds[i][0], dy = ds[i][1];
  86.             ll n1x = min(max(p1x + dx, 0ll), n - 1), n2x = min(max(p2x + dx, 0ll), n - 1);
  87.             ll n1y = min(max(p1y + dy, 0ll), n - 1), n2y = min(max(p2y + dy, 0ll), n - 1);
  88.             if (g[n1x][n1y] == '#')
  89.                 n1x = p1x, n1y = p1y;
  90.             if (g[n2x][n2y] == '#')
  91.                 n2x = p2x, n2y = p2y;
  92.             auto p2 = a4l{n1x, n1y, n2x, n2y};
  93.             if (p1 == p2)
  94.                 continue;
  95.             if (inqueue[n1x][n1y][n2x][n2y] == false) {
  96.                 inqueue[n1x][n1y][n2x][n2y] = true;
  97.                 q.push({d + 1, n1x, n1y, n2x, n2y});
  98.             }
  99.         }
  100.     }
  101.     cout << (ans == LLONG_MAX ? -1 : ans) << endl;
  102.     return 0;
  103. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement