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;
typedef long long ll;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define X first
#define Y second
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << ": " << x << endl;
const ll MAXN = 4e3 + 10;
const int DX[4] = {0, -1, 0, 1};
const int DY[4] = {-1, 0, 1, 0};
int state[MAXN][MAXN], n, m, dist[MAXN][MAXN];
deque<pll> dq;
inline void update(int i, int j, int d, int td) {
if (!state[i][j]) return;
if (d + td < dist[i][j]) {
dist[i][j] = d + td;
if (td == 0) dq.push_front({i, j});
else dq.push_back({i, j});
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
char c;
cin >> c;
if (c == 'F') state[i][j] = 1;
else if (c == 'R') state[i][j] = 2;
}
}
memset(dist, 63, sizeof dist);
dq.push_back({1, 1});
dist[1][1] = 0;
while (!dq.empty()) {
auto [x, y] = dq.front();
dq.pop_front();
int d = dist[x][y];
for (int f = 0; f < 4; f++) {
int tx = x + DX[f], ty = y + DY[f];
update(tx, ty, d, int(state[x][y] != state[tx][ty]));
}
}
int ans = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (state[i][j])
ans = max(ans, dist[i][j]);
cout << ans + 1 << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |