把花生数按从大到小排序,然后一个一个的去试,能摘就摘,不能摘就结束了.
View Code
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
struct node{
float x,y;
int num;
}a[2505];
float cmp(node x,node y)
{
return x.num>y.num;
}
int main()
{
int i,j,k,n,r,c,num,ans,len,step,limit;
scanf("%d",&n);
while(n--)
{
scanf("%d%d%d",&r,&c,&limit);
for(i=0,k=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&num);
if(num>0)
{
a[k].x=i;
a[k].y=j;
a[k++].num=num;
}
}
}
sort(a,a+k,cmp);
if(2*a[0].x+3>limit)
{
printf("0\n");
continue;
}
step=a[0].x+3;
ans=a[0].num;
for(i=1;i<k;i++)
{
len=(int)fabs(a[i].x-a[i-1].x)+(int)fabs(a[i].y-a[i-1].y);
if(step+len+a[i].x+1>limit)break;
step+=len+1;
ans+=a[i].num;
}
printf("%d\n",ans);
}
return 0;
}