POJ 1928 The Peanuts (排序,水题)

41 篇文章 0 订阅
9 篇文章 0 订阅
这是一道关于路径规划和最大收获的算法题。题目要求在限定时间内,通过排序花生数量最多的点,计算猴子最多能采摘的花生数。解决方案包括对花生位置进行排序,然后模拟猴子的移动和采摘过程,确保能在规定时间内返回。
摘要由CSDN通过智能技术生成

题目意思:
在一片菜地里,种了花生,有个人戴了只猴子从路边走过,人希望猴子依次从花生最多的点采花生,但必须在规定时间内赶回主人身边,问你最多可以采摘到多少花生。每走动一格,花费一个单位的时间。同时, 在某点采花生,也花费一个单位的时间。

解答:
1、花生排序之后(按 花生的数量,x坐标,y坐标), 每取一点的花生,要计算回到路上的时间是否足够
2、简单的模拟,水题

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
const int MaxL = 2510;

struct Peanut
{
	int x, y;
	int cnt;
}peanuts[MaxL];

bool cmp(const Peanut& a, const Peanut& b)
{
	if(a.cnt != b.cnt)
	{
		return a.cnt > b.cnt;
	}else if(a.x != b.x){
		return a.x < b.x;
	}else{
		return a.y < b.y;
	}
}

int p_num;	// 有 peanut 的点的总数
int t, m, n, k;

void solve()
{
	sort(peanuts, peanuts + p_num, cmp);
	int ans = 0, time;
	if(peanuts[0].x * 2 + 1 > k)	//花费的总时间大于 k
	{
		printf("0\n");
		return;
	}
	ans += peanuts[0].cnt;
	time = 1 + peanuts[0].x;
	for(int i = 1; i < p_num; ++i)
	{
		if(time + abs(peanuts[i].x - peanuts[i - 1].x) + abs(peanuts[i].y - peanuts[i - 1].y) + 1
				+ peanuts[i].x <= k)
		{
			ans += peanuts[i].cnt;
			time += abs(peanuts[i].x - peanuts[i - 1].x) + abs(peanuts[i].y - peanuts[i - 1].y) + 1;
		}else{
			break;
		}
	}
	printf("%d\n", ans);
}

int main()
{
	scanf("%d", &t);
	int a;
	while(t--)
	{
		p_num = 0;
		scanf("%d%d%d", &m, &n, &k);
		for(int i = 1; i <= m; ++i)
		{
			for(int j = 1; j <= n; ++j)
			{
				scanf("%d", &a);
				if(a)
				{
					peanuts[p_num].x = i, peanuts[p_num].y = j;
					peanuts[p_num++].cnt = a;
				}
			}
		}
		solve();
	}
	return 0;
}

/*
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
*/

/*
37
28
*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值