当前位置:网站首页>Trilogy to solve the problem of playing chess first and then
Trilogy to solve the problem of playing chess first and then
2022-06-10 19:51:00 【Starlight technician】
The trilogy solves the problem of playing chess first and then
1. subject
Two people play chess , Given an array Arr={1,4,9,2,10,7}; Two people can only take elements from both ends of the array , Get the score of the winner ;
First, make it clear , The best solution of this game is to win first , But if two people play games in real life, it's not necessarily
2. Violent recursive solution
Ideas
Want to get the score of the winner , The winner must be one of the two , Either a wins or B wins ; When a wins, he may be the first or the second , Then it can be regarded as a pioneer ( Second hindhand ) The score obtained is the same as that of the second hand ( B first ) The maximum of the scores obtained ;
explain : function f(Arr,L,R) Represents in the array [L,R] From the beginning to the end of the game , A first hand Can also The maximum score obtained ; function s(Arr,L,R) Represents in the array [L,R] From the beginning to the end of the game , Posterior nail hand Can also The maximum score obtained ;code
int f(vector<int>& Arr, int L, int R)
{
if (L == R)
return Arr[L];
else
return max(Arr[L] + s(Arr, L + 1, R,dp), Arr[R] + s(Arr, L, R - 1));
}
int s(vector<int>& Arr, int L, int R)
{
if (L == R)
return 0;
else
return max(f(Arr, L + 1, R, dp), f(Arr, L, R - 1, dp));
}

Solution 1 has repeated calculation , Suppose the state array , Record the status reached
- code
#include<iostream>
#include<vector>
using namespace std;
class QQ
{
public:
int f(vector<int>& Arr, int L, int R, vector<vector<int>>& dp)
{
if (dp[L][R] != -1)
return dp[L][R];
if (L == R)
dp[L][R] = Arr[L];
else
dp[L][R] = max(Arr[L] + s(Arr, L + 1, R,dp), Arr[R] + s(Arr, L, R - 1,dp));
return dp[L][R];
}
int s(vector<int>& Arr, int L, int R, vector<vector<int>>& dp)
{
if (dp[L][R] != -1)
return dp[L][R];
if (L == R)
dp[L][R] = 0;
else
dp[L][R] = max(f(Arr, L + 1, R, dp), f(Arr, L, R - 1, dp));
return dp[L][R];
}
};
int main()
{
QQ qq;
vector<int> Arr = {
3,100,2,50 };
int L = 0;
int R = Arr.size();
vector<vector<int>> dp(R, vector<int>(R,-1));
int res = qq.s(Arr, L, R - 1,dp);
cout << res << endl;
return 0;
}
4. Strict table structure solution
Define two tables f and s, First, determine the position in the table where the value can be directly obtained , Then determine the location dependency , Determine the traversal order ;
- code
void code2(vector<int>& Arr, vector<vector<int>>& f, vector<vector<int>>& s)
{
for (int i = 0; i < Arr.size(); i++)
f[i][i] = Arr[i];
int row = 0;
int col = 1;
while (col < Arr.size())
{
int m = row;
int n = col;
while (n < Arr.size())
{
f[m][n] = max(Arr[m] + s[m + 1][n], Arr[n] + s[m][n - 1]);
s[m][n] = max(f[m][n - 1], f[m + 1][n]);
m++;
n++;
}
col++;
}
}
int main()
{
vector<int> Arr = {
3,100,70,50 };
int R = Arr.size();
vector<vector<int>> f(R, vector<int>(R, 0));
vector<vector<int>> s(R, vector<int>(R, 0));
code2(Arr, f, s);
int res = max(f[0][R-1], s[0][R-1]);
cout << res << endl;
return 0;
}
边栏推荐
- Computer: successfully teach you how to use one trick to retrieve the previous password (the password once saved but currently displayed as ******)
- How do big factories write data analysis reports?
- Source code analysis of Tencent libco collaborative process open source library (II) -- persimmon starts from the soft pinch, and the sample code officially begins to explore the source code
- Micronet practice: image classification using micronet
- [C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure
- Cet article vous donne un aperçu de la tâche future de j.u.c, du cadre Fork / join et de la file d'attente de blocage
- Sliding window maximum value problem
- 2022.05.29 (lc_6078_rearranges characters to form target string)
- [advanced C language] advanced pointer [Part 1]
- 恭喜 | 医学院那洁课题组通过多组学分析揭示JUNB在体外分化人造血祖细胞过程中的功能
猜你喜欢

2022最强版应届生软件测试面试攻略,助你直通大厂

2022.05.27 (lc_647_palindrome substring)

2022 software test interview strategy for the strongest version of fresh students to help you get directly to the big factory
![[advanced C language] advanced pointer [Part 1]](/img/a7/7a6f5286307d80b553c11582cf1827.png)
[advanced C language] advanced pointer [Part 1]

2022.05.28 (lc_516_longest palindrome subsequence)

【C语言进阶】数据的存储【下篇】【万字总结】

618 great promotion is coming, mining bad reviews with AI and realizing emotional analysis of 100 million comments with zero code

【C语言进阶】指针的进阶【上篇】

今年高考期间各考点秩序井然,未发生影响安全的敏感案事件

【 Web 】 page d'accueil personnelle 】 Programme d'études 】 albums de photos 】 babillard d'information 】
随机推荐
Basic improvement - tree DP supplement
一文带你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
[C language] have you mastered these classic questions? Learn these questions in one article
高考开启,VR全景可以这样看考点
MySql的MyISAM引擎切换InnoDB时报错Row size too large (&gt; 8126)解决
One article explains in detail the exploration and practice of eventmesh landing on Huawei cloud
Basic model and properties of SAR echo signal
Morris traversal of binary tree
Nature Biotechnol | 李家洋/余泓团队利用平铺删除策略打破性状连锁,突破水稻产量瓶颈
【 Web 】 page d'accueil personnelle 】 Programme d'études 】 albums de photos 】 babillard d'information 】
It is forbidden to throw away rotten software. A guide for software test engineers to advance from elementary level to advanced level will help you promote all the way
禁止摆烂,软件测试工程师从初级到高级进阶指南,助你一路晋升
大学生毕业季找房,VR全景看房帮你线上筛选
Monotonic stack structure
马斯克称自己不喜欢做CEO,更想做技术和设计;吴恩达的《机器学习》课程即将关闭注册|极客头条
ESP8266 系统环境搭建
Mixin -- mixed
Computer: successfully teach you how to use one trick to retrieve the previous password (the password once saved but currently displayed as ******)
Tidb - quick start, cluster setup
618大促将至,用AI挖掘差评,零代码实现亿级评论观点情感分析