Submission #1251660

#TimeUsernameProblemLanguageResultExecution timeMemory
1251660kunzaZa183Tracks in the Snow (BOI13_tracks)C++20
97.81 / 100
1343 ms1114112 KiB
#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  vector<string> vs(n);
  for (auto &a : vs)
    cin >> a;

  vector<vector<int>> vvi(n, vector<int>(m, -1));
  vector<pair<int, int>> newones, new2;
  newones.push_back({0, 0});
  int ct = 0;

  const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
  function<void(int, int)> flood = [&](int x, int y) {
    if (vvi[x][y] != -1)
      return;
    vvi[x][y] = ct;
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
        if (vvi[x][y] != -1 && vs[nx][ny] != '.') {
          if (vs[nx][ny] == vs[x][y])
            flood(nx, ny);
          else {
            new2.push_back({nx, ny});
          }
        }
      }
    }
  };
  while (!newones.empty()) {
    ct++;

    for (auto [x, y] : newones) {
      flood(x, y);
    }

    new2.swap(newones);
    new2.clear();
  }

  int maxi = 0;
  for (auto &a : vvi)
    for (auto b : a)
      maxi = max(maxi, b);
  cout << maxi << "\n";
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...