제출 #304816

#제출 시각아이디문제언어결과실행 시간메모리
304816shrek12357Maja (COCI18_maja)C++14
11 / 110
2091 ms65540 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
using namespace std;

int best(int a, int b, int c, int d) {
	return max(max(a, b), max(c, d));
}

int main() {
	int n, m, a, b, k;
	cin >> n >> m >> a >> b >> k;
	a--;
	b--;
	int grid[105][105];
	int dp[105][105];
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> grid[i][j];
			dp[i][j] = (int)-1e9;
		}
	}
	dp[a][b] = 0;
	
	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();
		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];
		check.push({ x + 1, y });
		check.push({ x - 1, y });
		check.push({ x, y + 1 });
		check.push({ x, y - 1 });
	}
	int ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			int diff = max(i - a, a - i) + max(j - b, b - j);
			int 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;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...