2023-07-03 16:44:43 +08:00
|
|
|
|
/**
|
|
|
|
|
* File: climbing_stairs_dfs.cs
|
|
|
|
|
* Created Time: 2023-06-30
|
|
|
|
|
* Author: hpstory (hpstory1024@163.com)
|
|
|
|
|
*/
|
|
|
|
|
|
2023-09-02 23:54:38 +08:00
|
|
|
|
namespace hello_algo.chapter_dynamic_programming;
|
2023-07-03 16:44:43 +08:00
|
|
|
|
|
|
|
|
|
public class climbing_stairs_dfs {
|
|
|
|
|
/* 搜索 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public int Dfs(int i) {
|
2023-07-03 16:44:43 +08:00
|
|
|
|
// 已知 dp[1] 和 dp[2] ,返回之
|
|
|
|
|
if (i == 1 || i == 2)
|
|
|
|
|
return i;
|
|
|
|
|
// dp[i] = dp[i-1] + dp[i-2]
|
2023-10-08 01:33:46 +08:00
|
|
|
|
int count = Dfs(i - 1) + Dfs(i - 2);
|
2023-07-03 16:44:43 +08:00
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 爬楼梯:搜索 */
|
2023-10-08 01:33:46 +08:00
|
|
|
|
public int ClimbingStairsDFS(int n) {
|
|
|
|
|
return Dfs(n);
|
2023-07-03 16:44:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Test]
|
|
|
|
|
public void Test() {
|
|
|
|
|
int n = 9;
|
2023-10-08 01:33:46 +08:00
|
|
|
|
int res = ClimbingStairsDFS(n);
|
2023-07-03 16:44:43 +08:00
|
|
|
|
Console.WriteLine($"爬 {n} 阶楼梯共有 {res} 种方案");
|
|
|
|
|
}
|
|
|
|
|
}
|