The Peanuts POJ - 1928

 

Mr. Robinson and his pet monkey Dodo love peanuts very much. One day while they were having a walk on a country road, Dodo found a sign by the road, pasted with a small piece of paper, saying "Free Peanuts Here! " You can imagine how happy Mr. Robinson and Dodo were. 

There was a peanut field on one side of the road. The peanuts were planted on the intersecting points of a grid as shown in Figure-1. At each point, there are either zero or more peanuts. For example, in Figure-2, only four points have more than zero peanuts, and the numbers are 15, 13, 9 and 7 respectively. One could only walk from an intersection point to one of the four adjacent points, taking one unit of time. It also takes one unit of time to do one of the following: to walk from the road to the field, to walk from the field to the road, or pick peanuts on a point. 


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. Mr. Robinson was not so patient as to wait for Dodo to pick all the peanuts and he asked Dodo to return to the road in a certain period of time. For example, Dodo could pick 37 peanuts within 21 units of time in the situation given in Figure-2.

Your task is, given the distribution of the peanuts and a certain period of time, tell how many peanuts Dodo could pick. You can assume that each point contains a different amount of peanuts, except 0, which may appear more than once. 

 

Input

The first line of input contains the test case number T (1 <= T <= 20). For each test case, the first line contains three integers, M, N and K (1 <= M, N <= 50, 0 <= K <= 20000). Each of the following M lines contain N integers. None of the integers will exceed 3000. (M * N) describes the peanut field. The j-th integer X in the i-th line means there are X peanuts on the point (i, j). K means Dodo must return to the road in K units of time.

 

Output

For each test case, print one line containing the amount of peanuts Dodo can pick.

 

Sample Input

2
6 7 21
0 0 0 0 0 0 0
0 0 0 0 13 0 0
0 0 0 0 0 0 7
0 15 0 0 0 0 0
0 0 0 9 0 0 0
0 0 0 0 0 0 0
6 7 20
0 0 0 0 0 0 0
0 0 0 0 13 0 0
0 0 0 0 0 0 7
0 15 0 0 0 0 0
0 0 0 9 0 0 0
0 0 0 0 0 0 0

Sample Output

37
28

这个题虽然写着有些麻烦,但是重在思维锻炼,思考去采摘花生所用的时间

然后把花生在的位置,用快速排序的方法,排到最前面,并且记录花生原本所在的坐标

接着从比较采摘花生所用时间和输入时间下手,依次记录花生个数,一旦时间超过所输入的时间,就输出目前所采摘到的花生个数

 

代码如下:

#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;

struct node{
    int row,col,num;
}p[3000];      //记录花生所在的x坐标和y坐标还有个数

int cmp(node a,node b)
{
    return a.num>b.num;
}      //把花生个数最多的位置排在前面,优先记录

int step(int a,int b,int c,int d)
{
    return (abs(a-c)+abs(b-d));
}     //计算两个位置之间的距离

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int m,n,k,l=0;
        cin>>m>>n>>k;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>p[l].num;
                p[l].row=i;
                p[l].col=j;
                l++;
            }        //统计每个位置的花生个数还有x坐标与y坐标
        }
        sort(p,p+l,cmp);    //排序,把有花生的位置按照花生个数从大到小排序
        int time=0,sum=0;
        if((p[0].row*2+3)<=k)
        {
            time+=p[0].row+2;
            sum+=p[0].num;
        }      //比较走到花生最多的位置采摘花生后并返回,所用时间是否超过输入的时间
        if(time==0)
        {
            cout<<"0"<<endl;
            continue;
        }    
        for(int i=1;i<l;i++)
        {
            if((time+step(p[i].row,p[i].col,p[i-1].row,p[i-1].col)+2+p[i].row)<=k)  
//计算从第i-1花生的位置到第i花生位置并采摘花生返回路上所用的时间是否超过输入的时间
            {
                time+=(step(p[i].row,p[i].col,p[i-1].row,p[i-1].col)+1);   
 //记录从开始到在i位置采摘花生后一共用的时间
                sum+=p[i].num;     //记录花生个数
            }
            else
            break;
        }
        cout<<sum<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值