hdu 1355 The Peanuts

题目链接..

/*********************************


看清题目,直接模拟,题中已经说了怎么做了:According to Mr. Robinson's requirement, Dodo should go to the plant with the most peanuts first. After picking them, he should then go to the next plant with the most peanuts, and so on. 
从多到少开始摘。

 *******************************/

#include"stdio.h"
#include"math.h"
#include"stdlib.h"
int map[51][51],n,m,k;
struct node
{
    int x,y,count;
}aa[2502];
int cmp(const void*a,const void*b)
{
    return (*(struct node*)b).count-(*(struct node*)a).count;
}
int main()
{
    int t,i,j,l,a,T,xx,yy,temp,ans;;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&k);
        l=0;
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=m;j++)
            {
                scanf("%d",&map[i][j]);
                if(map[i][j]!=0)
                {
                    aa[l].x=i;
                    aa[l].y=j;
                    aa[l++].count=map[i][j];
                }
            }
        }
        qsort(aa,l,sizeof(aa[0]),cmp);
        t=0;ans=0;
        xx=0;
        yy=aa[0].y;
        for(i=0;i<l;i++)
        {
            temp=fabs(aa[i].x-xx)+fabs(aa[i].y-yy);
            if(t+temp+aa[i].x+1<=k)
            {
                t=t+temp+1;
                xx=aa[i].x;
                yy=aa[i].y;
                ans+=aa[i].count;
            }
            else break;
        }
        printf("%d\n",ans);
    }
    return 0;
}



原文地址:https://www.cnblogs.com/yyf573462811/p/6365350.html