#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
typedef struct {
int x;
int y;
int num;
}PeaNut;
bool DWGreater(const PeaNut rhs, const PeaNut lhs)
{
return (rhs.num > lhs.num);
}
const int gettime(1);
const int inouttime(1);
int main()
{
int N,M,K;
PeaNut VecPeanut[400];
int sumtime(0);
int sumcnt(0);
int reachtime(0); /* 到达下一位置所需时间 */
int rettime(0); /* 从下一位置返回所需时间 */
PeaNut lastpea; /* 当前所处位置 */
int pos(0);
N = 0;
M = 0;
K = 0;
while (cin >> N >> M >> K) {
if (K == 0) {
cout << sumcnt;
continue;
}
for (int idx = 0; idx < N; idx++) {
for (int jdx = 0; jdx < M; jdx++) {
VecPeanut[idx*M + jdx].x = idx;
VecPeanut[idx*M + jdx].y = jdx;
cin >> VecPeanut[idx*M + jdx].num;
}
}
sort(VecPeanut, VecPeanut+(M*N), DWGreater); /* 按num从大到小排序 */
lastpea.x = -1;
lastpea.y = VecPeanut[0].y;
sumtime = 0;
sumcnt = 0;
reachtime = 0;
rettime = 0;
pos = 0;
while ((pos < M*N) && (VecPeanut[pos].num > 0)) {
reachtime = abs(VecPeanut[pos].x - lastpea.x) + abs(VecPeanut[pos].y - lastpea.y);
rettime = VecPeanut[pos].x + inouttime;
if ((sumtime + (reachtime + gettime + rettime)) > K) {
break;
} else {
sumtime += (reachtime + gettime); /* 累计时间 */
lastpea = VecPeanut[pos]; /* 更新上一次位置 */
sumcnt += VecPeanut[pos].num; /* 获取花生数量 */
pos++;
}
}
cout << sumcnt;
}
return 0;
}
在codeblock上运行测试几组正常,
放到牛客pat上直接段错误,麻烦大神指点下