You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
694 B
32 lines
694 B
// File: climbing_stairs_dfs.zig
|
|
// Created Time: 2023-07-15
|
|
// Author: codingonion (coderonion@gmail.com)
|
|
|
|
const std = @import("std");
|
|
|
|
// 搜尋
|
|
fn dfs(i: usize) i32 {
|
|
// 已知 dp[1] 和 dp[2] ,返回之
|
|
if (i == 1 or i == 2) {
|
|
return @intCast(i);
|
|
}
|
|
// dp[i] = dp[i-1] + dp[i-2]
|
|
var count = dfs(i - 1) + dfs(i - 2);
|
|
return count;
|
|
}
|
|
|
|
// 爬樓梯:搜尋
|
|
fn climbingStairsDFS(comptime n: usize) i32 {
|
|
return dfs(n);
|
|
}
|
|
|
|
// Driver Code
|
|
pub fn main() !void {
|
|
comptime var n: usize = 9;
|
|
|
|
var res = climbingStairsDFS(n);
|
|
std.debug.print("爬 {} 階樓梯共有 {} 種方案\n", .{ n, res });
|
|
|
|
_ = try std.io.getStdIn().reader().readByte();
|
|
}
|