#include <bits/stdc++.h>
using namespace std;
const int mn = 4000;
short vvi[mn][mn];
char vs[mn][mn + 1];
int n, m;
int ct = 0;
vector<pair<int, int>> newones, new2;
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
void flood(int x, int y) {
vvi[x][y] = ct;
// cout << x << " " << y << "\n";
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[nx][ny] == 0 && vs[nx][ny] != '.') {
if (vs[nx][ny] == vs[x][y]) {
vvi[nx][ny] = ct;
flood(nx, ny);
} else {
new2.push_back({nx, ny});
}
}
}
}
};
int main() {
// cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> vs[i];
newones.push_back({0, 0});
while (!newones.empty()) {
ct++;
for (auto [x, y] : newones) {
flood(x, y);
}
new2.swap(newones);
new2.clear();
}
short maxi = 0;
for (auto &a : vvi)
for (auto b : a)
maxi = max(maxi, b);
cout << maxi << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |