走楼梯2(Daimayuan Online Judge)

简介: 走楼梯2(Daimayuan Online Judge)

这题我的解法是使用状态机思维且看我下面的状态关系图:

这是我给出的三个状态,走一步用0状态表示,走一次两步用1状态表示,走两次两步用状态2表示,他们的转移关系如图

对图的解释:
1.走一步怎么转移过来的:可以选择继续走一步转移过来(如图中的自环);走两步无论第一次还是第二次走两步,可以下一次走一步。

2.走一次两步怎么转移过来的: 可以走一步后选择走两步。
3.连续走两次两步怎么转移过来的: 可以走一次两步后再接着走两步。

1.状态定义

定义f [ i , j ] f[i,j]f[i,j],i ii 为此时是第几个台阶,j jj为此时的状态是走一步转移过来的(0),还是连续两次走两步过来的(2),还是只走一次两步过来的(1),此时的方案数为f [ i ] [ 0 ] + f [ i ] [ 1 ] + f [ i ] [ 2 ] f[i][0]+f[i][1]+f[i][2]f[i][0]+f[i][1]+f[i][2]

2.状态表示

根据状态图可以得出下面的转移式

1.f [ i ] [ 0 ] + = f [ i − 1 ] [ 0 ] + f [ i − 1 ] [ 2 ] + f [ i − 1 ] [ 1 ] ; f[i][0]+=f[i-1][0]+f[i-1][2]+f[i-1][1];f[i][0]+=f[i1][0]+f[i1][2]+f[i1][1];

2.f [ i ] [ 1 ] + = f [ i − 2 ] [ 0 ] ; f[i][1]+=f[i-2][0];f[i][1]+=f[i2][0];

3.f [ i ] [ 2 ] + = f [ i − 2 ] [ 1 ] ; f[i][2]+=f[i-2][1];f[i][2]+=f[i2][1];

/*********************************************************************
    程序名:
    版权: Joecai
    作者: Joecai
    日期: 2022-03-27 21:17
    说明:
*********************************************************************/
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
# define rep(i,be,en) for(int i=be;i<=en;i++)
# define pre(i,be,en) for(int i=be;i>=en;i--)
typedef pair<int, int> PII;
#define ll long long
#define endl "\n"
#define LOCAL
#define pb push_back
const int N = 2e5 + 10, INF = 0x3f3f3f3f;
int n;
ll f[100][3];
void solve()
{
  cin >> n;
  f[0][0] = 1;
  f[1][0] = 1;
  for (int i = 2; i <= n; i++)
  {
    f[i][0] += f[i - 1][0] + f[i - 1][2] + f[i - 1][1];
    f[i][1] += f[i - 2][0];
    f[i][2] += + f[i - 2][1];
  }
  cout << f[n][0] + f[n][1] + f[n][2] << endl;
}
int main() {
  //std::ios::sync_with_stdio(false);
  //std::cin.tie(nullptr);
  //#ifdef LOCAL
  //freopen("data.in.txt","r",stdin);
  //freopen("data.out.txt","w",stdout);
  //#endif
  int __ = 1;
  //cin>>__;
  while (__--)
  {
    solve();
  }
  return 0;
}

点个赞走吧~

目录
相关文章
|
存储
HDOJ/HDU 1073 Online Judge(字符串处理~)
HDOJ/HDU 1073 Online Judge(字符串处理~)
110 0
|
人工智能 Java
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
HDU - 2018杭电ACM集训队单人排位赛 - 4 - Problem J. number sequence
147 0
|
测试技术 PHP C#
用C#做了个Online Judge,正在半公开测试,希望各位朋友赏光看下
Online Judge即在线评判系统。大学生acm/icpc程序设计大赛的评判系统即Online Judge。学习程序的同学可以通过Online Judge练习自己的技术与能力。成幻Online Judge简介:支持语言:g++/c++: 可以编译C语言或C++代码。
1023 0
uva 674Coin Change(完全背包)
点击打开链接uva 674 思路: 完全背包 分析: 裸题 代码: #include #include #include #include using namespace std; const int MAXN = 8000; ...
912 0
P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组(poj3261)
P2852 [USACO06DEC]牛奶模式Milk Patterns 后缀数组(poj3261)
124 0
Codeforces 791A Bear and Big Brother(暴力枚举,模拟)
A. Bear and Big Brother time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard ou...
1291 0
|
Java Go
组合数学 - 母函数的变形 --- hdu 1171:Big Event in HDU
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24002    Accepted Submissio...
1076 0
Optimal Coin Change(完全背包计数)
题目描述 In a 10-dollar shop, everything is worthy 10 dollars or less. In order to serve customers more effectively at the cashier, change needs to be provided in the minimum number of coins. In this problem, you are going to provide a given value of the change in different coins.
101 0
|
C++
【PAT甲级 - C++题解】1038 Recover the Smallest Number
【PAT甲级 - C++题解】1038 Recover the Smallest Number
89 1

热门文章

最新文章