2023-07-15 23:16:02 +08:00
|
|
|
// File: climbing_stairs_constraint_dp.zig
|
|
|
|
// Created Time: 2023-07-15
|
2024-01-02 01:25:37 +08:00
|
|
|
// Author: codingonion (coderonion@gmail.com)
|
2023-07-15 23:16:02 +08:00
|
|
|
|
|
|
|
const std = @import("std");
|
|
|
|
|
|
|
|
// 带约束爬楼梯:动态规划
|
2023-07-16 15:36:28 +08:00
|
|
|
fn climbingStairsConstraintDP(comptime n: usize) i32 {
|
2023-07-15 23:16:02 +08:00
|
|
|
if (n == 1 or n == 2) {
|
2023-08-31 02:31:58 +08:00
|
|
|
return 1;
|
2023-07-15 23:16:02 +08:00
|
|
|
}
|
|
|
|
// 初始化 dp 表,用于存储子问题的解
|
|
|
|
var dp = [_][3]i32{ [_]i32{ -1, -1, -1 } } ** (n + 1);
|
|
|
|
// 初始状态:预设最小子问题的解
|
|
|
|
dp[1][1] = 1;
|
|
|
|
dp[1][2] = 0;
|
|
|
|
dp[2][1] = 0;
|
|
|
|
dp[2][2] = 1;
|
|
|
|
// 状态转移:从较小子问题逐步求解较大子问题
|
|
|
|
for (3..n + 1) |i| {
|
|
|
|
dp[i][1] = dp[i - 1][2];
|
|
|
|
dp[i][2] = dp[i - 2][1] + dp[i - 2][2];
|
|
|
|
}
|
|
|
|
return dp[n][1] + dp[n][2];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Driver Code
|
|
|
|
pub fn main() !void {
|
|
|
|
comptime var n: usize = 9;
|
|
|
|
|
2023-07-16 15:36:28 +08:00
|
|
|
var res = climbingStairsConstraintDP(n);
|
2023-07-15 23:16:02 +08:00
|
|
|
std.debug.print("爬 {} 阶楼梯共有 {} 种方案\n", .{ n, res });
|
|
|
|
|
|
|
|
_ = try std.io.getStdIn().reader().readByte();
|
|
|
|
}
|