题解:主要是仔细看题,注意边界问题(比如第一个和最后一个要判断能否走回路边)
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y;
int val;
} p[500];
int m, n, k, cnt;
inline int read() {
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
return x * f;
}
inline bool cmp(node a, node b) {
return a.val > b.val;
}
inline int dfs(int sum_time, int sum_nut) {
for (int i = 1; i < cnt; ++i) {
if (sum_time + abs(p[i - 1].x - p[i].x) + abs(p[i - 1].y - p[i].y) +
p[i].x + 1 <=
k) {
sum_time += abs(p[i - 1].x - p[i].x) + abs(p[i - 1].y - p[i].y) + 1;
sum_nut += p[i].val;
} else
break;
}
return sum_nut;
}
int main() {
n = read(), m = read(), k = read();
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
p[cnt].val = read();
if (p[cnt].val) {
p[cnt].x = i, p[cnt].y = j;
cnt++;
}
}
}
sort(p, p + cnt, cmp);
if (p[0].x * 2 + 1 > k) {
cout << 0 << endl;
} else
cout << dfs(p[0].x + 1, p[0].val) << endl;
return 0;
}