이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define sz(x) int(x.size())
#define in(x) freopen(x, "r", stdin)
#define out(x) freopen(x, "w", stdout)
#define N 100500
#define oo ll(1e16)
#define ft first
#define sd second
#define pb push_back
#define ppb pop_back
#define el '\n'
#define elf endl
#define base ll(1e9 + 7)
using namespace std;
typedef long long ll;
typedef long double ld;
ll n, m, sx, sy, k, f[2][101][101], a[101][101], ans;
bool gd(int x, int y, int sx, int sy, int r) { return (abs(x - sx) + abs(y - sy) < r); }
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// in("input.txt");
cin >> n >> m >> sx >> sy >> k;
sx--; sy--;
k /= 2;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j], f[0][i][j] = -1;
f[0][sx][sy] = 0;
for (int t = 1; t <= k; t++) {
int cr = t & 1;
int lst = cr ^ 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
f[cr][i][j] = -1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (f[lst][i][j] == -1)
continue;
if (i - 1 >= 0) f[cr][i - 1][j] = max(f[cr][i - 1][j], f[lst][i][j] + a[i - 1][j]);
if (j - 1 >= 0) f[cr][i][j - 1] = max(f[cr][i][j - 1], f[lst][i][j] + a[i][j - 1]);
if (i + 1 < n) f[cr][i + 1][j] = max(f[cr][i + 1][j], f[lst][i][j] + a[i + 1][j]);
if (j + 1 < m) f[cr][i][j + 1] = max(f[cr][i][j + 1], f[lst][i][j] + a[i][j + 1]);
}
}
int t = (k - 1) & 1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
if (!(abs(i - sx) + abs(j - sy) <= k))
continue;
if (i - 1 >= 0 && gd(i - 1, j, sx, sy, k)) ans = max(ans, f[t][i - 1][j] * 2 + a[i][j]);
if (j - 1 >= 0 && gd(i, j - 1, sx, sy, k)) ans = max(ans, f[t][i][j - 1] * 2 + a[i][j]);
if (i + 1 < n && gd(i + 1, j, sx, sy, k)) ans = max(ans, f[t][i + 1][j] * 2 + a[i][j]);
if (j + 1 < m && gd(i, j + 1, sx, sy, k)) ans = max(ans, f[t][i][j + 1] * 2 + a[i][j]);
}
cout << ans;
}
# | 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... |