贪心/poj 1042 Gone Fishing

本文提供了一个具体的算法题目解析,通过C++实现了一种模拟捕鱼过程的算法,旨在最大化捕获鱼的数量。该算法考虑了时间限制、不同位置的鱼数量及捕鱼深度等因素,通过动态规划的方式得出最优解。
 1 #include<cstdio>
 2 #include<cstring>
 3 using namespace std;
 4 struct node
 5 {
 6     int d;
 7     int fish;
 8     int ans;
 9 };
10 node a[30],b[30];
11 int t[30];
12 int n,h,sum;
13 int main()
14 {
15     scanf("%d",&n);
16     while (n!=0)
17     {
18         memset(a,0,sizeof(a));
19         memset(t,0,sizeof(t));
20         scanf("%d",&h);
21         h=h*12;
22         for (int i=1;i<=n;i++) scanf("%d",&a[i].fish);
23         for (int i=1;i<=n;i++) scanf("%d",&a[i].d);
24         for (int i=1;i<n;i++)scanf("%d",&t[i]);
25         sum=-1;
26         for (int stop=1;stop<=n;stop++)
27         {
28             memset(b,0,sizeof(b));
29             int time=h;
30             int answer=0;
31             for (int i=1;i<=stop;i++)
32             {
33                 b[i].fish=a[i].fish;
34             }
35             for (int i=1;i<stop;i++) time-=t[i];
36 
37             for (int rest=time;rest>0;rest--)
38             {
39 
40                 int fishmax=1;
41                 for (int i=2;i<=stop;i++)
42                     if (b[fishmax].fish<b[i].fish) fishmax=i;
43 
44                 answer+=b[fishmax].fish;
45                 b[fishmax].ans++;
46                 b[fishmax].fish-=a[fishmax].d;
47                 if (b[fishmax].fish<0) b[fishmax].fish=0;
48             }
49             if (answer>sum)
50             {
51                 sum=answer;
52                 for (int i=1;i<=n;i++) a[i].ans=b[i].ans;
53             }
54         }
55         for (int i=1;i<n;i++) printf("%d, ",a[i].ans*5);
56         printf("%d\n",a[n].ans*5);
57         printf("Number of fish expected: %d\n\n",sum);
58         scanf("%d",&n);
59     }
60     return 0;
61 }

 

转载于:https://www.cnblogs.com/NicoleLam/p/4158689.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值