题目描述:
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
这题绝对是一道水题,纯粹用来考英文的,题目大致意思是给你一个m*n矩
阵,在一定时间k内能摘的花生最多,而每次都是去含最多花生的节点上摘,
所以只要对每个节点上的花生数排序,同时记录他的xy坐标,取的时候从上
往下,看够不够时间摘这个节点上的花生,够的话摘,不够退出。但这里有
个注意点,就是关于结构体的排序
struct ss
需要重载<符号,这样就可以像int一样比较两个结构体的大小了,排序
就可以调用sort,qsort了,我这里用的是set,其实就排序效率来讲都
差不多的;
代码:
#include<iostream> #include<set> #include<iterator> using namespace std; int min(int x,int y) { if (x<y) return x; else return y; } struct ss { int p,x,y; bool operator<(const ss b ) const { return (p<b.p); } }; int main() { int t,n,i,j,k,ans,time,m,u,_a; ss temp,a[3000]; set<ss> v; set<ss>::iterator it; while(cin>>t) for(u=1;u<=t;u++) { cin>>n>>m>>k; v.clear(); for(i=1;i<=n;i++) for(j=1;j<=m;j++) { cin>>temp.p; if (temp.p>0) { temp.x=i;temp.y=j; v.insert(temp); } } _a=0; for(it=v.begin();it!=v.end();it++) { a[++_a]=*it; // cout<<(*it).p<<' '; } ans=0;time=0; time+=a[_a].x; a[0]=a[1]; while((time+1+a[_a].x<=k)&&(_a>=1)) { ans+=a[_a].p; time+=a[_a].x+a[_a-1].x-2*min(a[_a].x,a[_a-1].x)+a[_a].y+a[_a-1].y-2*min(a[_a].y,a[_a-1].y)+1; _a--; } cout<<ans<<endl; } return 0; }