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 dx[4] = {0, -1, 0, 1};
int dy[4] = {1, 0, -1, 0};
const int MAXN = 4000;
int n, m;
bool check (int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
char a[MAXN][MAXN];
bool vis[MAXN][MAXN];
int dist[MAXN][MAXN];
deque <pair <int, int>> cur;
signed main () {
ios::sync_with_stdio(0); cin.tie(0);
memset(vis, false, sizeof(vis));
cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> a[i][j];
cur.push_back({0, 0}); vis[0][0] = 1;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) dist[i][j] = 1e9;
dist[0][0] = 0;
int ans = 0;
while (!cur.empty()) {
auto k = cur.front(); cur.pop_front();
ans = max(ans, dist[k.first][k.second]);
for (int i = 0; i < 4; i++) {
int x = k.first + dx[i], y = k.second + dy[i];
if (!check(x, y)) continue;
if (a[x][y] == a[k.first][k.second]) {
if (dist[k.first][k.second] < dist[x][y]) {
dist[x][y] = dist[k.first][k.second];
cur.push_front({x, y});
}
} else {
if (dist[k.first][k.second] + 1 < dist[x][y]) {
dist[x][y] = dist[k.first][k.second] + 1;
cur.push_back({x, y});
}
}
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |