Beingamanforever

D-Wizzard in Maze, 0-1 bfs

Jan 31st, 2025
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // #include <atcoder/all>
  3. using namespace std;
  4. #define int long long
  5. #define all(x) (x).begin(), (x).end()
  6. typedef vector<int> vi;
  7. typedef vector<vi> vvi;
  8. typedef vector<pair<int, int>> vpi;
  9. typedef pair<int, int> pi;
  10. #define f first
  11. #define s second
  12. #define pb push_back
  13. #define endl "\n"
  14. #define yes cout << "YES" << endl
  15. #define no cout << "NO" << endl
  16. int dx[] = {-1, 0, 1, 0};
  17. int dy[] = {0, 1, 0, -1};
  18. const int mod1 = 1e9 + 7, mod2 = 998244353, INF = 2e18, N = 1005, L = 19;
  19. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  20. // -----------------------------------------------------------------------------
  21. int cost[N][N];
  22. void solve()
  23. {
  24.     int n, m;
  25.     cin >> n >> m;
  26.     int sx, sy, ex, ey;
  27.     cin >> sx >> sy >> ex >> ey;
  28.     sx--, sy--, ex--, ey--;
  29.     string grid[N];
  30.     for (int i = 0; i < n; i++)
  31.     {
  32.         cin >> grid[i];
  33.     }
  34.     for (int i = 0; i < n; i++)
  35.     {
  36.         fill(cost[i], cost[i] + m, INF);
  37.     }
  38.     function<bool(int, int)> inside = [&](int x, int y)
  39.     {
  40.         return (x >= 0 && x < n && y >= 0 && y < m && grid[x][y] == '.');
  41.     };
  42.     deque<pi> dq;
  43.     dq.push_back({sx, sy});
  44.     cost[sx][sy] = 0;
  45.     while (!dq.empty())
  46.     {
  47.         auto [x, y] = dq.front();
  48.         dq.pop_front();
  49.         // explore all directions first via A
  50.         for (int i = 0; i < 4; i++)
  51.         {
  52.             int nx = x + dx[i];
  53.             int ny = y + dy[i];
  54.             if (inside(nx, ny) && cost[nx][ny] > cost[x][y])
  55.             {
  56.                 cost[nx][ny] = cost[x][y];
  57.                 dq.push_front({nx, ny});
  58.             }
  59.         }
  60.         // now via magic of B
  61.         for (int dx = -2; dx <= 2; dx++)
  62.         {
  63.             for (int dy = -2; dy <= 2; dy++)
  64.             {
  65.                 int nx = x + dx;
  66.                 int ny = y + dy;
  67.                 if (inside(nx, ny) && cost[nx][ny] > cost[x][y] + 1)
  68.                 {
  69.                     cost[nx][ny] = cost[x][y] + 1;
  70.                     dq.push_back({nx, ny});
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     int ans = cost[ex][ey];
  76.     cout << ((ans == (INF)) ? -1 : ans) << endl;
  77.     return;
  78. }
  79.  
  80. signed main()
  81. {
  82.     // __START__;
  83.     ios_base::sync_with_stdio(false);
  84.     cin.tie(NULL);
  85.     cout.tie(NULL);
  86.     int t = 1;
  87.     // cin >> t;
  88.     while (t--)
  89.     {
  90.         solve();
  91.     }
  92.     // __END__;
  93.     return 0;
  94. }
Tags: Atcoder 0-1bfs
Advertisement
Add Comment
Please, Sign In to add comment