noip2004 Peanut Picking (enumeration)

Source: Internet
Author: User
Tags abs printf first row time limit
A1148. Peanut picking time limit: 1.0s memory limit: 256.0MB Total commits: 321 AC: 102 average score: 57.04 share the subject to: View unformatted questions submit questions source N OIP2004 Universal group Problem description Mr. Robinson has a pet monkey named Toto. On this day, they were walking along the country road and suddenly found a little note on the sign on the roadside: "Welcome to the free tasting of my peanuts." --Bear word ".
Mr. Robinson and Toto are very happy, because peanuts are their favorite. Behind the sign, there really is a peanut field on the roadside, and the peanut plants are neatly arranged in a rectangular grid (Figure 1). With a lot of experience, you can see how many peanuts are in each peanut plant. In order to train a lot of arithmetic, Mr. Robinson said: "You first find the most peanuts plant, to pick its peanuts, and then find the remaining plants in the most peanuts, to pick its peanuts; and so on, but you must be in my limited time to return to the roadside. ”
We assume that a lot can do one of the following four things in each unit time:
1) jump from the roadside to a peanut plant closest to the roadside (i.e. the first row);
2) jump from one plant to another plant adjacent to or around it;
3) Picking peanuts under a plant;
4) Jump back to the roadside from a peanut plant closest to the roadside (i.e. the first row).
Now given the size of a peanut field and the distribution of peanuts, I would like to ask in a limited time, more than the maximum number of peanuts can be harvested. Note that it is possible that only some of the plants have peanuts under them, assuming that the number of peanuts under these plants varies.
For example, in the Peanut field shown in Figure 2, there are peanuts in the plants (2, 5), (3, 7), (4, 2), (5, 4), respectively, 13, 7, 15, 9. Along the route of the diagram, a maximum of 37 peanuts can be harvested in 21 unit time.
Input format the first line of the input file consists of three integers, M, N and K, separated by spaces, indicating that the peanut field size is M * N (1 <= m, n <= 20), the time limit for a lot of peanuts is K (0 <= k <= 1000) unit time. The next M-line, each line includes n non-negative integers, and is separated by a space; the J-Integer pij (0 <= pij <= 500) of line I + 1 indicates the number of peanuts under the Peanut field plant (i, j), and 0 indicates that there are no peanuts under the plant. Output format output file includes a row, this line contains only one integer, that is, in a limited time, the maximum number of peanuts can be harvested. Sample Input 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 Sample Output 37 Example input 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 28 data size and conventions


Resolution: It has been said that each point of the number of peanuts are different, then you can directly have each peanut point according to the number of peanuts sorted. And then according to the number of peanuts from large to small enumeration, for the I point of the peanut, if a lot of this peanut can also return to the roadside, then the enumeration is ended.

Code:

#include <cstdio>
#include <algorithm>
#define MAXN
using namespace std;
struct Tnode{int x,y,s;} A[MAXN*MAXN+50];
int m,n,t;

BOOL CMP (tnode A,tnode b)
{
  return a.s>b.s;
} 

int main ()
{
  int i,j,k=0,x,y,z;
  scanf ("%d%d%d", &m,&n,&t);
  for (i=1;i<=m;i++)
    for (j=1;j<=n;j++)
      {
        scanf ("%d", &a[k].s);
        if (a[k].s!=0) a[k].x=i,a[k].y=j,k++;
      }
  Sort (a,a+k,cmp);
  
  if (a[0].x*2+1>t) {printf ("0\n"); return;}
  X=A[0].X+1,Y=A[0].S;
  for (i=1;i<k;i++)
    {
      z=abs (a[i].x-a[i-1].x) +abs (A[I].Y-A[I-1].Y);
      if (x+z+1+a[i].x<=t) x+= (z+1), Y+=A[I].S;
      else 
        {
          x+=a[i-1].x;
          break;
        }
    }
  printf ("%d\n", y);
  return 0;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.