Beingamanforever

E-Bishop-2, 01 BFS

Feb 1st, 2025
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. // https://atcoder.jp/contests/abc246/tasks/abc246_e
  2. // 0-1 BFS
  3. #include <bits/stdc++.h>
  4. // #include <atcoder/all>
  5. using namespace std;
  6. #define int long long
  7. #define all(x) (x).begin(), (x).end()
  8. typedef vector<int> vi;
  9. typedef vector<vi> vvi;
  10. typedef vector<pair<int, int>> vpi;
  11. typedef pair<int, int> pi;
  12. typedef pair<pi, int> ppi;
  13. #define f first
  14. #define s second
  15. #define pb push_back
  16. #define endl "\n"
  17. #define yes cout << "YES" << endl
  18. #define no cout << "NO" << endl
  19. int dx[] = {-1, 1, -1, 1};
  20. int dy[] = {-1, 1, 1, -1};
  21. const int mod1 = 1e9 + 7, mod2 = 998244353, INF = 2e18, N = 2e5 + 5, L = 19;
  22. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  23. // -----------------------------------------------------------------------------
  24. void solve()
  25. {
  26.     int n, ax, ay, bx, by;
  27.     cin >> n >> ax >> ay >> bx >> by;
  28.     ax--, ay--, bx--, by--;
  29.     vector<string> grid(n);
  30.     for (int i = 0; i < n; i++)
  31.     {
  32.         cin >> grid[i];
  33.     }
  34.     auto inside = [&](int nx, int ny) -> bool
  35.     {
  36.         return (nx >= 0 && nx < n && ny >= 0 && ny < n && grid[nx][ny] != '#');
  37.     };
  38.     // x, y, direction -> unique state
  39.     vector<vvi> dis(n, vvi(n, vi(4, INF)));
  40.     vector<vvi> vis(n, vvi(n, vi(4, 0)));
  41.     deque<ppi> dq;
  42.     for (int i = 0; i < 4; i++)
  43.     {
  44.         int nx = ax + dx[i];
  45.         int ny = ay + dy[i];
  46.         if (inside(nx, ny))
  47.         {
  48.             if (dis[nx][ny][i] > 1)
  49.             {
  50.                 dis[nx][ny][i] = 1;
  51.                 dq.push_back({{nx, ny}, i});
  52.             }
  53.         }
  54.     }
  55.     while (!dq.empty())
  56.     {
  57.         auto cur = dq.front();
  58.         dq.pop_front();
  59.         int x = cur.f.f;
  60.         int y = cur.f.s;
  61.         int d = cur.s;
  62.         if (x == bx && y == by)
  63.         {
  64.             cout << dis[x][y][d] << endl;
  65.             return;
  66.         }
  67.         if (vis[x][y][d])
  68.         {
  69.             continue;
  70.         }
  71.         vis[x][y][d] = 1;
  72.         int curd = dis[x][y][d];
  73.         for (int i = 0; i < 4; i++)
  74.         {
  75.             int nx = x + dx[i];
  76.             int ny = y + dy[i];
  77.             if (!inside(nx, ny))
  78.             {
  79.                 continue;
  80.             }
  81.             int newd = curd + (i == d ? 0 : 1);
  82.             if (dis[nx][ny][i] > newd)
  83.             {
  84.                 dis[nx][ny][i] = newd;
  85.                 if (i == d)
  86.                 {
  87.                     dq.push_front({{nx, ny}, i});
  88.                 }
  89.                 else
  90.                 {
  91.                     dq.push_back({{nx, ny}, i});
  92.                 }
  93.             }
  94.         }
  95.     }
  96.     cout << -1 << endl;
  97. }
  98.  
  99. signed main()
  100. {
  101.     ios_base::sync_with_stdio(false);
  102.     cin.tie(nullptr);
  103.     cout.tie(nullptr);
  104.     int t = 1;
  105.     // cin >> t;
  106.     while (t--)
  107.     {
  108.         solve();
  109.     }
  110.     return 0;
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment