cpp-algo-cases/chapter_dynamic_programming/climbing_stairs_dfs.cpp

33 lines
584 B
C++
Raw Normal View History

2024-02-20 16:13:23 +08:00
/**
* File: climbing_stairs_dfs.cpp
* Created Time: 2023-06-30
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* <20><><EFBFBD><EFBFBD> */
int dfs(int i) {
// <20><>֪ dp[1] <20><> dp[2] <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮
if (i == 1 || i == 2)
return i;
// dp[i] = dp[i-1] + dp[i-2]
int count = dfs(i - 1) + dfs(i - 2);
return count;
}
/* <20><>¥<EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD> */
int climbingStairsDFS(int n) {
return dfs(n);
}
/* Driver Code */
int main() {
int n = 9;
int res = climbingStairsDFS(n);
cout << "<EFBFBD><EFBFBD> " << n << " <20><>¥<EFBFBD>ݹ<EFBFBD><DDB9><EFBFBD> " << res << " <20>ַ<EFBFBD><D6B7><EFBFBD>" << endl;
return 0;
}