HDU 4597 play game (memory-based search, Deep Search)

Source: Internet
Author: User

Question

 

 

 

// The legendary memory-based search. Well, it's just a deep search.
// Do more questions. This solution is searched and understandable.

// Question: give two groups of cards, which can only be obtained from the top and bottom, and then take turns for the two. They all follow their best strategy,
// Ask the first person's score for a given number.
// Solution thinking: memory-based search, which is very watery when the status comes out. DP [fl] [fr] [Sl] [SR] [flag],
// Indicates that FL is obtained from the top of the first heap and FR is obtained from the bottom. In the same SL model, Sr is the second heap and flag is the first few players.
// If it is the first person, DP should be as large as possible. If it is the second person, it must be as small as possible.

 

Http://www.2cto.com/kf/201404/295904.html

#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;        i f (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;}
Baidu's code -- reprinted

 

 

// Why does the mark start from 0 and start from 1? // If n is 0 or 1, if the subscript starts from 0, the problem may occur. Do not believe it. Try # include <stdio. h> # include <string. h ># include <algorithm> using namespace STD; int A [25], B [25], DP [25] [25] [25] [25] [2]; int DFS (INT A1, int A2, int B1, int B2, int flag) {If (A1> A2 & B1> B2) return DP [a1] [a2] [B1] [B2] [flag] = 0; If (DP [a1] [a2] [B1] [B2] [flag]! =-1) return DP [a1] [a2] [B1] [B2] [flag]; If (FLAG) {DP [a1] [a2] [B1] [B2] [flag] = 0; If (A1 <= a2) {DP [a1] [a2] [B1] [B2] [flag] = max (DP [a1] [a2] [B1] [B2] [flag], DFS (A1 + 1, A2, B1, B2, 1-flag) + A [a1]); DP [a1] [a2] [B1] [B2] [flag] = max (DP [a1] [a2] [B1] [B2] [flag], DFS (A1, a2-1, B1, B2, 1-flag) + A [a2]);} If (b1 <= b2) {DP [a1] [a2] [B1] [B2] [flag] = max (DP [a1] [a2] [B1] [B2] [flag], DFS (A1, A2, B1 + 1, B2, 1-flag) + B [B1]); DP [a1] [a2] [B1] [B2] [flag] = max (DP [a1] [a2] [B1] [B2] [flag], DFS (A1, a2, B1, b2-1, 1-flag) + B [B2]);} else {DP [a1] [a2] [B1] [B2] [flag] = 2100000000; if (A1 <= a2) {DP [a1] [a2] [B1] [B2] [flag] = min (DP [a1] [a2] [B1] [B2] [flag], DFS (A1 + 1, A2, B1, B2, 1-flag )); DP [a1] [a2] [B1] [B2] [flag] = min (DP [a1] [a2] [B1] [B2] [flag], DFS (A1, a2-1, B1, B2, 1-flag);} If (b1 <= b2) {DP [a1] [a2] [B1] [B2] [flag] = min (DP [a1] [a2] [B1] [B2] [flag], DFS (A1, A2, B1 + 1, B2, 1-flag )); DP [a1] [a2] [B1] [B2] [flag] = min (DP [a1] [a2] [B1] [B2] [flag], DFS (A1, a2, B1, b2-1, 1-flag);} return DP [a1] [a2] [B1] [B2] [flag];} int main () {int T, I, n, ans; scanf ("% d", & T); While (t --) {scanf ("% d", & N ); memset (DP,-1, sizeof (DP); for (I = 1; I <= N; I ++) // The subscript cannot start from 0, be sure to start scanf ("% d", & A [I]); for (I = 1; I <= N; I ++) from 1) // same as scanf ("% d", & B [I]); ans = DFS (1, n, 1, n, 1 ); printf ("% d \ n", ANS);} return 0 ;}
Write your own

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.