📜  德兰诺伊数

📅  最后修改于: 2021-04-26 17:37:22             🧑  作者: Mango

在数学中,Delanooy数字D描述了从矩形网格的西南角(0,0)到东北角(m,n)的路径数,仅需向北,向东北或向东移动。
例如,D(3,3)等于63。
Delannoy数可通过以下公式计算:

Delannoy数可用于查找:

  • 计算长度为m和n的两个序列的整体比对的数量。
  • m维整数格中距原点最多n步的点数。
  • 在细胞自动机中,半径为n的m维冯·诺伊曼(von Neumann)邻域中的细胞数。
  • 半径为n的m维冯·诺伊曼(von Neumann)邻域表面上的细胞数量。

例子 :

Input : n = 3, m = 3
Output : 63

Input : n = 4, m = 5
Output : 681

以下是查找Delannoy编号的实现:

C++
// CPP Program of finding nth Delannoy Number.
#include 
using namespace std;
 
// Return the nth Delannoy Number.
int dealnnoy(int n, int m)
{
    // Base case
    if (m == 0 || n == 0)
        return 1;
 
    // Recursive step.
    return dealnnoy(m - 1, n) +
           dealnnoy(m - 1, n - 1) +
           dealnnoy(m, n - 1);
}
 
// Driven Program
int main()
{
    int n = 3, m = 4;
    cout << dealnnoy(n, m) << endl;
    return 0;
}


Java
// Java Program for finding nth Delannoy Number.
import java.util.*;
import java.lang.*;
 
public class GfG{
     
    // Return the nth Delannoy Number.
    public static int dealnnoy(int n, int m)
    {
        // Base case
        if (m == 0 || n == 0)
            return 1;
 
        // Recursive step.
        return dealnnoy(m - 1, n) +
            dealnnoy(m - 1, n - 1) +
            dealnnoy(m, n - 1);
    }
     
    // driver function
    public static void main(String args[]){
        int n = 3, m = 4;
        System.out.println(dealnnoy(n, m));
    }
}
 
/* This code is contributed by Sagar Shukla. */


Python3
# Python3 Program for finding
# nth Delannoy Number.
 
# Return the nth Delannoy Number.
def dealnnoy(n, m):
     
    # Base case
    if (m == 0 or n == 0) :
        return 1
 
    # Recursive step.
    return dealnnoy(m - 1, n) + dealnnoy(m - 1, n - 1) + dealnnoy(m, n - 1)
 
# Driven code
n = 3
m = 4;
print( dealnnoy(n, m) )
 
# This code is contributed by "rishabh_jain".


C#
// C# Program for finding nth Delannoy Number.
using System;
 
public class GfG {
 
    // Return the nth Delannoy Number.
    public static int dealnnoy(int n, int m)
    {
 
        // Base case
        if (m == 0 || n == 0)
            return 1;
 
        // Recursive step.
        return dealnnoy(m - 1, n) +
               dealnnoy(m - 1, n - 1) +
                     dealnnoy(m, n - 1);
    }
 
    // driver function
    public static void Main()
    {
        int n = 3, m = 4;
        Console.WriteLine(dealnnoy(n, m));
    }
}
 
/* This code is contributed by vt_m. */


PHP


Javascript


C++
// CPP Program of finding nth Delannoy Number.
#include 
using namespace std;
 
// Return the nth Delannoy Number.
int dealnnoy(int n, int m)
{
    int dp[m + 1][n + 1];
 
    // Base cases
    for (int i = 0; i <= m; i++)
        dp[i][0] = 1;
    for (int i = 0; i <= m; i++)
        dp[0][i] = 1;   
 
    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= n; j++)
            dp[i][j] = dp[i - 1][j] +
                       dp[i - 1][j - 1] +
                       dp[i][j - 1];
 
    return dp[m][n];
}
 
// Driven Program
int main()
{
    int n = 3, m = 4;
    cout << dealnnoy(n, m) << endl;
    return 0;
}


Java
// Java Program of finding nth Delannoy Number.
 
import java.io.*;
 
class GFG {
     
    // Return the nth Delannoy Number.
    static int dealnnoy(int n, int m)
    {
        int dp[][]=new int[m + 1][n + 1];
      
        // Base cases
        for (int i = 0; i <= m; i++)
            dp[i][0] = 1;
        for (int i = 0; i < m; i++)
            dp[0][i] = 1;   
      
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                dp[i][j] = dp[i - 1][j] +
                           dp[i - 1][j - 1] +
                           dp[i][j - 1];
      
        return dp[m][n];
    }
      
    // Driven Program
    public static void main(String args[])
    {
        int n = 3, m = 4;
        System.out.println(dealnnoy(n, m));
         
    }
}
 
// This code is contributed by Nikita Tiwari.


Python3
# Python3 Program for finding nth
# Delannoy Number.
 
# Return the nth Delannoy Number.
def dealnnoy (n, m):
    dp = [[0 for x in range(n+1)] for x in range(m+1)]
 
    # Base cases
    for i in range(m):
        dp[0][i] = 1
     
    for i in range(1, m + 1):
        dp[i][0] = 1
 
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] + dp[i][j - 1];
 
    return dp[m][n]
 
# Driven code
n = 3
m = 4
print(dealnnoy(n, m))
 
# This code is contributed by "rishabh_jain".


C#
// C# Program of finding nth Delannoy Number.
using System;
 
class GFG {
 
    // Return the nth Delannoy Number.
    static int dealnnoy(int n, int m)
    {
         
        int[, ] dp = new int[m + 1, n + 1];
 
        // Base cases
        for (int i = 0; i <= m; i++)
            dp[i, 0] = 1;
        for (int i = 0; i < m; i++)
            dp[0, i] = 1;
 
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                dp[i, j] = dp[i - 1, j]
                     + dp[i - 1, j - 1]
                         + dp[i, j - 1];
 
        return dp[m, n];
    }
 
    // Driven Program
    public static void Main()
    {
        int n = 3, m = 4;
         
        Console.WriteLine(dealnnoy(n, m));
    }
}
 
// This code is contributed by vt_m.


PHP


输出:

129

下面是找到第n个Delannoy数的动态编程程序:

C++

// CPP Program of finding nth Delannoy Number.
#include 
using namespace std;
 
// Return the nth Delannoy Number.
int dealnnoy(int n, int m)
{
    int dp[m + 1][n + 1];
 
    // Base cases
    for (int i = 0; i <= m; i++)
        dp[i][0] = 1;
    for (int i = 0; i <= m; i++)
        dp[0][i] = 1;   
 
    for (int i = 1; i <= m; i++)
        for (int j = 1; j <= n; j++)
            dp[i][j] = dp[i - 1][j] +
                       dp[i - 1][j - 1] +
                       dp[i][j - 1];
 
    return dp[m][n];
}
 
// Driven Program
int main()
{
    int n = 3, m = 4;
    cout << dealnnoy(n, m) << endl;
    return 0;
}

Java

// Java Program of finding nth Delannoy Number.
 
import java.io.*;
 
class GFG {
     
    // Return the nth Delannoy Number.
    static int dealnnoy(int n, int m)
    {
        int dp[][]=new int[m + 1][n + 1];
      
        // Base cases
        for (int i = 0; i <= m; i++)
            dp[i][0] = 1;
        for (int i = 0; i < m; i++)
            dp[0][i] = 1;   
      
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                dp[i][j] = dp[i - 1][j] +
                           dp[i - 1][j - 1] +
                           dp[i][j - 1];
      
        return dp[m][n];
    }
      
    // Driven Program
    public static void main(String args[])
    {
        int n = 3, m = 4;
        System.out.println(dealnnoy(n, m));
         
    }
}
 
// This code is contributed by Nikita Tiwari.

Python3

# Python3 Program for finding nth
# Delannoy Number.
 
# Return the nth Delannoy Number.
def dealnnoy (n, m):
    dp = [[0 for x in range(n+1)] for x in range(m+1)]
 
    # Base cases
    for i in range(m):
        dp[0][i] = 1
     
    for i in range(1, m + 1):
        dp[i][0] = 1
 
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            dp[i][j] = dp[i - 1][j] + dp[i - 1][j - 1] + dp[i][j - 1];
 
    return dp[m][n]
 
# Driven code
n = 3
m = 4
print(dealnnoy(n, m))
 
# This code is contributed by "rishabh_jain".

C#

// C# Program of finding nth Delannoy Number.
using System;
 
class GFG {
 
    // Return the nth Delannoy Number.
    static int dealnnoy(int n, int m)
    {
         
        int[, ] dp = new int[m + 1, n + 1];
 
        // Base cases
        for (int i = 0; i <= m; i++)
            dp[i, 0] = 1;
        for (int i = 0; i < m; i++)
            dp[0, i] = 1;
 
        for (int i = 1; i <= m; i++)
            for (int j = 1; j <= n; j++)
                dp[i, j] = dp[i - 1, j]
                     + dp[i - 1, j - 1]
                         + dp[i, j - 1];
 
        return dp[m, n];
    }
 
    // Driven Program
    public static void Main()
    {
        int n = 3, m = 4;
         
        Console.WriteLine(dealnnoy(n, m));
    }
}
 
// This code is contributed by vt_m.

的PHP


输出 :

129