Play Game(게임 dp)

2247 단어 게임구간 dp
제목 링크:https://cn.vjudge.net/problem/HDU-4597 
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 a i (1≤a i≤10000). The third line contains N integer b i (1≤bi≤10000).
Output
For each case, output an integer, indicating the most score Alice can get.
Sample Input
2 
 
1 
23 
53 
 
3 
10 100 20 
2 4 3 

Sample Output
53 
105 

제목: 두 개의 카드 서열을 정하고 한 장의 카드마다 일정한 점수를 매긴다. Alice와 Bob은 두 개의 서열에서 카드를 번갈아 들고 매번 서열의 머리나 끝에서만 카드를 가져갈 수 있다.앨리스가 먼저 뽑으면 가장 많이 받을 수 있는 카드 점수와 많게는 얼마냐고 물었다.
pp[i][j][a][b]는 첫 번째 카드 시퀀스 i-j, 두 번째 카드 시퀀스 a-b에서 선취자가 가장 많이 얻을 수 있는 점수를 나타낸다.
매번 최대 4가지 취법이 있는데 어느 것이든 상대방의 선취 최우선 선택이 되었다. 현재의sum로 상대방의 최우선 선택을 줄이고 네 가지 중 가장 큰 것을 취하는 것이 자신의 최우선 선택이다.
기억화 검색
#include
#include
#include
using namespace std;
int num1[22],num2[22];
int sum1[22],sum2[22];
int dp[22][22][22][22];
int dfs(int i,int j,int a,int b){
	int sum=sum1[j]-sum1[i-1]+sum2[b]-sum2[a-1];
	if(dp[i][j][a][b]!=0) return dp[i][j][a][b];
	int ans=0;
	if(i<=j){
		ans=max(ans,sum-dfs(i+1,j,a,b)); 
		ans=max(ans,sum-dfs(i,j-1,a,b));
	}
	if(a<=b){
		ans=max(ans,sum-dfs(i,j,a+1,b));
		ans=max(ans,sum-dfs(i,j,a,b-1));
	}
	return dp[i][j][a][b]=ans;	
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		memset(dp,0,sizeof(dp));
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d",&num1[i]);
			sum1[i]=sum1[i-1]+num1[i];
		}
		for(int i=1;i<=n;i++){
			scanf("%d",&num2[i]);
			sum2[i]=sum2[i-1]+num2[i];
		}
		printf("%d
",dfs(1,n,1,n)); } return 0; }

좋은 웹페이지 즐겨찾기