采花生

原博客:https://blog.csdn.net/maofenghua/article/details/78036981

采花生

时间限制 1000 ms  内存限制 16384 KB  代码长度限制 100 KB  判断程序 Standard  (来自 小小)

题目描述

鲁宾逊先生有一只宠物猴,名叫多多。这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!——熊字”。

鲁宾逊先生和多多都很开心,因为花生正是他们的最爱。在告示牌背后,路边真的有一块花生田,花生植株整齐地排列成矩形网格。有经验的多多一眼就能看出,每棵花生植株下的花生有多少。为了训练多多的算术,鲁宾逊先生说:“你先找出花生最多的植株,去采摘它的花生;然后再找出剩下的植株里花生最多的,去采摘它的花生;依此类推,不过你一定要在我限定的时间内回到路边。”

我们假定多多在每个单位时间内,可以做下列四件事情中的一件:

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(没有花生),其他所有植株下花生的数目都不相同。


输出描述:

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

输入例子:

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

输出例子:

37



import java.util.Scanner;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
public class Main
{
	public static void main(String[] args) 
	{
		class Node
		{
			int x, y , count;
			public Node(int x,int y, int count)
			{
				this.x = x; this.y = y ; this.count = count;
			}
		}
		Comparator<Node> myCompartor = new Comparator<Node>()
		{
			public int compare(Node o1, Node o2)
			{
				if(o1==null||o2==null)
					return -1;
				int count1 = o1.count;
				int count2 = o2.count;
				if(count1<count2)return 1;
				else if(count1==count2) return 0;
				else return -1;
			}
		};
		Scanner cin =  new Scanner(System.in);
		while(cin.hasNext())
		{
			int m = cin.nextInt();
			int n = cin.nextInt();
			int k = cin.nextInt();
			List<Node> nodeList = new LinkedList<Node>();
			for(int i = 1;i<=m;i++)
			{
				for(int j = 1;j<=n;j++)
				{
					int count = cin.nextInt();
					Node node = new Node(j,i,count);
					nodeList.add(node);
				}
			}
			Collections.sort(nodeList,myCompartor);
			int total = 0;
			int steps = 0;
			int size = nodeList.size();
			for(int i = 0;i<size;i++)
			{
				int tempTotal = total;
				total = total + nodeList.get(i).count;
				if(i==0)
					steps = steps + 2*nodeList.get(i).y+1;
				else 
				{
					steps = steps + Math.abs((nodeList.get(i).y- nodeList.get(i-1).y))+
							Math.abs((nodeList.get(i).x - nodeList.get(i-1).x))+nodeList.get(i).y+1;
				}
				if(steps>k)
				{
					total = tempTotal ;
					break;
				}
				else 
					steps = steps - nodeList.get(i).y;
			}
			System.out.println(total);
		}
	}
	
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值