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.
hello-algo/codes/ruby/chapter_dynamic_programming/climbing_stairs_dfs.rb

27 lines
481 B

=begin
File: climbing_stairs_dfs.rb
Created Time: 2024-05-29
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
### 搜索 ###
def dfs(i)
# 已知 dp[1] 和 dp[2] ,返回之
return i if i == 1 || i == 2
# dp[i] = dp[i-1] + dp[i-2]
dfs(i - 1) + dfs(i - 2)
end
### 爬楼梯:搜索 ###
def climbing_stairs_dfs(n)
dfs(n)
end
### Driver Code ###
if __FILE__ == $0
n = 9
res = climbing_stairs_dfs(n)
puts "#{n} 阶楼梯共有 #{res} 种方案"
end