fjnu 1013 The Peanuts

来源:互联网 发布:淘宝外围正向和反向 编辑:程序博客网 时间:2024/04/30 15:19
 

Description

Mr. Robinson and his pet monkey Dodo love peanuts very much. One day while they were having a walk on a country road, Dodo found a sign by the road, pasted with a small piece of paper, saying "Free Peanuts Here! " You can imagine how happy Mr. Robinson and Dodo were.

There was a peanut field on one side of the road. The peanuts were planted on the intersecting points of a grid as shown in Figure-1. At each point, there are either zero or more peanuts. For example, in Figure-2, only four points have more than zero peanuts, and the numbers are 15, 13, 9 and 7 respectively. One could only walk from an intersection point to one of the four adjacent points, taking one unit of time. It also takes one unit of time to do one of the following: to walk from the road to the field, to walk from the field to the road, or pick peanuts on a point.

According to Mr. Robinson's requirement, Dodo should go to the plant with the most peanuts first. After picking them, he should then go to the next plant with the most peanuts, and so on. Mr. Robinson was not so patient as to wait for Dodo to pick all the peanuts and he asked Dodo to return to the road in a certain period of time. For example, Dodo could pick 37 peanuts within 21 units of time in the situation given in Figure-2.

Your task is, given the distribution of the peanuts and a certain period of time, tell how many peanuts Dodo could pick. You can assume that each point contains a different amount of peanuts, except 0, which may appear more than once.

Input

The first line of input contains the test case number T (1 <= T <= 20). For each test case, the first line contains three integers, M, N and K (1 <= M, N <= 50, 0 <= K <= 20000). Each of the following M lines contain N integers. None of the integers will exceed 3000. (M * N) describes the peanut field. The j-th integer X in the i-th line means there are X peanuts on the point (i, j). K means Dodo must return to the road in K units of time.

Output

For each test case, print one line containing the amount of peanuts Dodo can pick.

Sample Input

26 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 06 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 Output

3728
 
KEY:
由于猴子取果子要按照一定的顺序来取,所以要保证你取下一个果子,能够回去,貌似也没有什么要注意的了
  1. Source:
  2. #include<iostream>
  3. #include<algorithm>
  4. using namespace std;
  5. struct node 
  6. {
  7.     int x;
  8.     int y;
  9.     int num;
  10. };
  11. node a[2501];
  12. int b[100][100];
  13. int n,m;
  14. int k;
  15. int units;
  16. int ToRoad(node x)
  17. {
  18.     return x.x;
  19. }
  20. int Distance(node x,node y)
  21. {
  22.     return abs(x.x-y.x)+abs(x.y-y.y);
  23. }
  24. void input()
  25. {
  26.     int i,j;
  27.     for(i=1;i<=n;i++)
  28.         for(j=1;j<=m;j++)
  29.         {
  30.             cin>>b[i][j];
  31.             if(b[i][j]>0)
  32.             {
  33.                 a[++k].x=i;
  34.                 a[k].y=j;
  35.                 a[k].num=b[i][j];
  36.             }
  37.         }
  38. }
  39. int count()
  40. {
  41.     int nuts=0;
  42.     int i;
  43.     for(i=1;i<=k;i++)
  44.     {
  45.         if(i==1)
  46.         {
  47.             units-=ToRoad(a[i]);
  48.             nuts+=a[i].num;
  49.             units--;            
  50.             if(units-ToRoad(a[i])<0)
  51.             {
  52.                 return 0;
  53.             }
  54.         }
  55.         else
  56.         {
  57.             if(units>=Distance(a[i-1],a[i])+1+ToRoad(a[i]))
  58.             {
  59.                 nuts+=a[i].num;
  60.                 units-=Distance(a[i-1],a[i]);
  61.                 units--;
  62.             }
  63.             else
  64.             {
  65.                 return nuts;
  66.             }
  67.         }
  68.     }
  69.     return nuts;
  70. }
  71. int compare(node x,node y)
  72. {
  73.     if(x.num>y.num) return 1;
  74.     else return 0;
  75. }
  76. void init()
  77. {
  78.     k=0;
  79.     int i,j;
  80.     for(i=1;i<=50;i++)
  81.         for(j=1;j<=50;j++)
  82.             b[i][j]=0;
  83.     n=m=0;
  84.     units=0;
  85. }
  86. int main()
  87. {
  88.  //   freopen("fjnu_1013.in","r",stdin);
  89.     int N;
  90.     cin>>N;
  91.     int i;
  92.     for(i=1;i<=N;i++)
  93.     {
  94.         init();
  95.         cin>>n>>m>>units;
  96.         input();
  97.         sort(a+1,a+k+1,compare);
  98.         int nuts=count();
  99.         cout<<nuts<<endl;
  100.     }
  101.     return 0;
  102. }