#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<string> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
auto inside = [&](int x, int y) {
return (x >= 0 && x < n && y >= 0 && y < m && a[x][y] != '.');
};
vector<vector<int>> dp(n, vector<int>(m));
deque<pair<int, int>> q;
q.push_back({0, 0});
dp[0][0] = 1;
int res = 0;
while (!q.empty()) {
auto [x, y] = q.front();
q.pop_front();
res = max(res, dp[x][y]);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (inside(nx, ny) && dp[nx][ny] == 0) {
if (a[x][y] == a[nx][ny]) {
dp[nx][ny] = dp[x][y];
q.push_front({nx, ny});
} else {
dp[nx][ny] = dp[x][y] + 1;
q.push_back({nx, ny});
}
}
}
}
cout << res << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |