1382 I'm big stomach king

Source: Internet
Author: User
Description

Background]

2008.11.16

In the Mr Lv organization, the 3721 class held a group dumplings activity in the dining room of SAT :)

This event was a great success and everyone was very happy. The so-called "Hangzhou, Beijing Airlines ".

After coming down, someone developed a set of exercise questions based on the activity ......

------------------------------------------------

O (distinct _ distinct) O... Haha, this activity is fun. Every small class sends a representative. Who is in the competition for 30 seconds (OR 1 minute ?) The largest number of dumplings can be eaten. You can drink water!

Mr Wang was the final winner. When "interviewing", he broke the secret of success:

"In fact, I did some work before the start of the competition;

There are a lot of dumplings in front of me, I will first choose some dishes to eat, after all, not much time, it takes time to change the plate;

The rule I selected is:

First, divide the plane in front of me into N * m square boxes. Some square boxes contain plates and know the number of dumplings in them. For positions without plates, enter 0;

Then select the number of dumplings from the maximum to the minimum. I guess I can take out the r disk, so I only select the r disk;

However, if the distance between the two disks is greater than P, I will not select the next disk, skip until the distance of a disk is less than or equal to P;

For example, when I take the I disk, the position is (3, 3), and the next disk is (1, 1) in order, but P is 2, so I won't select it, here P is defined as | x1-x2 | + | y1-y2 |, Jump directly to the following disk, is according to the number of dumplings from large to small sequence, assuming that the plate position is (3, 2) then you can choose the I + 1 dumplings;

Of course, if you choose not enough R disks according to this rule ......

En ~ Thank you to the Party and the people. Thank you, mr. lv, for giving me this opportunity to prove that my algorithm is correct ......

"

Dizzy ...... As a judge, Please calculate the number of dumplings according to this rule;

Input

The first line of the input data contains an integer T, indicating that there are T groups of test data;

For each group of test data:

The first row contains four integers, n, m, R, and P. The meaning of each data is as follows;

Next is a matrix of N * M. Each element is a non-negative integer;

Ensure that N and m are both less than or equal to 100, R is less than N * m, and P is greater than 0;

To avoid ambiguity, make sure that the number of dumplings on each plate is not equal;

Output

Output The number of dumplings eaten by the big stomach king according to the topic description rules;

Sample Input
1
5 5 3 3
0 0 0 0 2
0 5 0 0 0
0 0 1 0 0
3 0 0 4 0
0 0 0 0 0
Sample output
9
(Explanation: 5 + 3 + 1)

 

 

Simulation questions

#include <iostream>#include <cstdio>#include<algorithm>using namespace std;struct node{    int x;    int y;    int data;};node a[10001];bool cmp(node a, node b){    return (a.data > b.data);}int main(){    int t;    scanf("%d", &t);    while(t --)    {        int n, m, r, p;        scanf("%d%d%d%d", &n, &m, &p, &r);        int i, j, k;        k = 0;        for(i = 0; i != n; i ++)            for(j = 0; j != m; j ++)            {                scanf("%d", &a[k].data);                if(a[k].data != 0)                {                    a[k].x = i;                    a[k].y = j;                    ++ k;                }            }        sort(a, a + k, cmp);        j = 0;        int sum = 0;        sum += a[j].data;        node temp;        temp = a[j];        ++ j;        int amount = 1;        while(amount < p)        {            if(abs(a[j].x - temp.x)+ abs(a[j].y - temp.y) <= r)            {                sum += a[j].data;                temp = a[j];                amount ++;            }            j ++;            if(j > k)                break;        }        printf("%d\n", sum);    }    return 0;}

 

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.