Beingamanforever

E-Stronger Takahashi, 0-1 BFS

Jan 31st, 2025
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.69 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 = 2e5 + 5, L = 19;
  19. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  20. // -----------------------------------------------------------------------------
  21.  
  22. void solve()
  23. {
  24.     int n, m;
  25.     cin >> n >> m;
  26.     char grid[n + 1][m + 1];
  27.     int cost[n + 1][m + 1];
  28.     for (int i = 1; i <= n; i++)
  29.     {
  30.         for (int j = 1; j <= m; j++)
  31.         {
  32.             cin >> grid[i][j];
  33.         }
  34.     }
  35.     for (int i = 0; i <= n; i++)
  36.     {
  37.         fill(cost[i], cost[i] + m + 1, INF);
  38.     }
  39.     function<bool(int, int)> inside = [&](int x, int y)
  40.     {
  41.         return (x >= 1 && x <= n && y >= 1 && y <= m);
  42.     };
  43.     deque<pi> dq;
  44.     cost[1][1] = 0;
  45.     dq.push_back({1, 1});
  46.     // go, then punch
  47.     while (!dq.empty())
  48.     {
  49.         auto [x, y] = dq.front();
  50.         dq.pop_front();
  51.         // 0 cost
  52.         for (int i = 0; i < 4; i++)
  53.         {
  54.             int nx = x + dx[i], ny = y + dy[i];
  55.             if (inside(nx, ny))
  56.             {
  57.                 // 0 costs
  58.                 if (grid[nx][ny] == '.' && cost[nx][ny] > cost[x][y])
  59.                 {
  60.                     cost[nx][ny] = cost[x][y];
  61.                     dq.push_front({nx, ny});
  62.                 }
  63.                 // 1 cost
  64.                 else
  65.                 {
  66.                     // punch a 2x2 grid
  67.                     for (int j = -1; j <= 1; j++)
  68.                     {
  69.                         for (int k = -1; k <= 1; k++)
  70.                         {
  71.                             int nnx = nx + j, nny = ny + k;
  72.                             if (inside(nnx, nny) && cost[nnx][nny] > cost[x][y] + 1)
  73.                             {
  74.                                 cost[nnx][nny] = cost[x][y] + 1;
  75.                                 dq.push_back({nnx, nny});
  76.                             }
  77.                         }
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.     }
  83.     int ans = cost[n][m];
  84.     cout << ((ans == INF) ? -1 : ans) << endl;
  85.     return;
  86. }
  87.  
  88. signed main()
  89. {
  90.     // __START__;
  91.     ios_base::sync_with_stdio(false);
  92.     cin.tie(NULL);
  93.     cout.tie(NULL);
  94.     int t = 1;
  95.     // cin >> t;
  96.     while (t--)
  97.     {
  98.         solve();
  99.     }
  100.     // __END__;
  101.     return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment