Sdnu 1167. Peanut picking "NOIP 2004 popularization Group" "Greedy" "August 6"

Source: Internet
Author: User

Peanut picking

Description Robinson has a pet monkey named Toto. This day, they are walking along the country road, suddenly found on the roadside signs with a small note: "Welcome to free tasting my peanut!" --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 (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, in the limited time, how many can the maximum number of peanuts to 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. The first line of input inputs consists of three integers, M, N and K, separated by a space, which indicates that the peanut field is M * N (1 <= m, n <= 20), and the limit time 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. The output outputs include one row, which contains only an integer, that is, the maximum number of peanuts that can be harvested within a limited time. Sample Input
6 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 0
Sample Output
37
At first thought is DP what kind of, carefully read the question:"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 have to go back to the curb within my time limit. This sentence is very clear that is greedy, from the most scanning, can pick up the pick, pick not come back on the pick, back to the roadside. The code is as follows:

#include <cstdio> #include <algorithm>using namespace std;struct pe{    int x,y,val;}; BOOL CMP (PE X,pe y) {    if (x.val>y.val) return true;    else return false;} int main () {    int m,n,k,f,g=0,xi,yi=0,sum=0;    PE v[410];    scanf ("%d%d%d", &m,&n,&k);    for (int i=1;i<=m;i++)    for (int j=1;j<=n;j++) {        scanf ("%d", &f);        if (f>0) {//record position and number of Peanuts            v[g].val=f;            V[g].x=j;            v[g++].y=i;        }    }    Sort (v,v+g,cmp);    xi=v[0].x;    for (int i=0;i<g;i++) {//Calculate the required time        int xmax=xi>v[i].x?xi:v[i].x;        int xmin=xi>v[i].x?v[i].x:xi;        int ymax=yi>v[i].y?yi:v[i].y;        int ymin=yi>v[i].y?v[i].y:yi;        if (xmax-xmin+ymax-ymin+v[i].y+1<=k) {            sum+=v[i].val;            k=k-(xmax-xmin+ymax-ymin+1);            xi=v[i].x;            yi=v[i].y;        }        else break;    }    printf ("%d\n", sum);    return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sdnu 1167. Peanut picking "NOIP 2004 popularization Group" "Greedy" "August 6"

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.