Submission #1343595

#TimeUsernameProblemLanguageResultExecution timeMemory
1343595krishgargTracks in the Snow (BOI13_tracks)C++20
100 / 100
673 ms208860 KiB
// https://oj.uz/problem/view/BOI13_tracks

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;

using dbl = long double;
using ll = long long;
using str = string;
using ch = char;
using vll = vector<ll>;
using vdbl = vector<dbl>;
using vvll = vector<vll>;
using pll = pair<ll, ll>;
using mpll = map<ll, ll>;
using vpll = vector<pll>;
#define eb emplace_back
#define pb push_back
#define fo(i, k, n) \
  for (ll i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
#define fu(i, k, n) for (ll i = k; i <= n; i++)
#define fd(i, k, n) for (ll i = k; i >= n; i--)
#define tin0(a, n) fo(i, 0, n) cin >> a[i]
#define tin1(a, n) fu(i, 1, n) cin >> a[i]
#define all(v) v.begin(), v.end()
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define f first
#define s second

typedef __gnu_pbds::tree<int, __gnu_pbds::null_type, less<int>,
                         __gnu_pbds::rb_tree_tag,
                         __gnu_pbds::tree_order_statistics_node_update>
    ordered_set;

ll pow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) res *= a;
    a *= a;
    b >>= 1;
  }
  return res;
}

ll modpow(ll a, ll b, ll m) {
  ll res = 1;
  while (b) {
    if (b & 1) res = (res * a) % m;
    a = (a * a) % m;
    b >>= 1;
  }
  return res;
}

ll inv(ll a, ll m) { return modpow(a, m - 2, m); }

void solve() {
  ll h, w;
  cin >> h >> w;
  vector<vector<ch>> grid(h, vector<ch>(w));
  fu(i, 0, h - 1) {
    fu(j, 0, w - 1) { cin >> grid[i][j]; }
  }

  auto inside = [&](ll x, ll y) {
    return x < h && y < w && x > -1 && y > -1 && grid[x][y] != '.';
  };

  deque<pll> q;
  vll dx = {1, -1, 0, 0};
  vll dy = {0, 0, 1, -1};
  vvll d(h, vll(w, 0));

  q.push_front({0, 0});
  d[0][0] = 1;
  ll ans = 1;

  while (!q.empty()) {
    pll c = q.front();
    q.pop_front();

    ans = max(ans, d[c.f][c.s]);

    fu(i, 0, 3) {
      ll x = c.f + dx[i], y = c.s + dy[i];
      if (inside(x, y) && d[x][y] == 0) {
        if (grid[x][y] == grid[c.f][c.s]) {
          d[x][y] = d[c.f][c.s];
          q.push_front({x, y});
        } else {
          d[x][y] = d[c.f][c.s] + 1;
          q.push_back({x, y});
        }
      }
    }
  }

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(0);

  // ll t;
  // cin >> t;
  // while (t--) {
  //   solve();
  // }

  solve();
  return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...