Luo gu 1086 peanut picking
Address: http://www.luogu.org/problem/show?pid=1086
Title Description
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 (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.
Input/output format
Input format:
The first line of the input file peanuts.in consists of three integers, M, N and K, separated by a space, which indicates that the peanut field size 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.
Output format:
The output file Peanuts.out includes one row, which contains only an integer, that is, the maximum number of peanuts that can be harvested within a limited time.
Input/Output sample
Input Sample # #:
"Sample Input 1" 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 input 2" 6 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 # # of output:
Sample Output 1 "37" Sample output 2 "28
Description
The 2nd question of noip2004 popularization group
Exercises
Analog + Sort
The idea of the problem is very clear, first of all we will plant by the number of peanuts in descending order (here is bubbling, the data is all right, but the author is recommended to use fast row), can reduce the search time. Enumeration finds the most recent plant in the peanut, and then calculates the time from the current position to the plant. Special Note: We need to consider whether the remaining time after reaching the plant can return to the roadside, if not, then directly from the current position back to the roadside (not too greedy oh). Guaranteed to harvest the most peanuts within a limited time. The following code is attached.
Code
- Program Peanuts;
- Var
- M,n,k,c,t,x,xx,y,yy,max,i,j,l:longint;
- A:array[1..20,1..20] of longint;
- B:array[1..400] of longint;
- Begin
- READLN (M,N,K);
- c:=0;
- max:=0;
- For i:=1 to m do
- For j:=1 to n do
- begin
- Read (A[i,j]);
- if a[i,j]<>0 Then
- begin
- Inc (c);
- B[C]:=A[I,J];
- end;
- end;
- For i:=1 to C do
- For j:=1 to c-i do
- if b[j]<b[j+1] Then
- begin
- T:=B[J];
- b[j]:=b[j+1];
- b[j+1]:=t;
- end;
- For i:=1 to C do
- begin
- For j:=1 to m do
- For l:=1 to n do
- if b[i]=a[j,l] Then
- begin
- if i=1 then y:=l;
- xx:=ABS (J-X);
- yy:=ABS (L-Y);
- if xx+yy+j+1>k Then
- begin
- Writeln (max);
- Halt
- End
- Else
- begin
- Dec (k,xx+yy+1);
- Inc (Max,b[i]);
- end;
- X:=j;
- Y:=l;
- end;
- end;
- Writeln (max);
- End.
(This article is the author original, without permission not reproduced)
A report on the problem of peanut picking in Luo gu 1086