Play Game
Problem Description
Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from either pile, and the score of the card will be added to his total score. Alice and Bob are both clever enough, and will pick up cards to get as many scores as possible. Do you know how many scores can Alice get if he picks up first?
Input
The first line contains an integer T (T≤100), indicating the number of cases.
Each case contains 3 lines. The first line is the N (N≤20). The second line contains N integer ai (1≤ai≤10000). The third line contains N integer bi (1≤bi≤10000).
Output
For each case, output an integer, indicating the most score Alice can get.
题目意思:
两堆卡片,每堆N张卡片,两堆的N张卡片对于n个值ai,bi,每次取卡片只能从堆顶或堆底选择,Alice和Bob轮流选取卡片,并获得卡片面值。
求Alice先手,取卡片能够获得的最大值。
解题思路:
记忆化搜索,其实说白了就是暴搜+动态规划,每次搜索过程中记录一些状态的值,下次搜索过程中如果再次经历这个状态,则直接调用,减少重复搜索量。
s1代表第一堆卡片堆顶位置,e1代表第一堆卡片堆底位置,s2,e2对第二堆同理...
dfs搜索的是Bob取卡片的值,所以我们要取最小。
dp[MAX_N][MAX_N][MAX_N][MAX_N]记录的是每次搜索Alice取卡片的最大值。
AC Code:
#include
#include
#include
#include
#include
#include
#include
#include