当前位置:网站首页>补2:圆环回原点问题
补2:圆环回原点问题
2022-06-11 08:28:00 【zmm_mohua】
补2:圆环回原点问题
题目

分析
考虑爬楼梯问题,本题和其相似,当你 n 步到 0 点的时候,那么 n-1 步一定是会在 0 点的相邻位置(即 1 和 9 ),所以这是一道动态规划的问题。
那么 dp[i][j] 就表示从 0 点出发,走 i 步到达 j 点的方案数
于是有:dp[i][j] = dp[i-1][(j-1+length) % length] + dp[i-1][(j+1) % length]
其中: dp[i-1][(j-1+length) % length] :逆时针(后退)
dp[i-1][(j+1) % length] :顺时针(前进)
代码
#include <iostream>
#include <vector>
using namespace std;
int backToOrigin(int n){
int length = 10;
// 从0点出发走i步到j点的方案数
vector<vector<int> > dp(n + 1, vector<int>(length));
dp[0][0] = 1;
for(int i = 1; i < n + 1; i++){
for(int j = 0; j < length; j++){
// 逆时针(后退):dp[i-1][(j-1+length) % length]
// 顺时针(前进): dp[i-1][(j+1) % length]
dp[i][j] = dp[i-1][(j-1+length) % length] + dp[i-1][(j+1) % length];
}
}
return dp[n][0];
}
int main(){
int n, res;
cin>>n;
res = backToOrigin(n);
cout<<res;
return 0;
}
边栏推荐
- Timestamp of PostgreSQL and Oracle
- qiao-lerna:lerna辅助工具
- Disk format FAT32, exFAT, NTFS of [software tool]
- Summary of embedded software interview questions
- Dameng user management
- Typescript class and interface, class and generic, interface merging
- How to make hyperlinks in RichTextBox- How can I make a hyperlink work in a RichTextBox?
- Pg/oracle database ASCII code to string custom function
- Go language: string connection, digital conversion string
- (taking pytorch as an example) a simple understanding of the regularization method of path (depth) -drop path
猜你喜欢

(taking pytorch as an example) a simple understanding of the regularization method of path (depth) -drop path

【clickhouse专栏】新建库角色用户初始化

Anaconda+tensorflow most effective summary version (blood and tears summary of 6 reloads)

go for it Easily manage all types of items with "flying items"

Reference implementation scheme of database database and table division strategy

CentOS essay 03:centos8.2 installing MySQL

node报错整理

知识图谱入门之---yedda标注

(一)aac开篇-核心组件原理之Lifecycle、LiveData、ViewModel与源码分析技巧(转载)

Typescript header file usage details
随机推荐
【1】 Integrated learning: quickly understand Integrated Learning
Asynchronous notification mechanism of character device driver
ActiveMQ简单教程,适合初学者,学习笔记yyds
ICML2022有意思的文章
Introduction to guava cache usage
Sign in system design: how to draw the sign in function
Classical graph theory, depth first and breadth first, topology, prim and krukal, it's time to review
Difference between threadpooltaskexecutor and ThreadPoolExecutor
The difference between & & and &
结果和目标出入太大?不妨借助目标管理精准直达目标!
Js基础学习Script
SSM file upload and download
(resolved) the tqdm progress bar in the Jupiter notebook does not update and display in one line, but scrolls down to output
Pg/oracle database ASCII code to string custom function
你所不知道的console
What is concurrent search set? Are you still worried about it? In fact, it is a problem of connected graph, which is not so difficult to understand
Zookepper===> animal management system
Record a murder case caused by ignoring the @suppresslint ("newapi") prompt
Empty difference between postgrepsql and Oracle
Typescript namespace