当前位置:网站首页>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;
}
边栏推荐
- 【C语言进阶】指针的进阶【上篇】
- 【C语言进阶】数据的存储【上篇】【万字总结】
- Explain the interview questions by holding chestnuts (interview, review and study)
- [01] every high-quality author deserves to be seen. Let's take a look at this week's high-quality content!
- Basic model and properties of SAR echo signal
- 【C语言】还搞不明白结构体吗?不妨来看看这篇文章,带你初步了解结构体
- Basic improvement - tree DP supplement
- Computer:成功教你如何使用一招—就能找回以前的密码(曾经保存的密码但当前显示为******号的密码)
- 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
- SAR回波信号基本模型与性质
猜你喜欢

首批!青藤通过信通院CWPP能力评估检验

【 Web 】 page d'accueil personnelle 】 Programme d'études 】 albums de photos 】 babillard d'information 】

Ranked first in China's SDN (software) market share for six consecutive years

Developers changing the world - Yao Guang teenagers playing Tetris

【C语言】一不小心写出bug?凡人教你如何写出好代码【详解vs中调试技巧】

SAR image focusing quality evaluation plug-in
![[web] personal homepage web homework](/img/16/90b7b559e43e7cd6d5e32865eb0c00.png)
[web] personal homepage web homework "timetable", "photo album" and "message board"

Domain Driven Design (VI) - Architecture Design

How to add aggregation hotspots in VR panorama? How to add a content module?

改变世界的开发者丨玩转“俄罗斯方块”的瑶光少年
随机推荐
2022.05.29 (lc_6078_rearranges characters to form target string)
SAR回波信号基本模型与性质
腾讯Libco协程开源库 源码分析(二)---- 柿子先从软的捏 入手示例代码 正式开始探究源码
One article explains in detail the exploration and practice of eventmesh landing on Huawei cloud
2022最强版应届生软件测试面试攻略,助你直通大厂
[advanced C language] data storage [part I] [ten thousand words summary]
This article introduces you to j.u.c's futuretask, fork/join framework and BlockingQueue
2022.05.23 (lc_300_longest increment subsequence)
[C language] still don't understand the structure? Take a look at this article to give you a preliminary understanding of structure
基于改进SEIR模型分析上海疫情
[6.4-6.10] wonderful review of Blog
高考开启,VR全景可以这样看考点
618 great promotion is coming, mining bad reviews with AI and realizing emotional analysis of 100 million comments with zero code
大厂测试员年薪30万到月薪8K,吐槽工资太低,反被网友群嘲?
Nature Biotechnol | 李家洋/余泓团队利用平铺删除策略打破性状连锁,突破水稻产量瓶颈
Computer: successfully teach you how to use one trick to retrieve the previous password (the password once saved but currently displayed as ******)
[advanced C language] data storage [Part 2] [ten thousand words summary]
DDD landing practice repeat record of theoretical training & Event storm
金融行业的密钥及加密机制
Design and reality of JSP project laboratory management system based on SSM doc