Poj 1928 peanuts sorting

Source: Internet
Author: User
The Peanuts
Time Limit:1000 MS   Memory Limit:30000 K
Total Submissions:6863   Accepted:2834

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,
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 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
Algorithm idea: sort the number of peanuts contained in all vertices to obtain the maximum number of peanuts. Note that the number of peanuts varies. Then the peanut edge is taken out to determine the time.
# Include <stdio. h> # include <string. h> # include <math. h> # include <algorithm> # include <iostream> # include <stdlib. h> using namespace std; struct node {int x, y, ans;} a [1, 3000]; int cmp (node a, node B) {return. ans> B. ans;} int main () {// freopen ("ysh. text "," r ", stdin); // freopen (" ans. text "," w ", stdout); int I, j, n, m, k, x, y, z, time, ge, t, spend; scanf ("% d", & z); while (z --) {ge = 0; memset (a, 0, sizeof ()); scanf ("% d", & n, & m, & time); for (I = 1; I <= n; I ++) for (j = 1; j <= m; j ++) {scanf ("% d", & a [(I-1) * m + j]. ans); if (a [(I-1) * m + j]. ans> 0) ge ++; // record the number of points with peanuts a [(I-1) * m + j]. x = I; // record the coordinates of each point a [(I-1) * m + j]. y = j;} sort (a + 1, a + 1 + I * j, cmp); // sort the points with peanuts. y = a [1]. y; x = 0; t = 0; j = 0; I = 1; int sum = 0; while (t <= time) {// cout <"t =" <t <endl; if (j = ge) break; spend = abs (a [I]. x-x) + 1 + abs (a [I]. y-y) + a [I]. x; // determine whether the if (t + spend> time) break can be returned after obtaining the peanut; spend-= a [I]. x; // if the result is returned, the remaining time t + = spend is subtracted. sum + = a [I]. ans; // The number of accumulated peanuts x = a [I]. x; y = a [I]. y; I ++; j ++;} printf ("% d \ n", sum );}}

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.