이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const ll inf = 1e18;
const int maxn = 100;
int di[4] = {-1, 0, 1, 0};
int dj[4] = {0, 1, 0, -1};
int N, M, A, B;
ll K, T;
array<array<ll, maxn>, maxn> C, cur, nxt;
// dp[k][i][j] = max sum for path to here after k steps
void dp_step() {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
nxt[i][j] = -inf;
for (int d = 0; d < 4; ++d) {
int i1 = i + di[d], j1 = j + dj[d];
if (i1 >= 0 && i1 < N && j1 >= 0 && j1 < M) {
nxt[i][j] = max(nxt[i][j], cur[i1][j1] + C[i][j]);
}
}
}
}
swap(cur, nxt);
}
void simulate() {
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
ll gains = 0;
for (int d = 0; d < 4; ++d) {
int i1 = i + di[d], j1 = j + dj[d];
if (i1 >= 0 && i1 < N && j1 >= 0 && j1 < M) {
gains = max(gains, C[i1][j1] + C[i][j]);
}
}
cur[i][j] += gains * (K - 2*T) / 2;
}
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
cin >> N >> M >> A >> B >> K;
--A, --B;
T = N * M * 2;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < M; ++j) {
cin >> C[i][j];
cur[i][j] = -inf;
}
}
cur[A][B] = 0;
if (2*T >= K) {
for (int i = 0; i < K; ++i) {
dp_step();
}
} else {
// T steps, simulate going back and forth, T steps again
for (int i = 0; i < T; ++i) {
dp_step();
}
simulate();
for (int i = 0; i < T; ++i) {
dp_step();
}
}
cout << cur[A][B] << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |