You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
2.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 30;
int n, m, k;
int g[N][N];
PII get_max() {
PII r;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (g[r.x][r.y] < g[i][j])
r = {i, j};
return r;
}
int main() {
cin >> n >> m >> k; // 花生田的大小n*m,k为限定的时间
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> g[i][j];
// 首轮次第一个位置
PII t = get_max();
// ① 拿起一个花生需要1个时间
// ② 从界外进入比如进到第二行来时需要走2步回去需要走2步再加上拿起的时间共5个时间
if (t.x * 2 + 1 > k) { // 从界外进入花生田,直线距离到了回不去了,表示最大的取不到啊~,之所以加1是因为摘花生还需要一个单位的时间
puts("0");
exit(0);
}
// 先把最大值拿走,之所以第一个与后续的不同,是因为第一个可以从界外任意一列进入,而其它的是从(x1,y1)某个点到(x2,y2)的,有区别,需单独处理
int res = g[t.x][t.y]; // 拿到这个位置上的花生
k -= t.x + 1; // 拿起来第一个需要1个时间走到第t.x行需要t.x个时间
g[t.x][t.y] = 0; // 这个位置上的花生没有了
while (true) { // 不断的取花生不断的减少时间k
PII r = get_max();
if (g[r.x][r.y] == 0) break; // ① 如果没有花生存在,就退出循环
int d = abs(r.x - t.x) + abs(r.y - t.y); // 从当前位置,走到下一个最大值位置,需要走的步数
if (d + 1 + r.x > k) break; // ② 如果去摘了下一个最大值的花生,那么时间上是不是允许呢?如果不行就停止
res += g[r.x][r.y]; // 摘到这个花生
g[r.x][r.y] = 0; // 将此位置上的花生数量清零
k -= d + 1; // 时间上减去本轮的移动时间和摘花生的时间
t = r; // 更新当前位置
}
// 输出结果
printf("%d\n", res);
return 0;
}