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>
#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("avx2","popcnt","sse4","abm")
using namespace std;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
using ll = long long;
typedef pair<int, int> pii;
const int MXN = 4005;
int n, m;
string s[MXN];
int dis[MXN][MXN];
void BFS(int srx, int sry) {
deque<pii> dq;
FOR(i, 0, n + 2) FOR(j, 0, m + 2) dis[i][j] = -1;
auto PUSH = [&](int x, int y, int dx, int dy) -> void {
dx += x;
dy += y;
if (s[dx][dy] == '.') return;
if (dis[dx][dy] != -1) return;
if (s[x][y] == s[dx][dy]) {
dis[dx][dy] = dis[x][y];
dq.push_front(mp(dx, dy));
} else {
dis[dx][dy] = dis[x][y] + 1;
dq.push_back(mp(dx, dy));
}
};
dis[srx][sry] = 1;
dq.push_back(mp(srx, sry));
while (dq.size()) {
auto [x, y] = dq.front();
dq.pop_front();
PUSH(x, y, 1, 0);
PUSH(x, y, -1, 0);
PUSH(x, y, 0, 1);
PUSH(x, y, 0, -1);
}
}
void miku() {
cin >> n >> m;
FOR(i, 1, n + 1) {
cin >> s[i];
s[i] = '.' + s[i] + '.';
}
s[0] = string(m + 2, '.');
s[n + 1] = string(m + 2, '.');
BFS(1, 1);
int ans = 0;
FOR(i, 1, n + 1) FOR(j, 1, m + 1) {
if (s[i][j] == '.') continue;
ans = max(ans, dis[i][j]);
}
cout << ans << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |