当前位置:网站首页>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;
}
边栏推荐
- Apicloud visual development novice graphic tutorial
- Ranked first in China's SDN (software) market share for six consecutive years
- 在VR全景中如何添加聚合热点?内容模块如何添加?
- 618 great promotion is coming, mining bad reviews with AI and realizing emotional analysis of 100 million comments with zero code
- 面试中经常问到的几个问题,快来看看能答对几道吧
- Beijing Metro ticketing system
- Basic improvement - tree DP supplement
- Go语学习笔记 - 跨域配置、全局异常捕获 | Web框架Gin(四)
- frp reverse proxy
- 【C语言】还搞不明白结构体吗?不妨来看看这篇文章,带你初步了解结构体
猜你喜欢

MATLAB 根据任意角度、取样点数(分辨率)、位置、大小画椭圆代码

【C语言进阶】数据的存储【下篇】【万字总结】
![[C language] accidentally write a bug? Mortals teach you how to write good code [explain debugging skills in vs]](/img/34/6f254e027e5941bb2c3462b665c3ba.png)
[C language] accidentally write a bug? Mortals teach you how to write good code [explain debugging skills in vs]

2022.05.24 (lc_674_longest continuous increasing sequence)

Computer: successfully teach you how to use one trick to retrieve the previous password (the password once saved but currently displayed as ******)

SAR回波信号基本模型与性质

高考开启,VR全景可以这样看考点

高考后选择哪所学校?VR全景校园全方位展示

Sliding window maximum value problem

100003 words, take you to decrypt the system architecture under the double 11 and 618 e-commerce promotion scenarios
随机推荐
[advanced C language] data storage [part I] [ten thousand words summary]
How to add aggregation hotspots in VR panorama? How to add a content module?
大厂测试员年薪30万到月薪8K,吐槽工资太低,反被网友群嘲?
Logback exclude specified package / class / method log output
在VR全景中如何添加聚合热点?内容模块如何添加?
Mongodb 唯一索引
Harbor image pull voucher configuration
掌握高性能计算前,我们先了解一下它的历史
Code solution of simplex method (including super detailed code notes and the whole flow chart)
详细解读TPH-YOLOv5 | 让目标检测任务中的小目标无处遁形
一文帶你了解J.U.C的FutureTask、Fork/Join框架和BlockingQueue
Ding Dong grabs vegetables - monitoring and pushing tools for delivery period
Developers changing the world - Yao Guang teenagers playing Tetris
基于改进SEIR模型分析上海疫情
How to add independent hotspots in VR panoramic works?
Mongodb index unique
[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure
Key and encryption mechanism in financial industry
Design and development of hospital reservation registration platform based on JSP Zip (thesis + project source code)
2022 software test interview strategy for the strongest version of fresh students to help you get directly to the big factory