当前位置:网站首页>Durham High Lord (classic DP)
Durham High Lord (classic DP)
2022-07-28 20:14:00 【Tiredd】
The question :

Ideas :dfs Ideas , from 1 Start passing m Time , You can choose left or right every time , But the complexity is 2^30 Obviously not ( Can be seen as a 30 The binary number of bits , Yes 0 and 1, When traversal to 11111……111 It will traverse all the schemes , This is it. dfs Complexity ), But we can use the violence code , Write a test to see us dp Is it correct
dfs The code is as follows :
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m, res;
void dfs(int u, int cnt)
{
// Move to the left
if(u == 0)
{
dfs(n, cnt);
return ;
}
if(u == n + 1)
{
dfs(1, cnt);
return ;
}
if(cnt == 0)
{
if(u == 1)
res ++ ;
return ;
}
// Move left
// cout << u << endl;
if(cnt - 1 >= 0)
dfs(u - 1, cnt - 1);
// Move right
if(cnt - 1 >= 0)
dfs(u + 1, cnt - 1);
}
int main()
{
cin >> n >> m;
dfs(1, m);
cout << res << endl;
return 0;
}DP Ideas : The equation of state of this problem is still very easy to figure out , The equation of state should grasp how to clearly describe a kind of state . Can be set f[i][j] To pass j Time , The current in i Number of schemes .
We get the equation of state transfer f[i][j] = f[i - 1][j - 1] + f[i + 1][j - 1].
You can find that the status is updated , from j - 1 Column updated . The update of the line is made by i + 1 and i - 1 to update . So if you hold it first , It will cause when calculating the current state , Their pre state has not been calculated . So here we take enumeration by column .
The code is as follows :
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5050;
int n, m, f[N][N]; //fij To pass j Time , The current in i Total number of programmes , Easy to get f[i][j] = f[i - 1][j - 1] + f[i + 1][j - 1],
// If it is held first , It will lead to calculation fij when , The last question did not calculate , So first enumerate the columns
int main()
{
cin >> n >> m;
f[1][0] = 1; // Pass on 0 Time , The current in 1 Start the problem , The plan is 1
for(int j = 1; j <= m; j ++ )
for(int i = 1; i <= n; i ++ )
{
if(i - 1 == 0) f[i][j] = f[n][j - 1]; // A special case , Special judgement
else f[i][j] = f[i - 1][j - 1];
if(i + 1 == n + 1) f[i][j] += f[1][j - 1];// A special case , Special judgement
else f[i][j] += f[i + 1][j - 1];
}
cout << f[1][m] << endl;
return 0;
}In this way, we can successfully solve this problem ~~~
边栏推荐
- 1. C language variable type, global variable, local variable
- 2022年下半年系统集成项目管理工程师认证8月20日开班
- English translation Italian - batch English translation Italian tools free of charge
- Implementation of memcpy in C language
- XOR operation and its usage
- 为什么客户支持对SaaS公司很重要?
- robobrowser的简单使用
- English Translation Spanish - batch English Translation Spanish tools free of charge
- Prometheus deployment
- Circular linked list OJ question
猜你喜欢

NEIL: Extracting Visual Knowledge from Web Data

English translation Italian - batch English translation Italian tools free of charge
![[NPP installation plug-in]](/img/6f/97e53116ec4ebc6a6338d125ddad8b.png)
[NPP installation plug-in]

WFST decoding process

Why is customer support important to SaaS?
![[C language] summary of methods for solving the greatest common divisor](/img/38/3a099948ebf51fd0da3076f71f9dad.png)
[C language] summary of methods for solving the greatest common divisor

为什么客户支持对SaaS公司很重要?

跨区域网络的通信学习静态路由

C language pointer and two-dimensional array
![[experiment sharing] CCIE BGP reflector experiment](/img/e4/1ddd611c8438cb6ca1be32f34fa67a.png)
[experiment sharing] CCIE BGP reflector experiment
随机推荐
[C language] Gobang game [array and function]
Solve flask integration_ Error reporting in restplus
lattice
Know small and medium LAN WLAN
C language - data type
数字图像理论知识(一)(个人浅析)
English translation Italian - batch English translation Italian tools free of charge
Communication learning static routing across regional networks
Saltstack advanced
Sequential linear table - practice in class
河北:稳粮扩豆助力粮油生产提质增效
New fruit naming (DP is similar to the longest common subsequence)
基于 MinIO 对象存储保障 Rancher 数据
[experiment sharing] CCIE BGP reflector experiment
Information management system and games based on C language
Overcome the "fear of looking at teeth", and we use technology to change the industry
Source code analysis of scripy spider
Data system of saltstack
Labelme (I)
[NPP installation plug-in]