当前位置:网站首页>【洛谷P1541】乌龟棋【DP】
【洛谷P1541】乌龟棋【DP】
2022-07-02 22:07:00 【Ayane.】
[NOIP2010 提高组] 乌龟棋
题目背景
小明过生日的时候,爸爸送给他一副乌龟棋当作礼物。
题目描述
乌龟棋的棋盘是一行 N N N个格子,每个格子上一个分数(非负整数)。棋盘第1格是唯一的起点,第 N N N格是终点,游戏要求玩家控制一个乌龟棋子从起点出发走到终点。
乌龟棋中 M M M张爬行卡片,分成4种不同的类型( M M M张卡片中不一定包含所有 4 4 4种类型的卡片,见样例),每种类型的卡片上分别标有 1 , 2 , 3 , 4 1,2,3,4 1,2,3,4四个数字之一,表示使用这种卡片后,乌龟棋子将向前爬行相应的格子数。游戏中,玩家每次需要从所有的爬行卡片中选择一张之前没有使用过的爬行卡片,控制乌龟棋子前进相应的格子数,每张卡片只能使用一次。
游戏中,乌龟棋子自动获得起点格子的分数,并且在后续的爬行中每到达一个格子,就得到该格子相应的分数。玩家最终游戏得分就是乌龟棋子从起点到终点过程中到过的所有格子的分数总和。
很明显,用不同的爬行卡片使用顺序会使得最终游戏的得分不同,小明想要找到一种卡片使用顺序使得最终游戏得分最多。
现在,告诉你棋盘上每个格子的分数和所有的爬行卡片,你能告诉小明,他最多能得到多少分吗?
输入格式
每行中两个数之间用一个空格隔开。
第 1 1 1行 2 2 2个正整数 N , M N,M N,M,分别表示棋盘格子数和爬行卡片数。
第 2 2 2行 N N N个非负整数, a 1 , a 2 , … , a N a_1,a_2,…,a_N a1,a2,…,aN,其中 a i a_i ai表示棋盘第 i i i个格子上的分数。
第 3 3 3行 M M M个整数, b 1 , b 2 , … , b M b_1,b_2,…,b_M b1,b2,…,bM,表示M张爬行卡片上的数字。
输入数据保证到达终点时刚好用光 M M M张爬行卡片。
输出格式
1 1 1个整数,表示小明最多能得到的分数。
样例 #1
样例输入 #1
9 5
6 10 14 2 8 8 18 5 17
1 3 1 2 1
样例输出 #1
73
提示
每个测试点 1 s 1s 1s
小明使用爬行卡片顺序为 1 , 1 , 3 , 1 , 2 1,1,3,1,2 1,1,3,1,2,得到的分数为 6 + 10 + 14 + 8 + 18 + 17 = 73 6+10+14+8+18+17=73 6+10+14+8+18+17=73。注意,由于起点是 1 1 1,所以自动获得第 1 1 1格的分数 6 6 6。
对于 30 % 30\% 30%的数据有 1 ≤ N ≤ 30 , 1 ≤ M ≤ 12 1≤N≤30,1≤M≤12 1≤N≤30,1≤M≤12。
对于 50 % 50\% 50%的数据有 1 ≤ N ≤ 120 , 1 ≤ M ≤ 50 1≤N≤120,1≤M≤50 1≤N≤120,1≤M≤50,且 4 4 4种爬行卡片,每种卡片的张数不会超过 20 20 20。
对于 100 % 100\% 100%的数据有 1 ≤ N ≤ 350 , 1 ≤ M ≤ 120 1≤N≤350,1≤M≤120 1≤N≤350,1≤M≤120,且 4 4 4种爬行卡片,每种卡片的张数不会超过 40 40 40; 0 ≤ a i ≤ 100 , 1 ≤ i ≤ N , 1 ≤ b i ≤ 4 , 1 ≤ i ≤ M 0≤a_i≤100,1≤i≤N,1≤b_i≤4,1≤i≤M 0≤ai≤100,1≤i≤N,1≤bi≤4,1≤i≤M。
l i n k link link
分析:
设 f i , j , k , l f_{i,j,k,l} fi,j,k,l表示一步卡牌用了 i i i次 两步用了 j j j次以此类推
因为每种最多只有 40 40 40张 所以 O ( n 4 ) O(n^4) O(n4)可做
那么直接转移
CODE:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define reg register
using namespace std;
typedef long long ll;
const int N=355,M=45;
int n,m,val[N],f[M][M][M][M],a,b,c,d;
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
for(int i=1,x;i<=m;i++)
{
scanf("%d",&x);
if(x==1) a++;
if(x==2) b++;
if(x==3) c++;
if(x==4) d++;
}
f[0][0][0][0]=val[1];
for(int i=0;i<=a;i++)
for(int j=0;j<=b;j++)
for(int k=0;k<=c;k++)
for(int l=0;l<=d;l++)
{
int step=i+j*2+k*3+l*4+1;
if(i>0) f[i][j][k][l]=max(f[i][j][k][l],f[i-1][j][k][l]+val[step]);
if(j>0) f[i][j][k][l]=max(f[i][j][k][l],f[i][j-1][k][l]+val[step]);
if(k>0) f[i][j][k][l]=max(f[i][j][k][l],f[i][j][k-1][l]+val[step]);
if(l>0) f[i][j][k][l]=max(f[i][j][k][l],f[i][j][k][l-1]+val[step]);
}
printf("%d",f[a][b][c][d]);
return 0;
}
边栏推荐
- NC24325 [USACO 2012 Mar S]Flowerpot
- Market Research - current situation and future development trend of herringbone gear Market
- Using rendertext() to output multiple lines of text with rendertext() in R shiny
- JS获取display为none的隐藏元素的宽度和高度的解决方案
- PHP微信抢红包的算法
- Perceptron model and Application
- 小鹏P7出事故,安全气囊未弹出,这正常吗?
- [shutter] shutter application theme (themedata | dynamic modification theme)
- [shutter] shutter gesture interaction (small ball following the movement of fingers)
- 送给即将工作的自己
猜你喜欢

phpcms实现订单直接支付宝支付功能

uniapp微信登录返显用户名和头像
![[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)](/img/f7/cb41d159e5c5ef3f4f1b9468d52ccc.jpg)
[shutter] shutter opens a third-party application (url|launcher plug-in search and installation | url| launcher plug-in official example | open browser | open a third-party application)

SimpleITK使用——4. 奇怪的问题

Simpleitk use - 4 Strange question

`${}`的用法

Simpleitk use - 3 Common operations
![[LeetCode] 数组中的第K个最大元素【215】](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[LeetCode] 数组中的第K个最大元素【215】

Hanoi Tower problem

kubernetes 使用主机名将 pod 分配在指定节点上
随机推荐
JS获取display为none的隐藏元素的宽度和高度的解决方案
Wait to solve the zombie process
Basic concepts of image and deep understanding of yuv/rgb
#include errors detected. Please update your includePath.
Market Research - current situation and future development trend of cell-based seafood market
SimpleITK使用——3. 常见操作
[LeetCode] 存在重复元素【217】
小鹏P7出事故,安全气囊未弹出,这正常吗?
Unity3d learning notes 4 - create mesh advanced interface
Simpleitk use - 3 Common operations
Share how to make professional hand drawn electronic maps
uniapp微信登录返显用户名和头像
[ODX studio edit PDX] -0.1- how to quickly view the differences in supported diagnostic information between variant variants (service, sub function...)
How can I use knockout's $parent/$root pseudovariables from inside a . computed() observable?
Dynamic memory allocation (malloc calloc realloc free)
How should programmers write logs
Market Research - current market situation and future development trend of total nutrition products
Oracle PL / SQL programming
Market Research - current situation and future development trend of environmental friendly fireworks Market
Market Research - current market situation and future development trend of aircraft audio control panel system