#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
using namespace std;
long long best(long long a, long long b, long long c, long long d) {
return max(max(a, b), max(c, d));
}
int main() {
long long n, m, a, b, k;
cin >> n >> m >> a >> b >> k;
a--;
b--;
long long grid[105][105];
long long dp[105][105];
bool vis[105][105];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
dp[i][j] = -1e9;
vis[i][j] = false;
}
}
dp[a][b] = 0;
vis[a][b] = true;
queue<pair<int, int>> check;
check.push({ a + 1, b });
check.push({ a - 1, b });
check.push({ a, b - 1 });
check.push({ a, b + 1 });
while (check.size() > 0) {
int x = check.front().first, y = check.front().second;
check.pop();
dp[x][y] = true;
if (x < 0 || x >= n || y < 0 || y >= m || dp[x][y] >= 0) {
continue;
}
dp[x][y] = best(dp[x - 1][y], dp[x + 1][y], dp[x][y - 1], dp[x][y + 1]) + grid[x][y];
if(x + 1 < n && !dp[x + 1][y])
check.push({ x + 1, y });
if(x - 1 >= 0 && !dp[x - 1][y])
check.push({ x - 1, y });
if(y + 1 < m && !dp[x][y + 1])
check.push({ x, y + 1 });
if(y - 1 >= 0 && !dp[x][y - 1])
check.push({ x, y - 1 });
}
long long ans = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
long long diff = max(i - a, a - i) + max(j - b, b - j);
long long cur = best(grid[i - 1][j], grid[i + 1][j], grid[i][j - 1], grid[i][j + 1]) + grid[i][j];
ans = max(ans, dp[i][j] + dp[i][j] - grid[i][j] + (k - diff * 2) / 2 * cur);
}
}
cout << ans << endl;
}
Compilation message
maja.cpp: In function 'int main()':
maja.cpp:24:7: warning: variable 'vis' set but not used [-Wunused-but-set-variable]
24 | bool vis[105][105];
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
416 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |