牛客网 PAT乙级练习题 1001采花生

版权声明:转载请联系博主 https://blog.csdn.net/zxtalentwolf/article/details/80152117
链接:https://www.nowcoder.com/questionTerminal/83740e9b96074dd89edaf9bfad43cac3
来源:牛客网
鲁宾逊先生有一只宠物猴,名叫多多。这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!——熊字”。
 鲁宾逊先生和多多都很开心,因为花生正是他们的最爱。在告示牌背后,路边真的有一块花生田,花生植株整齐地排列成矩形网格。有经验的多多一眼就能看出,每棵花生植株下的花生有多少。为了训练多多的算术,鲁宾逊先生说:“你先找出花生最多的植株,去采摘它的花生;然后再找出剩下的植株里花生最多的,去采摘它的花生;依此类推,不过你一定要在我限定的时间内回到路边。”
 我们假定多多在每个单位时间内,可以做下列四件事情中的一件:
 1. 从路边跳到最靠近路边(即第一行)的某棵花生植株;
 2. 从一棵植株跳到前后左右与之相邻的另一棵植株;
 3. 采摘一棵植株下的花生;
 4. 从最靠近路边(即第一行)的某棵花生植株跳回路边。
 现在给定一块花生田的大小和花生的分布,请问在限定时间内,多多最多可以采到多少个花生?
 注意可能只有部分植株下面长有花生,假设这些植株下的花生个数各不相同。例如花生田里只有位于(2, 5), (3, 7), (4, 2), (5,
4)的植株下长有花生,个数分别为 13, 7, 15, 9。多多在 21 个单位时间内,只能经过(4, 2)、(2, 5)、(5,
4),最多可以采到 37 个花生。

输入描述:
输入包含多组数据,每组数据第一行包括三个整数 M(1≤M≤20)、N(1≤N≤20)和 K(0≤K≤1000),用空格隔开;表示花生田的大小为 M * N,多多采花生的限定时间为 K个单位时间。紧接着 M 行,每行包括 N 个自然数 P(0≤P≤500),用空格隔开;表示花生田里植株下花生的数目,并且除了0(没有花生),其他所有植株下花生的数目都不相同。

输出描述:
对应每一组数据,输出一个整数,即在限定时间内,多多最多可以采到花生的个数。

这个题很坑,因为描述不清楚,前面的评论写的人描述的也不是很清楚,我这里把有坑的地方给大家说明一下:

  1. 输入是要循环输入的,不是只有一组用例,有很多组,如果输入一组用例退出的话会报错,具体可以参考我的程序
  2. 这个题的意思是,从路边到第一个花生不用算列的时间,直接从第0行到第n行(第一个花生所在的行数),用n的时间,采摘+1时间,然后到下一个花生,不会返回路边,只有到最后一个花生才会返回路边

PS:统一回复一下前面栈溢出,输出错误例子不明确的同学,牛客网的例子输出好像有bug,一般这种情况都是你们程序写错了,不妨就认为没有错误用例输出重新改程序把,当acmer

/*
 * =====================================================================================
 *
 *       Filename:  1001.cpp
 *
 *    Description:  PAT乙级练习题:1001采花生
 *
 *        Version:  1.0
 *        Created:  2018/04/30 19:03:37
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:   Zhangxin
 *   Organization:
 *
 * =====================================================================================
 */
#include <stdlib.h>
#include<iostream>
#include<cmath>
#include <algorithm>
#include<vector>
using namespace std;

struct Penut {
    int row;
    int col;
    int value;
};                /* ----------  end of struct Penut  ---------- */
bool cmp(Penut a,Penut b) {
    return a.value>b.value;
}


typedef struct Penut Penut;

#include    <stdlib.h>

/*
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:
 * =====================================================================================
 */
int main ( int argc, char *argv[] ) {
    freopen("data.in","r",stdin);
    int m,n,k,r,c,v;
    while(scanf("%d %d %d",&m,&n,&k)==3) {
        vector<Penut> ps;
        Penut a;
        a.row=0;
        a.col=1;
        a.value=100000;
        ps.push_back(a);
        for(int i=1; i<=m; i++) {
            for(int j=1; j<=n; j++) {
                scanf("%d",&v);
                if(v!=0) {
                    Penut p;
                    p.row=i;
                    p.col=j;
                    p.value=v;
                    ps.push_back(p);
                    //             cout<<"input "<<v<<endl;
                }

            }
        }
        sort(ps.begin(),ps.end(),cmp);
        ps[0].col=ps[1].col;
      //  for(vector<Penut>::iterator i=ps.begin(); i!=ps.end(); i++) {
      //      printf("%d ",i->value);
      //  }
      //  cout<<endl;
        int time=0;
        int res=0;
        int index=1;
        while(time<k && index<ps.size()) {
            //  cout<<index<<endl;
            int temp=abs(ps[index].col-ps[index-1].col)+abs(ps[index].row-ps[index-1].row)+1;
        //    cout<<index<<" "<<time<<" "<<temp<<" "<<temp+time+ps[index].row<<endl;
            if(temp+time+ps[index].row<=k) {
                time=time+temp;
                //  cout<<index<<" "<<res<<" "<<temp<<" "<<time<<endl;
                res+=ps[index].value;
                index++;
            } else {
                break;

            }
        }
        printf("%d\n",res);
    }

    return EXIT_SUCCESS;
}                /* ----------  end of function main  ---------- */

猜你喜欢

转载自blog.csdn.net/zxtalentwolf/article/details/80152117
今日推荐