標籤:style blog http color os io
樓梯有n階台階,上樓可以一步上1階,也可以一步上2階,編一程式列出每一種走法。
例:3階台階的走法是
{
{ 1, 1, 1, },
{ 1, 2, },
{ 2, 1, },
}
因為每一步只有兩個選擇1/2階,那麼根據題意可建立一棵二叉樹,比如當n=3時:
0 / 1 2 / \ / 1 2 1 / 1
這樣(深搜+回溯)一棵樹的所有到達葉子節點的路徑就是上樓梯的所有方法了
(遞迴法)C++代碼如下:
#include <iostream>
#include <list>
struct Node{ int value = 0; Node *left = nullptr; Node *right = nullptr;};//@param n 總樓梯數//@param value 步長(1 || 2)//@param cur 當前已走過的總樓梯數void BuildTree(Node *root, int n, int value, int cur = 0){ root->value = value; cur += value; if (cur == n) return; if (cur + 1 <= n) { root->left = new Node; BuildTree(root->left, n, 1, cur); } if (cur + 2 <= n) { root->right = new Node; BuildTree(root->right, n, 2, cur); }}void PrintAll(Node *root, std::list<int> &vec, int cur = 0){ if (root == nullptr) return; cur += root->value; vec.push_back(root->value); if (root->left == nullptr && root->right == nullptr) { for (int i : vec) { if(i != 0) printf("%d ", i); } printf("\n"); } PrintAll(root->left, vec, cur); PrintAll(root->right, vec, cur); vec.pop_back();}void DeleteAll(Node *root){ if (root == nullptr) return; DeleteAll(root->left); DeleteAll(root->right); delete root;}void Compute(int n){ Node *root = new Node; std::list<int> st; BuildTree(root, n, 0); PrintAll(root, st); DeleteAll(root);}int main(){ std::cout << "n = 3 :" << std::endl; Compute(3); std::cout << std::endl; std::cout << "n = 4:" << std::endl; Compute(4);}
(遞迴法)lua 代碼:
--@param n 總樓梯數[email protected] value 步長(1 || 2)--@param cur 當前已走過的總樓梯數function BuildTree(root, n, value, cur) root.value = value cur = cur + value if cur == n then return end if cur + 1 <= n then root.left = { value = 0, left = nil, right = nil} BuildTree(root.left, n, 1, cur) end if cur + 2 <= n then root.right = { value = 0, left = nil, right = nil} BuildTree(root.right, n, 2, cur) endend
function PrintAllPath(root, stack, cur) if root == nil then return end cur = cur + root.value table.insert(stack, root.value) if root.left == nil and root.right == nil then for key, value in pairs(stack) do if value ~= 0 then io.write(value,‘ ‘) end end io.write(‘\n‘) end PrintAllPath(root.left, stack, cur) PrintAllPath(root.right, stack, cur) table.remove(stack)endfunction Compute(n) stack = {} root = { value = 0, left = nil, right = nil} BuildTree(root, n, 0, 0) --BuildTree(root, n) PrintAllPath(root, stack, 0)endprint("n=3:")Compute(3)io.write(‘\n‘)print("n=4:")Compute(4)io.write(‘\n‘)print("n=5:")Compute(5)
迭代法(C++):
void BuildTree(Node *root, int n){ if (root == nullptr) return; int cur(0); std::stack<Node*> st; std::map<Node*, int> stepMap;//記錄當前已走過的樓梯數 Node *p = root; st.push(p); stepMap[p] = 0; while (!st.empty()) { p = st.top(); st.pop(); cur = stepMap[p]; if (cur + 2 <= n) { p->right = new Node; p->right->value = 2; if (cur + 2 < n) { st.push(p->right); stepMap[p->right] = cur + 2; } } if (cur + 1 <= n) { p->left = new Node; p->left->value = 1; if (cur + 1 < n) { st.push(p->left); stepMap[p->left] = cur + 1; } } }}
迭代法(Lua):
function BuildTree(root, n) if root == nil then return end cur = 0 stack = {} stepMap = {} p = root table.insert(stack, p) stepMap[p] = 0 while(#stack ~= 0) do p = stack[1] table.remove(stack, 1) cur = stepMap[p] if cur + 2 <= n then p.right = {value = 2, left = nil, right = nil} if cur + 2 < n then table.insert(stack, p.right) stepMap[p.right] = cur + 2 end end if cur + 1 <= n then p.left = {value = 1, left = nil, right = nil} if cur + 1 < n then table.insert(stack, p.left) stepMap[p.left] = cur + 1 end end endend