#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int inf = 1e9;
void solve() {
int n, m; cin >> n >> m;
vector<string> s(n);
for(auto &str : s) cin >> str;
vector d(n, vector(m, inf));
d[0][0] = 1;
deque<array<int, 2>> q;
q.push_back({0, 0});
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ans = 0;
while(!q.empty()) {
auto [x, y] = q.front();
q.pop_front();
ans = max(ans, d[x][y]);
for(int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dy[i];
if(nx < 0 || ny < 0 || nx >= n || ny >= m || s[nx][ny] == '.') continue;
if(s[x][y] == s[nx][ny]) {
if(d[nx][ny] > d[x][y]) {
d[nx][ny] = d[x][y];
q.push_front({nx, ny});
}
} else {
if(d[nx][ny] > d[x][y] + 1) {
d[nx][ny] = d[x][y] + 1;
q.push_back({nx, ny});
}
}
}
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |