poj 1928 peanuts 排序

The Peanuts
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6863   Accepted: 2834

Description

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. 
poj 1928 peanuts 排序_第1张图片
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<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
using namespace std;
struct node{
        int x,y,ans;
          }a[3000];
int cmp(node a,node b)
{
   return a.ans>b.ans;
}

int main()
{

//   freopen("ysh.text","r",stdin);
//   freopen("ans.text","w",stdout);
   int i,j,n,m,k,x,y,z,time,ge,t,spend;
   scanf("%d",&z);
   while(z--)
   {
      ge=0;
      memset(a,0,sizeof(a));
      scanf("%d %d %d",&n,&m,&time);
      for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
        {
         scanf("%d",&a[(i-1)*m+j].ans);
         if( a[(i-1)*m+j].ans >0 )ge++;         //记录有多少个有花生的点
         a[ (i-1)*m+j ].x=i;                    //记录每个点的坐标
         a[ (i-1)*m+j ].y=j;
        } 
        sort(a+1,a+1+i*j,cmp);                  //对有花生的点进行排序             

        y=a[1].y;
        x=0;

        t=0;j=0;i=1;
        int sum=0;
        while(t<=time)
        {
 //       cout<<"t="<<t<<endl;
          if(j==ge)break;
          spend= abs(a[i].x-x)+1+abs(a[ i ].y-y)+a[i].x;  //判断取完花生后能否返回
          if(t+spend>time)break;
          spend-=a[i].x;                                  //能返回的话就减去剩余时间
          t+=spend;
          sum+=a[i].ans;                                  //累加采摘的花生的数量
          x=a[i].x;
          y=a[i].y;
          i++;
          j++;
        }
        printf("%d\n",sum);
   }







}









你可能感兴趣的:(poj 1928 peanuts 排序)