当前位置:网站首页>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 ~~~
边栏推荐
- “中国网事·感动2022”二季度网络感动人物评选结果揭晓
- [C language] advanced pointer exercise 1
- Find the memory occupied by the structure
- Token verification program index.php when configuring wechat official account server
- [C language] simulation implementation of pow function (recursion)
- [C language] Pointer advanced knowledge points
- HSETNX KEY_NAME FIELD VALUE 用法
- 河北邯郸:拓展基层就业空间 助力高校毕业生就业
- flask_ Mail source code error
- Overcome the "fear of looking at teeth", and we use technology to change the industry
猜你喜欢
![最大交换[贪心思想&单调栈实现]](/img/ad/8f0914f23648f37e1d1ce69086fd2e.png)
最大交换[贪心思想&单调栈实现]

JVM (24) -- performance monitoring and tuning (5) -- Analyzing GC logs
![[NPP installation plug-in]](/img/6f/97e53116ec4ebc6a6338d125ddad8b.png)
[NPP installation plug-in]

Multi-Modal Knowledge Graph Construction and Application: A Survey

河北邯郸:拓展基层就业空间 助力高校毕业生就业

私有化部署的即时通讯平台,为企业移动业务安全保驾护航

Article translation software - batch free translation software supports major translation interfaces

WFST decoding process
![[C language] Pointer advanced knowledge points](/img/8f/0057243c603ddfe20381c9bd446f03.png)
[C language] Pointer advanced knowledge points

Getting started with enterprise distributed crawler framework
随机推荐
[experiment sharing] CCIE BGP reflector experiment
Array method added in ES6
Communication learning static routing across regional networks
Merge sort template
robobrowser的简单使用
Reverse string
Power Bi 2021 calendar DAX code
JVM(二十四) -- 性能监控与调优(五) -- 分析GC日志
Two methods to judge the size end
河北:稳粮扩豆助力粮油生产提质增效
爬取IP
Prometheus deployment
A chip company fell in round B
通配符 SSL/TLS 证书
Use of strtok and strError
C+ + core programming
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
In the second half of 2022, the system integration project management engineer certification starts on August 20
Cdga | how can the industrial Internet industry do a good job in data governance?
C language - question brushing column