Turtle Chess (Dynamic Programming)

On Xiao Ming's birthday, his father gave him a tortoise chess set as a gift.

The board of Turtle Chess has only one row, and the row has N squares, and each square has a score (non-negative integer).

The first square of the board is the only starting point, and the Nth square is the ending point. The game requires the player to control a tortoise piece from the starting point to the ending point.

There are M crawling cards in Turtle Chess, which are divided into 4 different types (M cards may not contain all 4 types of cards). Each type of card is marked with four numbers 1, 2, 3, and 4 respectively. One means that after using this card, the turtle chess piece will crawl forward by the corresponding number of squares.

In the game, the player needs to select a crawling card that has not been used before from all the crawling cards, and control the turtle chess piece to advance the corresponding number of squares. Each card can only be used once.

In the game, the tortoise pieces automatically get the score of the starting grid, and each time they reach a grid in the subsequent crawl, they get the corresponding score of the grid.

The player's final game score is the sum of the scores of all the grids that the tortoise piece has reached from the starting point to the ending point.

Obviously, using different crawling card use order will make the final game score different. Xiao Ming wants to find a card use order that makes the final game score the most.

Now, tell you the score of each grid on the board and all the crawling cards. Can you tell Xiao Ming how many points he can get?

Input format

A space separates the two numbers in each line of the input file.

The two positive integers N and M in the first row represent the number of checkerboard grids and the number of crawling cards, respectively.

The second row is N non-negative integers, a1, a2,..., aN, where ai represents the score on the i-th grid of the chessboard.

In the third row, M integers, b1, b2,..., bM, represent the numbers on M crawling cards.

Enter the data to ensure that M crawling cards are used up when the destination is reached.

Output format

The output is only 1 line and contains 1 integer, which represents the maximum score that Xiao Ming can get.

data range

1≤N≤350,
1≤M≤120,
0≤ai≤100,
1≤bi≤4, the number of
each kind of crawling card will not exceed 40.

Input sample:

9 5
6 10 14 2 8 8 18 5 17
1 3 1 2 1

Sample output:

73

Learning high math is silly QAQ ( it’s silly, don’t blame high math )

The status is represented by the number of current cards used, and the status transition is performed according to the last used card

When opening the array, press 121 to open the memory and it will explode. The question says that the number of each card will not exceed 40, so it can be opened to 41.

41^4=2,825,761, the order of magnitude is 1e6, which is acceptable

#include <iostream>
#include <algorithm>
using namespace std;
const int N = 41;

int a[355], b[5];
int dp[N][N][N][N];

int main(void)
{
    
    
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
    
    
        cin >> a[i];
    }
    int t;
    for (int i = 1; i <= m; i++) {
    
    
        cin >> t;
        b[t]++;
    }
    dp[0][0][0][0] = a[1];
    for (int A = 0; A <= b[1]; A++) {
    
    
        for (int B = 0; B <= b[2]; B++) {
    
    
            for (int C = 0; C <= b[3]; C++) {
    
    
                for (int D = 0; D <= b[4]; D++) {
    
    
                    int sum = A * 1 + B * 2 + C * 3 + D * 4 + 1; // 不要忘了加1
                    int &x = dp[A][B][C][D];
                    if (A) x = max(x, dp[A - 1][B][C][D] + a[sum]);
                    if (B) x = max(x, dp[A][B - 1][C][D] + a[sum]);
                    if (C) x = max(x, dp[A][B][C - 1][D] + a[sum]);
                    if (D) x = max(x, dp[A][B][C][D - 1] + a[sum]);
                }
            }
        }
    }
    cout << dp[b[1]][b[2]][b[3]][b[4]] << endl;
    
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43772166/article/details/113775543