Advertisement
pb_jiang

ABC339D TLE

Feb 15th, 2024
959
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 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.     using vvl = vector<vl>;
  36.     using vvvl = vector<vvl>;
  37.     using vvvvl = vector<vvvl>;
  38.     vvvvl dist(n, vvvl(n, vvl(n, vl(n, LLONG_MAX / 2))));
  39.     vvvvl vis(n, vvvl(n, vvl(n, vl(n, 0))));
  40.  
  41.     // map<a4l, ll> dist;
  42.     mpq<a5l> q;
  43.     q.push({0, p1[0], p1[1], p2[0], p2[1]});
  44.     while (!q.empty()) {
  45.         // dbg(q.top());
  46.         auto [d, p1x, p1y, p2x, p2y] = q.top();
  47.         if (p1x == 2 && p1y == 0)
  48.             dbg(q.top());
  49.         q.pop();
  50.         auto p1 = a4l{p1x, p1y, p2x, p2y};
  51.         if (vis[p1x][p1y][p2x][p2y])
  52.             continue;
  53.         vis[p1x][p1y][p2x][p2y] = 1;
  54.         dist[p1x][p1y][p2x][p2y] = d;
  55.         if (p1x == p2x && p1y == p2y)
  56.             ans = min(ans, d);
  57.  
  58.         for (ll i = 0; i < 4; ++i) {
  59.             ll dx = ds[i][0], dy = ds[i][1];
  60.             ll n1x = min(max(p1x + dx, 0ll), n - 1), n2x = min(max(p2x + dx, 0ll), n - 1);
  61.             ll n1y = min(max(p1y + dy, 0ll), n - 1), n2y = min(max(p2y + dy, 0ll), n - 1);
  62.             if (g[n1x][n1y] == '#')
  63.                 n1x = p1x, n1y = p1y;
  64.             if (g[n2x][n2y] == '#')
  65.                 n2x = p2x, n2y = p2y;
  66.             auto p2 = a4l{n1x, n1y, n2x, n2y};
  67.             if (p1 == p2)
  68.                 continue;
  69.  
  70.             if (dist[n1x][n1y][n2x][n2y] > d + 1 && vis[n1x][n1y][n2x][n2y] == 0) {
  71.                 dist[n1x][n1y][n2x][n2y] = d + 1;
  72.                 q.push({d + 1, n1x, n1y, n2x, n2y});
  73.             }
  74.         }
  75.     }
  76.     /*
  77.     using vb = vector<bool>;
  78.     using vvb = vector<vb>;
  79.     using vvvb = vector<vvb>;
  80.     using vvvvb = vector<vvvb>;
  81.     vvvvb inqueue(n, vvvb(n, vvb(n, vb(n))));
  82.     queue<a5l> q;
  83.     q.push({0, p1[0], p1[1], p2[0], p2[1]});
  84.     while (!q.empty()) {
  85.         auto [d, p1x, p1y, p2x, p2y] = q.front();
  86.         q.pop();
  87.         auto p1 = a4l{p1x, p1y, p2x, p2y};
  88.         if (p1x == p2x && p1y == p2y)
  89.             ans = min(ans, d);
  90.         for (ll i = 0; i < 4; ++i) {
  91.             ll dx = ds[i][0], dy = ds[i][1];
  92.             ll n1x = min(max(p1x + dx, 0ll), n - 1), n2x = min(max(p2x + dx, 0ll), n - 1);
  93.             ll n1y = min(max(p1y + dy, 0ll), n - 1), n2y = min(max(p2y + dy, 0ll), n - 1);
  94.             if (g[n1x][n1y] == '#')
  95.                 n1x = p1x, n1y = p1y;
  96.             if (g[n2x][n2y] == '#')
  97.                 n2x = p2x, n2y = p2y;
  98.             auto p2 = a4l{n1x, n1y, n2x, n2y};
  99.             if (p1 == p2)
  100.                 continue;
  101.             if (inqueue[n1x][n1y][n2x][n2y] == false) {
  102.                 inqueue[n1x][n1y][n2x][n2y] = true;
  103.                 q.push({d + 1, n1x, n1y, n2x, n2y});
  104.             }
  105.         }
  106.     }
  107.     */
  108.     cout << (ans == LLONG_MAX ? -1 : ans) << endl;
  109.     return 0;
  110. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement