MM-Chess

There is an interesting game called MM-Chess. The size of the board is 1*N, every grid has a score (non-negative).  The first grid is the start and the Nth grid is the end. The game requires players to control the chess starting from the starting point to the end.

There're four types of cards in the game and the total number is M. Each type of card are labeled one integer number in [1,4]. After using a card with number x on it, the chess will move x steps forward. Each time, player will choose one unused card to move the chess forward, and each card can only be used once. In the game, the chess gains the score at the starting point automatically.  When the chess arrives at a new grid, it also gets the score on that point. The goal of the game is to get the most score.

Input:

                The first line contains two integers N (the size of board) and M (the number of the cards).

                The second line contains N integers, meaning the scores on the the board (the i-th integer corresponds to the score on the i-th grid).

                The third line contains M integers, meaning the numbers on the M cards.

                The sum of the number of M cards equals to N-1.

                You can assume that 1 <= N <= 350, 1 <= M <= 120, and that the number of cards are less than 40 for each kind.

Output:

                One integer. The most score that player can get.


Sample Input 1
4 2
1 2 1 2
1 2

Sample Output 1

5


Given two cards with number 1 and number 2 each, we have two choices: path one is 1 -> 2 -> 2, path two is 1 -> 1 -> 2. The maximum score is 5, which is the output.

Sample Input 2
5 3
1 2 1 2 1

1 2 1


Sample Output 2
6

Given three cards (one can move 2 steps, two can move 1 steps), we have three choices: path one is 1 -> 2 -> 1 -> 1, path two is 1 -> 2 -> 2 -> 1, path three is 1 -> 1 -> 2 -> 1. The maximum score is 6, which is the output.

18 7
1 4 5 4 5 1 2 8 3 10 1 2 3 4 6 9 1 3
3 2 1 4 1 3 3

42

because each type of card are labeled one integer number in [1,4]
dp[n] = max(dp[n-1], dp[n-2], dp[n-3], dp[n-4]) + scores[n];

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

void copyArr(int src[], int dst[])
{
        for (int i = 0; i < 4; ++i)
                dst[i] = src[i];
}
int search(int n, int scores[], int cards[])
{
        if (0 == n)
                return scores[0];
        int res = 0, tmp = 0;
        for (int i = 0; i < 4; ++i)
        {
                if (cards[i] > 0)
                {
                        int tcards[4] = { 0 };
                        copyArr(cards, tcards);
                        --tcards[i];
                        tmp = search(n-i-1, scores, tcards);
                        if (tmp + scores[n] > res)
                                res = tmp + scores[n];
                }
        }

        return res;
}


int main()
{
        int n, m, tmp;
        cin >> n >> m;
        int *scores = new int[n];
        for (int i = 0; i < n; ++i)
                cin >> scores[i];

        int cards[4] = { 0 };
        for (int i = 0; i < m; ++i)
        {
                cin >> tmp;
                ++cards[tmp-1];
        }
        int res = search(n-1, scores, cards);
        cout << res << endl;

        delete []scores;
        scores = NULL;

        return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值