当前位置:网站首页>【洛谷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;
}
边栏推荐
- #include errors detected. Please update your includePath.
- Share how to make professional hand drawn electronic maps
- New feature of go1.18: introduce new netip Network Library
- Market Research - current situation and future development trend of preclinical medical device testing service market
- Pointer array parameter passing, pointer parameter passing
- 服务器响应状态码
- 'when to use const char * and when to use const char []' - when to use const char * and when to use const char []
- Market Research - current market situation and future development trend of intravenous injection (IV) bottles
- 杰理之充电拔出,无法触摸开机【篇】
- [QT] QT multithreading development - four methods to realize multithreading design
猜你喜欢
[foreign journal] sleep and weight loss
#include errors detected. Please update your includePath.
[LeetCode] 反转字符串中的单词 III【557】
Simpleitk use - 4 Strange question
Additional: [login information storage] and [login status verification]; (including: summarizing all the contents of [login information storage] and [login status verification] so far;)
uniapp微信登录返显用户名和头像
Basic concepts of image and deep understanding of yuv/rgb
[001] [arm-cortex-m3/4] internal register
Socket套接字C/S端流程
小鹏P7出事故,安全气囊未弹出,这正常吗?
随机推荐
[foreign journal] sleep and weight loss
Market Research - current market situation and future development trend of high tibial osteotomy plate
Solve the error of changing the selected file when uploading excel file. Net:: err_ UPLOAD_ FILE_ CHANGED
Pointer and string
Market Research - current market situation and future development trend of aircraft audio control panel system
杰理之修改不需要长按开机功能【篇】
[LeetCode] 存在重复元素【217】
#include errors detected. Please update your includePath.
Jerry's modification does not require long press the boot function [chapter]
Market Research - current market situation and future development trend of aircraft wireless intercom system
Market Research - current situation and future development trend of herringbone gear Market
[autosar-dcm] - 4.3-how UDS $22 and $2e services read and write NVM data
Market Research - current situation and future development trend of cell-based seafood market
New feature of go1.18: introduce new netip Network Library
Struct, bit segment, enumeration, union
uniapp微信登录返显用户名和头像
Meibeer company is called "Manhattan Project", and its product name is related to the atomic bomb, which has caused dissatisfaction among Japanese netizens
Socket socket c/s end process
phpcms实现订单直接支付宝支付功能
杰理之快速触摸不响应问题【篇】