#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> meadow;
int dx[4]{1, -1, 0, 0}, dy[4]{0, 0, 1, -1};
bool check(int x, int y) {
return x >= 0 && y >= 0 && x < n && y < m && meadow[x][y] != '.';
}
int main() {
cin >> n >> m;
meadow.resize(n);
for (int i = 0; i < n; i++) cin >> meadow[i];
int ans = 1;
vector<vector<int>> dist(n, vector<int>(m));
deque<pair<int, int>> q;
q.push_back({0, 0});
dist[0][0] = 1;
while (!q.empty()) {
pair<int, int> c = q.front();
q.pop_front();
ans = max(ans, dist[c.first][c.second]);
for (int i = 0; i < 4; i++) {
int x = c.first + dx[i], y = c.second + dy[i];
if (check(x, y) && dist[x][y] == 0) {
if (meadow[x][y] == meadow[c.first][c.second]) {
dist[x][y] = dist[c.first][c.second];
q.push_front({x, y});
}else {
dist[x][y] = dist[c.first][c.second] + 1;
q.push_back({x, y});
}
}
}
}
cout << ans << endl;
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |