当前位置:网站首页>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 ~~~
边栏推荐
- zfoo增加类似于mydog的路由
- Common modules of saltstack
- 私有化部署的即时通讯平台,为企业移动业务安全保驾护航
- local/chain/run_ tdnn.sh:
- JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver
- 最大交换[贪心思想&单调栈实现]
- Article translation software - batch free translation software supports major translation interfaces
- 通配符 SSL/TLS 证书
- Advanced notes (Part 2)
- C+ + core programming
猜你喜欢
![[C language] Pointer advanced knowledge points](/img/8f/0057243c603ddfe20381c9bd446f03.png)
[C language] Pointer advanced knowledge points

9. Pointer of C language (4) pointer and one-dimensional array, pointer operation

Tencent cloud deployment lamp_ Experience of building a station

JVM(二十四) -- 性能监控与调优(五) -- 分析GC日志
![[C language] header file of complex number four operations and complex number operations](/img/f9/b389fe5367f1fa6cd18aaac856bc0d.png)
[C language] header file of complex number four operations and complex number operations

Intermediate soft test (system integration project management engineer) high frequency test site

What is the variance?
![[C language] simulation implementation of strlen (recursive and non recursive)](/img/73/e92fe714515491f1ea366d6924c9ec.png)
[C language] simulation implementation of strlen (recursive and non recursive)

Array out of bounds

83. (cesium home) how the cesium example works
随机推荐
The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced
基于 MinIO 对象存储保障 Rancher 数据
2、 Relationship between software operation and memory
Scene thread allocation in MMO real-time combat games
English translation Italian - batch English translation Italian tools free of charge
8. Compilation errors of C language and Chinese explanation
Longest Palindromic Substring
adb remount of the / superblock failed: Permission denied
[C language] Pointer elementary knowledge points
Crawl IP
[C language] scanf format input and modifier summary
Read how to deploy highly available k3s with external database
Item exception handling in SSM
Return and job management of saltstack
Multi-Modal Knowledge Graph Construction and Application: A Survey
Using Lex (Flex) to generate lexical analyzer of PL language
BeanFactory not initialized or already closed - call ‘refresh‘ before accessing beans via the Applic
In the second half of 2022, the system integration project management engineer certification starts on August 20
[C language] advanced pointer exercise 1
Find the memory occupied by the structure