POJ 1928 The Peanuts

Source: Internet
Author: User

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 cocould 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 shoshould go to the plant with the most peanuts first. after picking them, he shoshould 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 cocould 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 cocould pick. you can assume that each point contains a different amount of peanuts, counter t 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 13 0 00 0 0 0 0 0 70 15 0 0 0 0 00 0 9 0 0 00 0 0 0 0 0 0 06 7 200 0 0 0 0 0 0 0 0 13 0 0 0 0 0 0 70 15 0 0 0 0 0 0 9 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Sample Output

3728


Train of Thought: first sort the number of peanuts from large to small, and then determine whether to time out.

1 # include <stdio. h> 2 # include <math. h> 3 # include <stdlib. h> 4 5 struct point 6 {7 int x; 8 int y; 9 int num; 10} node [2510], temp; 11 12 int init (int m, int n) 13 {14 int t, I, j, k = 0; 15 for (I = 1; I <= m; ++ I) 16 for (j = 1; j <= n; ++ j) 17 {18 scanf ("% d", & t); 19 if (t) 20 {21 node [k]. x = j; 22 node [k]. y = I; 23 node [k]. num = t; 24 + + k; 25} 26} 27 return k; 28} 29 30 int comp (const void * p1, const void * p2) 31 {32 return (* (struct point *) p2 ). num-(* (struct point *) p1 ). num; 33} 34 35 int main () 36 {37 int T, m, n, I, time, k; 38 long sum; 39 scanf ("% d ", & T); 40 while (T --) 41 {42 scanf ("% d", & m, & n, & time); 43 sum = 0; 44 k = init (m, n); 45 qsort (node, k, sizeof (struct point), comp); 46 time-= node [0]. y; 47 for (I = 0; I <k; ++ I) 48 {49 if (time> = node [I]. y + 1) 50 {51 sum + = node [I]. num; 52 time-= abs (node [I]. x-node [I + 1]. x) + abs (node [I]. y-node [I + 1]. y) + 1; 53} 54 else55 break; 56} 57 printf ("% d \ n", sum); 58} 59 return 0; 60}
View Code

 

POJ 1928 The Peanuts

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.