This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
int depth[4001][4001], ans = 1;
string s[4001];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) {
cin >> s[i];
}
int dx[]{0, -1, 0, 1, 0};
auto good = [&](int x, int y) { return x >= 0 && x < n && y >= 0 && y < m && s[x][y] != '.'; };
deque<pair<int, int>> dq;
dq.push_back({0, 0});
depth[0][0] = 1;
while (!dq.empty()) {
auto [x, y] = dq.front();
dq.pop_front();
ans = max(ans, depth[x][y]);
for (int i = 0; i < 4; i++) {
int nx = x + dx[i], ny = y + dx[i + 1];
if (good(nx, ny) && depth[nx][ny] == 0) {
if (s[x][y] == s[nx][ny]) {
depth[nx][ny] = depth[x][y];
dq.push_front({nx, ny});
} else {
depth[nx][ny] = depth[x][y] + 1;
dq.push_back({nx, ny});
}
}
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |