本文主要是介绍hdu 4597 Play Game(记忆化搜索),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目链接:hdu 4597 Play Game
题目大意:给出两堆牌,只能从最上和最下取,然后两个人轮流取,都按照自己最优的策略,问说第一个人对多的分值。
解题思路:记忆化搜索,状态出来就非常水,dp[fl][fr][sl][sr][flag],表示第一堆牌上边取到fl,下面取到fr,同样sl,sr为第二堆牌,flag为第几个人在取。如果是第一个人,dp既要尽量大,如果是第二个人,那么肯定尽量小。
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;const int N = 25;
const int INF = 0x3f3f3f3f;
int n, f[N], s[N], dp[N][N][N][N][2];void init () {memset(dp, -1, sizeof(dp));scanf("%d", &n);for (int i = 1; i <= n; i++)scanf("%d", &f[i]);for (int i = 1; i <= n; i++)scanf("%d", &s[i]);
}int solve (int fl, int fr, int sl, int sr, int flag) {int& ans = dp[fl][fr][sl][sr][flag];if (fl > fr && sl > sr)return ans = 0;if (ans != -1)return ans;if (flag) {ans = 0;if (fl <= fr) {ans = max(ans, solve(fl+1, fr, sl, sr, 1-flag) + f[fl]);ans = max(ans, solve(fl, fr-1, sl, sr, 1-flag) + f[fr]);}if (sl <= sr) {ans = max(ans, solve(fl, fr, sl+1, sr, 1-flag) + s[sl]);ans = max(ans, solve(fl, fr, sl, sr-1, 1-flag) + s[sr]);}} else {ans = INF;if (fl <= fr) {ans = min(ans, solve(fl+1, fr, sl, sr, 1-flag));ans = min(ans, solve(fl, fr-1, sl, sr, 1-flag));}if (sl <= sr) {ans = min(ans, solve(fl, fr, sl+1, sr, 1-flag));ans = min(ans, solve(fl, fr, sl, sr-1, 1-flag));}}return ans;
}int main () {int cas;scanf("%d", &cas);while (cas--) {init ();printf("%d\n", solve(1, n, 1, n, 1));}return 0;
}
这篇关于hdu 4597 Play Game(记忆化搜索)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!