이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |