The description of the topic is:
Mr. 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. 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 one 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, peanuts are planted in the peanut fields (2, 5), (3, 7), (4, 2), and (5, 4), with the numbers being 13, 7, 15, 9, respectively. More than 21 units of time, only through (4, 2), (2, 5), (5, 4), up to a maximum of 37 peanuts.
Input Description:
Input contains more than one set of data, the first row of each group of data consists of three integers m (1≤m≤20), N (1≤n≤20) and K (0≤k≤1000), separated by a space, the size of the peanut field is M * N, a lot of peanut harvest time is K unit time.
Immediately following the M line, each row consists of N natural number P (0≤p≤500) separated by a space, and the number of peanuts in the peanut field is indicated, and the number of peanuts in all other plants is different except for 0 (no peanuts).
Output Description:
corresponding to each set of data, output an integer, that is, in the limited time, more than the maximum number of peanuts can be harvested.
Input Example:
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
Output Example:
37
My Code is:
1#include <iostream>2#include <memory.h>3#include <algorithm>4 using namespacestd;5 6 structMAX7 {8 intvalue;9 intHang ;Ten intlie; One }; A - //Specify descending order - BOOLCMP (MAX A,max B) the { - returnA.value>B.value; - } - + intMain () - { + intm,n,k; A inta[ -][ -]; at -MAX max[ -]; - while(1) - { -cin>>m>>n;//the size of the peanut field - incin>>k;//time limit - toMemset (A,0,sizeof(a)); +memset (Max,0,sizeof(max)); - the intI=0, j=0; * for(i=0; i<m;i++) $ for(j=0; j<n;j++)Panax NotoginsengCin>>A[i][j]; - the //find out where peanut-containing plants are located + A intD=0; the for(i=0; i<m;i++) + { - for(j=0; j<n;j++) $ { $ if(a[i][j]>0)//record the number of peanuts in a row. - { -Max[d].value=A[i][j]; themax[d].hang=i+1; -max[d].lie=j+1;Wuyid++; the } - Wu } - } About $ //sort the size of the found number -Sort (max,max+d,cmp); - - //start picking Peanuts . A intT=k,l=0; + intsum=0; the - //First time $I=0; the if(t>2) the { the //start looking from the roadside . the -T= (t-max[0].hang-1+1-1);//first one in the //jump to the plant and decide if you can jump back to the curb the if((t-max[0].hang-1+1) >=0) About { thesum+=max[0].value; the while(t>=0) the { +i++; -L=abs (max[i].hang-max[i-1].hang) +abs (max[i].lie-max[i-1].lie); theT= (t-l-1);Bayi if((T-max[i].hang) <0) the { the Break; - } -sum+=Max[i].value; the } the } the the - } the Else the { the 94sum=0; the the } the 98cout<<sum<<Endl; About - 101 } 102 return 0; 103}
Show Time_out??? It's depressing.
[Programming questions] Take peanuts-why is the time super?