Luo Gu -1541 chess turtle

Title Description
turtle chess board is a line of N cells, each a fraction of the grid (nonnegative integer). The first checkerboard grid is only a starting point, the N-th frame is the end, the game requires players to control a turtle piece from the starting point to an end.
M Zhang chess turtle crawling cards divided into four different types (M cards do not necessarily contain all four types of cards, see examples), were labeled on each type of card 1,2,3,4 one of the four figures, showing the use of such cards, turtle piece number corresponding cells crawling forward. The game, each player needs to select a not previously used the card from crawling crawling all cards, the control tortoise pawn forward the corresponding grid number, each card can only be used once.
Game, turtle pieces automatically starting lattice points, and each lattice reach a subsequent crawling, to give the corresponding points of the grid. The final score of the game players score the sum of all the grid is the tortoise piece from start to finish the process to before.
Obviously, the use of a different order of crawling card will make the final score of the game different, Xiao Ming wants to find a card using the order making the final score of the game the most.
Now, you tell each grid scores and crawling all cards on the board, can you tell Xiaoming, how many points he can get up to it?
Input and output format
input format:
There is a space between the two numbers in each row.
Line 12 positive integers N, M, respectively, and the number of checkerboard creep card number.
Row 2 N non-negative integer, a1, a2, ..., aN , which represents a fraction of the board on a lattice of iii.
M row. 3 integers, b1, b2, ..., bM , Zhang M represents crawling on the card number.
Input data to ensure just run out of M Zhang crawling card at the finish.
Output format:
an integer that represents the fraction obtained up to Bob.

Sample Input Output
Input Sample # 1:
. 9. 5
. 6. 8. 8 18 is 10 14. 5. 17 2
1 1 2 1. 3

Output Sample # 1:
73 is

DESCRIPTION
each test point 1s
to 100% of the data have 1≤N≤350,1≤M≤120, reptiles and 4 or cards, the number of each card does not exceed 40; 0≤ai≤100,1≤i ≤N, 1≤bi≤4,1≤i≤M

Explanation: see the data range, will know very simple memory search, provided dp [i] [a] [b] [c] represents now i, 4 have a, 3 there b, 2 c, there is a maximum value, search directly on OK

#include<iostream>
#define N 355
#define M 41
using namespace std;
int dp[N][M][M][M]={0};
int H[5]={0};
int A[N]={0};
int n=0,m=0;
int dfs(int x,int a,int b,int c){
    int sum=0;
    if(dp[x][a][b][c]) return dp[x][a][b][c];
    if(a) sum=max(sum,dfs(x+4,a-1,b,c));
    if(b) sum=max(sum,dfs(x+3,a,b-1,c));
    if(c) sum=max(sum,dfs(x+2,a,b,c-1));
    if(n-x-4*a-3*b-2*c) sum=max(sum,dfs(x+1,a,b,c));
    return dp[x][a][b][c]=sum+A[x];
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>A[i];
    for(int i=1,temp;i<=m;i++){
        cin>>temp;H[temp]++;
    }
    dp[n][0][0][0]=A[n];
    cout<<dfs(1,H[4],H[3],H[2])<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/mkopvec/article/details/92019520