A report on the problem of peanut picking in Luo gu 1086

Source: Internet
Author: User

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
  1. Program Peanuts;
  2. Var
  3. M,n,k,c,t,x,xx,y,yy,max,i,j,l:longint;
  4. A:array[1..20,1..20] of longint;
  5. B:array[1..400] of longint;
  6. Begin
  7. READLN (M,N,K);
  8. c:=0;
  9. max:=0;
  10. For i:=1 to m do
  11. For j:=1 to n do
  12. begin
  13. Read (A[i,j]);
  14. if a[i,j]<>0 Then
  15. begin
  16. Inc (c);
  17. B[C]:=A[I,J];
  18. end;
  19. end;
  20. For i:=1 to C do
  21. For j:=1 to c-i do
  22. if b[j]<b[j+1] Then
  23. begin
  24. T:=B[J];
  25. b[j]:=b[j+1];
  26. b[j+1]:=t;
  27. end;
  28. For i:=1 to C do
  29. begin
  30. For j:=1 to m do
  31. For l:=1 to n do
  32. if b[i]=a[j,l] Then
  33. begin
  34. if i=1 then y:=l;
  35. xx:=ABS (J-X);
  36. yy:=ABS (L-Y);
  37. if xx+yy+j+1>k Then
  38. begin
  39. Writeln (max);
  40. Halt
  41. End
  42. Else
  43. begin
  44. Dec (k,xx+yy+1);
  45. Inc (Max,b[i]);
  46. end;
  47. X:=j;
  48. Y:=l;
  49. end;
  50. end;
  51. Writeln (max);
  52. End.

(This article is the author original, without permission not reproduced)

A report on the problem of peanut picking in Luo gu 1086

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.