Play Game(게임 dp)
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;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
조밀한 게임을 Scratch로 만들어 보았다. 그 1.2021년 1월 7일에 스가 총리가 긴급 사태 선언을 발령했습니다. 역시 「밀」이 좋지 않은 일이라고, 재차 생각했으므로 넷에서 보인 명작 을 마음대로 흉내내 Scratch로 만들어 보았습니다. <완성 이미지> 링크...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.