zoukankan      html  css  js  c++  java
  • POJ 1928 The Peanuts

    The Peanuts
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 6500   Accepted: 2736

    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

    2
    6 7 21
    0 0 0 0 0 0 0
    0 0 0 0 13 0 0
    0 0 0 0 0 0 7
    0 15 0 0 0 0 0
    0 0 0 9 0 0 0
    0 0 0 0 0 0 0
    6 7 20
    0 0 0 0 0 0 0
    0 0 0 0 13 0 0
    0 0 0 0 0 0 7
    0 15 0 0 0 0 0
    0 0 0 9 0 0 0
    0 0 0 0 0 0 0
    

    Sample Output

    37
    28
      1 /* 
      2    功能Function Description:     POJ 1928  The Peanuts  
      3    开发环境Environment:          DEV C++ 4.9.9.1
      4    技术特点Technique:
      5    版本Version:
      6    作者Author:                   可笑痴狂
      7    日期Date:                     20120727
      8    备注Notes:                    结构体+排序
      9 */
     10 /*
     11 //代码一:冒泡排序   196K 141MS 
     12 #include<stdio.h>
     13 #include<math.h>
     14 
     15 struct point
     16 {
     17     int x;
     18     int y;
     19     int num;
     20 }node[2510],temp;
     21 
     22 int init(int m,int n)           //初始化存储有花生的节点,并返回节点个数
     23 {
     24     int t,i,j,k=0;
     25     for(i=1;i<=m;++i)
     26         for(j=1;j<=n;++j)
     27         {
     28             scanf("%d",&t);
     29             if(t)
     30             {
     31                 node[k].x=j;
     32                 node[k].y=i;
     33                 node[k].num=t;
     34                 ++k;
     35             }
     36         }
     37         return k;
     38 }
     39 
     40 int main()
     41 {
     42     int T,m,n,i,j,time,k;
     43     long sum;
     44     scanf("%d",&T);
     45     while(T--)
     46     {
     47         scanf("%d%d%d",&m,&n,&time);
     48         sum=0;
     49         k=init(m,n);
     50         for(i=k-1;i>0;--i)
     51             for(j=0;j<i;++j)
     52             {
     53                 if(node[j].num<node[j+1].num)
     54                 {
     55                     temp=node[j];
     56                     node[j]=node[j+1];
     57                     node[j+1]=temp;
     58                 }
     59             }
     60         time-=node[0].y;
     61         for(i=0;i<k;++i)
     62         {
     63             if(time>=node[i].y+1)
     64             {
     65                 sum+=node[i].num;
     66                 time-=abs(node[i].x-node[i+1].x)+abs(node[i].y-node[i+1].y)+1;
     67             }
     68             else
     69                 break;
     70         }
     71         printf("%d\n",sum);
     72     }
     73     return 0;
     74 }
     75 
     76  */
     77 
     78 //代码二:用qsort实现快排  196K 16MS 
     79 #include<stdio.h>
     80 #include<math.h>
     81 #include<stdlib.h>
     82 
     83 struct point
     84 {
     85     int x;
     86     int y;
     87     int num;
     88 }node[2510],temp;
     89 
     90 int init(int m,int n)           //初始化存储有花生的节点,并返回节点个数
     91 {
     92     int t,i,j,k=0;
     93     for(i=1;i<=m;++i)
     94         for(j=1;j<=n;++j)
     95         {
     96             scanf("%d",&t);
     97             if(t)
     98             {
     99                 node[k].x=j;
    100                 node[k].y=i;
    101                 node[k].num=t;
    102                 ++k;
    103             }
    104         }
    105         return k;
    106 }
    107 
    108 int comp(const void *p1,const void *p2)
    109 {
    110     return (*(struct point *)p2).num-(*(struct point *)p1).num;
    111 }
    112 
    113 int main()
    114 {
    115     int T,m,n,i,time,k;
    116     long sum;
    117     scanf("%d",&T);
    118     while(T--)
    119     {
    120         scanf("%d%d%d",&m,&n,&time);
    121         sum=0;
    122         k=init(m,n);
    123         qsort(node,k,sizeof(struct point),comp);
    124         time-=node[0].y;
    125         for(i=0;i<k;++i)
    126         {
    127             if(time>=node[i].y+1)
    128             {
    129                 sum+=node[i].num;
    130                 time-=abs(node[i].x-node[i+1].x)+abs(node[i].y-node[i+1].y)+1;
    131             }
    132             else
    133                 break;
    134         }
    135         printf("%d\n",sum);
    136     }
    137     return 0;
    138 }
    功不成,身已退
  • 相关阅读:
    poj 3636
    poj 1065 Wooden Sticks
    2017北京国庆刷题Day6 afternoon
    2017北京国庆刷题Day5 afternoon
    2017北京国庆刷题Day3 afternoon
    2017北京国庆刷题Day3 morning
    2017北京国庆刷题Day2 afternoon
    2017北京国庆刷题Day2 morning
    poj2420 A Star not a Tree?
    NOIP2016 愤怒的小鸟
  • 原文地址:https://www.cnblogs.com/dongsheng/p/2611507.html
Copyright © 2011-2022 走看看