📌  相关文章
📜  由N个盒子组成的不同楼梯的总数

📅  最后修改于: 2021-04-23 07:01:12             🧑  作者: Mango

给定N个框,每个框的尺寸为(1×1 meter^2 )。任务是根据以下规则找到可以从这些框中制作的不同楼梯的总数:

  • 楼梯必须严格按降序排列。
  • 每个楼梯至少包含两个台阶。 (总步长等于楼梯的宽度。)

例子

如果考虑总步数= 2,则可以观察到如果N增加2,则楼梯的数量增加1。我们可以从下图说明上述情况:

现在,如果总步数大于2(假设总步数= K),那么我们可以将其作为第一步,为楼梯创建一个底数(底数需要等于总步数的框),然后在其上放置另一个步长为K的楼梯和K – 1的框为N –K。(因为K框已用于创建基础)。因此,我们可以使用自底向上的动态编程来解决此问题。

下面是上述方法的实现:

C++
// C++ program to find the total number of
// different staircase that can made
// from N boxes
#include 
using namespace std;
  
// Function to find the total number of
// different staircase that can made
// from N boxes
int countStaircases(int N)
{
    // DP table, there are two states.
    // First describes the number of boxes
    // and second describes the step
    int memo[N + 5][N + 5];
  
    // Initilize all the elements of
    // the table to zero
    for (int i = 0; i <= N; i++) {
        for (int j = 0; j <= N; j++) {
            memo[i][j] = 0;
        }
    }
  
    // Base case
    memo[3][2] = memo[4][2] = 1;
  
    for (int i = 5; i <= N; i++) {
        for (int j = 2; j <= i; j++) {
  
            // When step is equal to 2
            if (j == 2) {
                memo[i][j] = memo[i - j][j] + 1;
            }
  
            // When step is greater than 2
            else {
                memo[i][j] = memo[i - j][j] + 
                             memo[i - j][j - 1];
            }
        }
    }
  
    // Count the total staircase
    // from all the steps
    int answer = 0;
    for (int i = 1; i <= N; i++) 
        answer = answer + memo[N][i];    
  
    return answer;
}
  
// Driver Code
int main()
{
    int N = 7;
  
    cout << countStaircases(N);
  
    return 0;
}


Java
// Java program to find the total number of
// different staircase that can made
// from N boxes
  
import java.util.*;
  
class GFG
{
        // Function to find the total number of
        // different staircase that can made
        // from N boxes
        static int countStaircases(int N)
        {
            // DP table, there are two states.
            // First describes the number of boxes
            // and second describes the step
            int [][] memo=new int[N + 5][N + 5];
          
            // Initilize all the elements of
            // the table to zero
            for (int i = 0; i <= N; i++) {
                for (int j = 0; j <= N; j++) {
                    memo[i][j] = 0;
                }
            }
          
            // Base case
            memo[3][2] = memo[4][2] = 1;
          
            for (int i = 5; i <= N; i++) {
                for (int j = 2; j <= i; j++) {
          
                    // When step is equal to 2
                    if (j == 2) {
                        memo[i][j] = memo[i - j][j] + 1;
                    }
          
                    // When step is greater than 2
                    else {
                        memo[i][j] = memo[i - j][j] + 
                                    memo[i - j][j - 1];
                    }
                }
            }
          
            // Count the total staircase
            // from all the steps
            int answer = 0;
            for (int i = 1; i <= N; i++) 
                answer = answer + memo[N][i]; 
          
            return answer;
        }
          
        // Driver Code
        public static void main(String [] args)
        {
            int N = 7;
          
            System.out.println(countStaircases(N));
          
              
        }
  
}
  
// This code is contributed 
// by ihritik


Python 3
# Python 3 program to find the total 
# number of different staircase that 
# can made from N boxes
  
# Function to find the total number 
# of different staircase that can 
# made from N boxes
def countStaircases(N):
  
    # DP table, there are two states.
    # First describes the number of boxes
    # and second describes the step
    memo = [[0 for x in range(N + 5)]
               for y in range(N + 5)]
  
    # Initilize all the elements of
    # the table to zero
    for i in range(N + 1):
        for j in range (N + 1):
            memo[i][j] = 0
          
    # Base case
    memo[3][2] = memo[4][2] = 1
  
    for i in range (5, N + 1) :
        for j in range (2, i + 1) :
  
            # When step is equal to 2
            if (j == 2) :
                memo[i][j] = memo[i - j][j] + 1
              
            # When step is greater than 2
            else :
                memo[i][j] = (memo[i - j][j] + 
                              memo[i - j][j - 1])
      
    # Count the total staircase
    # from all the steps
    answer = 0
    for i in range (1, N + 1):
        answer = answer + memo[N][i] 
  
    return answer
  
# Driver Code
if __name__ == "__main__":
  
    N = 7
  
    print (countStaircases(N))
  
# This code is contributed
# by ChitraNayal


C#
// C# program to find the total number 
// of different staircase that can made
// from N boxes
using System;
  
class GFG
{
      
// Function to find the total number 
// of different staircase that can 
// made from N boxes
static int countStaircases(int N)
{
    // DP table, there are two states.
    // First describes the number of boxes
    // and second describes the step
    int [,] memo = new int[N + 5, N + 5];
  
    // Initilize all the elements 
    // of the table to zero
    for (int i = 0; i <= N; i++) 
    {
        for (int j = 0; j <= N; j++)
        {
            memo[i, j] = 0;
        }
    }
  
    // Base case
    memo[3, 2] = memo[4, 2] = 1;
  
    for (int i = 5; i <= N; i++)
    {
        for (int j = 2; j <= i; j++) 
        {
  
            // When step is equal to 2
            if (j == 2) 
            {
                memo[i, j] = memo[i - j, j] + 1;
            }
  
            // When step is greater than 2
            else 
            {
                memo[i, j] = memo[i - j, j] + 
                             memo[i - j, j - 1];
            }
        }
    }
  
    // Count the total staircase
    // from all the steps
    int answer = 0;
    for (int i = 1; i <= N; i++) 
        answer = answer + memo[N, i]; 
  
    return answer;
}
  
// Driver Code
public static void Main()
{
    int N = 7;
  
    Console.WriteLine(countStaircases(N));
}
}
  
// This code is contributed 
// by Subhadeep


PHP


输出:
4

时间复杂度:O( n^2 )。