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;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define deb(x) cout<<#x<<'='<<x<<'\n';
#define deb2(x,y) cout<<#x<<'='<<x<<", "<<#y<<'='<<y<<'\n';
#define int long long
#define all(x) (x).begin(), (x).end()
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second
#define sz(x) (int)(x).size()
const long double PI = acos(-1);
const int mod = 1e9 + 7, inf = 1e10;
const int D = 4010;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
int n, m;
string a[D];
int d[D][D];
void solve_test() {
cin >> n >> m;
for (int i = 0; i < n; i++)
cin >> a[i];
deque<pii> q;
q.pb({0, 0});
d[0][0] = 1;
int ans = 1;
while (!q.empty()) {
pii u = q.front();
int ux = u.f, uy = u.s;
q.pop_front();
ans = max(ans, d[ux][uy]);
for (int i = 0; i < 4; i++) {
int vx = ux + dx[i];
int vy = uy + dy[i];
if (vx<0 or vx >= n or vy<0 or vy >= m)
continue;
if (a[vx][vy] == '.')
continue;
int wt = (a[ux][uy] != a[vx][vy]);
if (d[vx][vy] == 0) {
d[vx][vy] = d[ux][uy] + wt;
if (wt) q.pb({vx, vy});
else q.push_front({vx, vy});
}
}
}
cout << ans << '\n';
}
signed main() {
fastio
solve_test();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |