mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-24 23:56:29 +08:00
feat: Add C code for the section of intro to DP (#771)
* Add dynamic programming C language code * Add dynamic programming chapter C language code * Modifying errors in files
This commit is contained in:
parent
22be495412
commit
191330e73a
4 changed files with 174 additions and 0 deletions
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* File: climbing_stairs_backtrack.c
|
||||
* Created Time: 2023-09-22
|
||||
* Author: huawuque404 (huawuque404@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 回溯 */
|
||||
void backtrack(int *choices, int state, int n, int *res, int len) {
|
||||
// 当爬到第 n 阶时,方案数量加 1
|
||||
if (state == n)
|
||||
res[0]++;
|
||||
// 遍历所有选择
|
||||
for (int i = 0; i < len; i++) {
|
||||
int choice = choices[i];
|
||||
// 剪枝:不允许越过第 n 阶
|
||||
if (state + choice > n)
|
||||
break;
|
||||
// 尝试:做出选择,更新状态
|
||||
backtrack(choices, state + choice, n, res, len);
|
||||
// 回退
|
||||
}
|
||||
}
|
||||
|
||||
/* 爬楼梯:回溯 */
|
||||
int climbingStairsBacktrack(int n) {
|
||||
int choices[2] = {1, 2}; // 可选择向上爬 1 或 2 阶
|
||||
int state = 0; // 从第 0 阶开始爬
|
||||
int *res = (int *)malloc(sizeof(int));
|
||||
*res = 0; // 使用 res[0] 记录方案数量
|
||||
int len = sizeof(choices) / sizeof(int);
|
||||
backtrack(choices, state, n, res, len);
|
||||
int result = *res;
|
||||
free(res);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsBacktrack(n);
|
||||
printf("爬 %d 阶楼梯共有 %d 种方案\n", n, res);
|
||||
|
||||
return 0;
|
||||
}
|
32
codes/c/chapter_dynamic_programming/climbing_stairs_dfs.c
Normal file
32
codes/c/chapter_dynamic_programming/climbing_stairs_dfs.c
Normal file
|
@ -0,0 +1,32 @@
|
|||
/**
|
||||
* File: climbing_stairs_dfs.c
|
||||
* Created Time: 2023-09-19
|
||||
* Author: huawuque404 (huawuque404@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 搜索 */
|
||||
int dfs(int i) {
|
||||
// 已知 dp[1] 和 dp[2] ,返回之
|
||||
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;
|
||||
}
|
||||
|
||||
/* 爬楼梯:搜索 */
|
||||
int climbingStairsDFS(int n) {
|
||||
return dfs(n);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsDFS(n);
|
||||
printf("爬 %d 阶楼梯共有 %d 种方案\n", n, res);
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
/**
|
||||
* File: climbing_stairs_dfs_mem.c
|
||||
* Created Time: 2023-09-19
|
||||
* Author: huawuque404 (huawuque404@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 记忆化搜索 */
|
||||
int dfs(int i, int *mem) {
|
||||
// 已知 dp[1] 和 dp[2] ,返回之
|
||||
if (i == 1 || i == 2)
|
||||
return i;
|
||||
// 若存在记录 dp[i] ,则直接返回之
|
||||
if (mem[i] != -1)
|
||||
return mem[i];
|
||||
// dp[i] = dp[i-1] + dp[i-2]
|
||||
int count = dfs(i - 1, mem) + dfs(i - 2, mem);
|
||||
// 记录 dp[i]
|
||||
mem[i] = count;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* 爬楼梯:记忆化搜索 */
|
||||
int climbingStairsDFSMem(int n) {
|
||||
// mem[i] 记录爬到第 i 阶的方案总数,-1 代表无记录
|
||||
int *mem = (int *)malloc((n + 1) * sizeof(int));
|
||||
for (int i = 0; i <= n; i++) {
|
||||
mem[i] = -1;
|
||||
}
|
||||
int result = dfs(n, mem);
|
||||
free(mem);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsDFSMem(n);
|
||||
printf("爬 %d 阶楼梯共有 %d 种方案\n", n, res);
|
||||
|
||||
return 0;
|
||||
}
|
51
codes/c/chapter_dynamic_programming/climbing_stairs_dp.c
Normal file
51
codes/c/chapter_dynamic_programming/climbing_stairs_dp.c
Normal file
|
@ -0,0 +1,51 @@
|
|||
/**
|
||||
* File: climbing_stairs_dp.c
|
||||
* Created Time: 2023-09-19
|
||||
* Author: huawuque404 (huawuque404@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 爬楼梯:动态规划 */
|
||||
int climbingStairsDP(int n) {
|
||||
if (n == 1 || n == 2)
|
||||
return n;
|
||||
// 初始化 dp 表,用于存储子问题的解
|
||||
int *dp = (int *)malloc((n + 1) * sizeof(int));
|
||||
// 初始状态:预设最小子问题的解
|
||||
dp[1] = 1;
|
||||
dp[2] = 2;
|
||||
// 状态转移:从较小子问题逐步求解较大子问题
|
||||
for (int i = 3; i <= n; i++) {
|
||||
dp[i] = dp[i - 1] + dp[i - 2];
|
||||
}
|
||||
int result = dp[n];
|
||||
free(dp);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* 爬楼梯:空间优化后的动态规划 */
|
||||
int climbingStairsDPComp(int n) {
|
||||
if (n == 1 || n == 2)
|
||||
return n;
|
||||
int a = 1, b = 2;
|
||||
for (int i = 3; i <= n; i++) {
|
||||
int tmp = b;
|
||||
b = a + b;
|
||||
a = tmp;
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
int n = 9;
|
||||
|
||||
int res = climbingStairsDP(n);
|
||||
printf("爬 %d 阶楼梯共有 %d 种方案\n", n, res);
|
||||
|
||||
res = climbingStairsDPComp(n);
|
||||
printf("爬 %d 阶楼梯共有 %d 种方案\n", n, res);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue