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;
using vi = vector<int>;
using vvi = vector<vector<int> >;
using pi = pair<int, int>;
using ll = long long;
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define f first
#define s second
#define pb push_back
#define MOD 1e9 + 7
int h, w, ans = 1;
vector<string> snow(4000);
int dx[4] {1, -1, 0, 0}, dy[4] {0, 0, 1, -1};
vvi dist(4000, vi (4000));
bool isInside(int x, int y) {
return x >= 0 && x < h && y >= 0 && y < h && snow[x][y] != '.';
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> h >> w;
for (int i = 0; i < h; i++) cin >> snow[i];
deque<pi> dq;
dq.push_front({0, 0});
dist[0][0] = 1;
while (!dq.empty()) {
auto curr = dq.front();
dq.pop_front();
ans = max(ans, dist[curr.f][curr.s]);
for (int i = 0; i < 4; i++) {
int x = curr.f + dx[i], y = curr.s + dy[i];
if (isInside(x, y) && dist[x][y] == 0) {
if (snow[x][y] == snow[curr.f][curr.s]) {
dist[x][y] = dist[curr.f][curr.s];
dq.push_front({x, y});
} else {
dist[x][y] = dist[curr.f][curr.s] + 1;
dq.push_back({x, y});
}
}
}
}
cout << ans << "\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |