The Peanuts
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 172
Accepted Submission(s): 101
Problem 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.
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
Recommend
JGShining
原来老师推荐的课本上的原题,题意就是从路边到田地里采花生,但是要在制定的时间之内回来,直接模拟就行了,要注意的是,回来时还要在最上边的路上,而不是四周任何一条路就可以,而且,采花生的时候也不能跑出田地的范围。
代码:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
typedef struct point{
int x,y,num;
}target;
target map[2555];
int cmp(const void *a,const void *b)
{
struct point *c,*d;
c=(struct point *)a;
d=(struct point *)b;
return d->num-c->num;
}
int main()
{
int t,i,j,m,n,k,a,q,sum,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&m,&n,&k);
q=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
scanf("%d",&a);
if(a>0)
{
map[q].x=i;
map[q].y=j;
map[q].num=a;
q++;
}
}
qsort(map,q,sizeof(map[0]),cmp);
sum=0;a=0;b=map[0].y;i=0;
while(k>0){
if(i==q)break;
if(abs(map[i].x-a)+abs(map[i].y-b)+map[i].x+1>k)
break;
else{
sum+=map[i].num;
k-=abs(map[i].x-a)+abs(map[i].y-b)+1;
a=map[i].x;b=map[i].y;
i++;
}
}
printf("%d\n",sum);
}
return 0;
}