이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define PB push_back
using namespace std;
typedef long long ll;
const int N = 110;
const int PW = 22;
int c[N][N], a, b, n, m, k;
ll ans[4 * N][N][N];
void MAX(ll &x, ll y){
x = max(x, y);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> a >> b >> k;
a--; b--;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> c[i][j];
ans[0][a][b] = 1;
for (int ite = 1; ite <= 2 * (n + m); ite++){
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++){
if (ans[ite - 1][i][j] == 0) continue;
if (i > 0) MAX(ans[ite][i - 1][j], ans[ite - 1][i][j] + c[i - 1][j]);
if (j > 0) MAX(ans[ite][i][j - 1], ans[ite - 1][i][j] + c[i][j - 1]);
if (j + 1 < m) MAX(ans[ite][i][j + 1], ans[ite - 1][i][j] + c[i][j + 1]);
if (i + 1 < n) MAX(ans[ite][i + 1][j], ans[ite - 1][i][j] + c[i + 1][j]);
}
}
if (k <= 2 * n * m)
cout << ans[k][a][b] - 1 << '\n';
else {
ll res = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
for (int st = 0; st <= 2 * (n + m); st++){
if (ans[i][j][st] == 0) continue;
if (st + st > k) break;
ll cur = ans[i][j][st] - 1;
ll mx = 0;
if (i > 0) MAX(mx, c[i - 1][j]);
if (j > 0) MAX(mx, c[i][j - 1]);
if (j + 1 < m) MAX(mx, c[i][j + 1]);
if (i + 1 < n) MAX(mx, c[i + 1][j]);
mx += c[i][j];
res = max(res, cur * 2 + mx * (k - st * 2) / 2);
}
cout << res;
}
return 0;
}
# | 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... |