#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define all(x) x.begin(), x.end()
template<class A, class B>
bool chmin(A &a, const B &b) {
return a > b ? (a = b) == b : false;
} template<class A, class B>
bool chmax(A &a, const B &b) {
return a < b ? (a = b) == b : false;
}
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
signed main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m; cin >> n >> m;
vector<string> s(n);
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
auto in = [&](int i, int j) {
return i >= 0 && i < n && j >= 0 && j < m;
};
deque<pair<int, int>> q;
vector<vector<int>> d(n, vector<int> (m, -1));
q.push_back({0, 0});
d[0][0] = 1;
while (!q.empty()) {
auto [i, j] = q.front();
q.pop_front();
for (int k = 0; k < 4; ++k) {
int x = i + dx[k];
int y = j + dy[k];
if (in(x, y) && d[x][y] == -1 && s[x][y] != '.') {
if (s[x][y] == s[i][j]) {
d[x][y] = d[i][j];
q.push_front({x, y});
} else {
d[x][y] = d[i][j] + 1;
q.push_back({x, y});
}
}
}
}
int res = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
chmax(res, d[i][j]);
}
}
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |