poj1928!【数学】

/*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

Source

Beijing 2004 Preliminary@POJ
*/
#include<stdio.h>
#include<stdlib.h>
struct point
{
	int row;
	int col;
	int num;
} p[2550];
int cmp(const void *a, const void *b) 
{
	return  (*(struct point *)b).num - (*(struct point *)a).num;
}
int step(int a, int b, int c, int d)//两点之间的时间 
{
	return (abs(a-c) + abs(b-d));
}
int main()
{
	int m, n, k, i , j, l, t;
	scanf("%d", &t);
	while(t--)
	{
		int sum = 0, time = 0;
		scanf("%d%d%d", &m, &n, &k);
		l = 0;
		for(i = 0;i  < m; i++)
		for(j = 0; j <n; j++)
		{
			scanf("%d", &p[l].num);
			p[l].row = i;
			p[l].col = j;
			l++;
		}
		qsort(p, l, sizeof(p[0]), cmp);
		if( ( p[0].row * 2 + 3) <= k)
		{
			time += ( p[0].row + 2);
			sum += p[0].num;
		}
		if(time == 0)       //刚开始忘了结果为0的情况, 哎 
		{
			printf("0\n");
			continue;
		}
		for(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)
			{
				time  += (step(p[i].row,p[i].col, p[i-1].row, p[i-1].col) + 1);
				sum += p[i].num;
			}
			else
			break;
		}
		printf("%d\n", sum);
	}
	return 0;
}


 

题意:找到数组中最大的数,并计和,在规定时间内,按照数字从大到小来计和,求出在规定时间内所能得到的最大值。

注意点:来自本题的讨论区

一:这个题目读题时应该仔细读。有的同学没有看到每次只能拿剩下花生株中最
大的,而是希望找到一种在规定时间内能够拿最多花生的组合,把题目变成了另外一道题。  
二:有的同学没有读到“没有两株花生株的花生数目相同”的条件,因此把题目
复杂化了。 
三:这个题目是假设猴子在取花生的过程中不会回到大路上的,有些同学在思考
是否可能在中间回到大路上,因为题目没说在大路上移动要花时间,所以有可能中途出来再
进去摘的花生更多。 

 

你可能感兴趣的:(c)