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>
#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... |