首页 > 代码库 > POJ 1928 The Peanuts
POJ 1928 The Peanuts
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.
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
26 7 210 0 0 0 0 0 00 0 0 0 13 0 00 0 0 0 0 0 70 15 0 0 0 0 00 0 0 9 0 0 00 0 0 0 0 0 06 7 200 0 0 0 0 0 00 0 0 0 13 0 00 0 0 0 0 0 70 15 0 0 0 0 00 0 0 9 0 0 00 0 0 0 0 0 0
Sample Output
3728
思路:先把花生个数从大到小排序,然后判断是否超时。
1 #include<stdio.h> 2 #include<math.h> 3 #include<stdlib.h> 4 5 struct point 6 { 7 int x; 8 int y; 9 int num;10 }node[2510],temp;11 12 int init(int m,int n) 13 {14 int t,i,j,k=0;15 for(i=1;i<=m;++i)16 for(j=1;j<=n;++j)17 {18 scanf("%d",&t);19 if(t)20 {21 node[k].x=j;22 node[k].y=i;23 node[k].num=t;24 ++k;25 }26 }27 return k;28 }29 30 int comp(const void *p1,const void *p2)31 {32 return (*(struct point *)p2).num-(*(struct point *)p1).num;33 }34 35 int main()36 {37 int T,m,n,i,time,k;38 long sum;39 scanf("%d",&T);40 while(T--)41 {42 scanf("%d%d%d",&m,&n,&time);43 sum=0;44 k=init(m,n);45 qsort(node,k,sizeof(struct point),comp);46 time-=node[0].y;47 for(i=0;i<k;++i)48 {49 if(time>=node[i].y+1)50 {51 sum+=node[i].num;52 time-=abs(node[i].x-node[i+1].x)+abs(node[i].y-node[i+1].y)+1;53 }54 else55 break;56 }57 printf("%d\n",sum);58 }59 return 0;60 }
POJ 1928 The Peanuts
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。