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;
const int H = 4005;
const int dI[4] = {-1, 0, +1, 0};
const int dJ[4] = {0, +1, 0, -1};
int h, w;
char d[H][H];
int f[H][H];
bool prc[H][H];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> h >> w;
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
cin >> d[i][j];
}
}
deque<pair<int, int>> dq;
memset(f, 0x3f3f3f3f, sizeof(f));
f[h][w] = 1;
int mx = 1;
dq.push_front({h, w});
while (!dq.empty()) {
int x, y;
tie(x, y) = dq.front();
dq.pop_front();
if (prc[x][y]) {
continue;
}
mx = max(mx, f[x][y]);
prc[x][y] = true;
for (int dir = 0; dir < 4; dir++) {
int nx = x + dI[dir];
int ny = y + dJ[dir];
if (1 <= nx && nx <= h && 1 <= ny && ny <= w) {
if (d[nx][ny] != '.') {
if (d[x][y] == d[nx][ny]) {
if (f[nx][ny] > f[x][y]) {
f[nx][ny] = f[x][y];
dq.push_front({nx, ny});
}
} else if (f[nx][ny] > f[x][y] + 1) {
f[nx][ny] = f[x][y] + 1;
dq.push_back({nx, ny});
}
}
}
}
}
cout << mx;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |