En matemáticas, un número de Delannoy D describe el número de caminos desde la esquina suroeste (0, 0) de una cuadrícula rectangular hasta la esquina noreste (m, n), usando solo pasos hacia el norte, noreste o este.
Por ejemplo, D(3, 3) es igual a 63.
El número de Delannoy se puede calcular mediante:
El número de Delannoy se puede utilizar para encontrar:
- Cuenta el número de alineaciones globales de dos secuencias de longitud m y n.
- Número de puntos en una red de enteros de dimensión m que están como máximo a n pasos del origen.
- En los autómatas celulares, el número de células en una vecindad de von Neumann m-dimensional de radio n.
- Número de celdas en una superficie de una vecindad de von Neumann m-dimensional de radio n.
Ejemplos:
Input : n = 3, m = 3 Output : 63 Input : n = 4, m = 5 Output : 681
A continuación se muestra la implementación de encontrar el número de Delannoy:
C++
// CPP Program of finding nth Delannoy Number. #include <bits/stdc++.h> using namespace std; // Return the nth Delannoy Number. int Delannoy(int n, int m) { // Base case if (m == 0 || n == 0) return 1; // Recursive step. return Delannoy(m - 1, n) + Delannoy(m - 1, n - 1) + Delannoy(m, n - 1); } // Driven Program int main() { int n = 3, m = 4; cout << Delannoy(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 Delannoy(int n, int m) { // Base case if (m == 0 || n == 0) return 1; // Recursive step. return Delannoy(m - 1, n) + Delannoy(m - 1, n - 1) + Delannoy(m, n - 1); } // driver function public static void main(String args[]){ int n = 3, m = 4; System.out.println(Delannoy(n, m)); } } /* This code is contributed by Sagar Shukla. */
Python3
# Python3 Program for finding # nth Delannoy Number. # Return the nth Delannoy Number. def Delannoy(n, m): # Base case if (m == 0 or n == 0) : return 1 # Recursive step. return Delannoy(m - 1, n) + Delannoy(m - 1, n - 1) + Delannoy(m, n - 1) # Driven code n = 3 m = 4; print( Delannoy(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 Delannoy(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
<?php // PHP Program of finding nth // Delannoy Number. // Return the nth Delannoy Number. function Delannoy( $n, $m) { // Base case if ($m == 0 or $n == 0) return 1; // Recursive step. return Delannoy($m - 1, $n) + Delannoy($m - 1, $n - 1) + Delannoy($m, $n - 1); } // Driver Code $n = 3; $m = 4; echo Delannoy($n, $m); // This code is contributed by anuj_67. ?>
Javascript
<script> // javascript Program for finding nth Delannoy Number. // Return the nth Delannoy Number. function Delannoy(n, m) { // Base case if (m == 0 || n == 0) return 1; // Recursive step. return Delannoy(m - 1, n) + Delannoy(m - 1, n - 1) + Delannoy(m, n - 1); } // Driver code let n = 3, m = 4; document.write(Delannoy(n, m)); // This code is contributed by susmitakundugoaldanga. </script>
Producción:
129
A continuación se muestra el programa de programación dinámica para encontrar el n-ésimo número de Delannoy:
C++
// CPP Program of finding nth Delannoy Number. #include <bits/stdc++.h> using namespace std; // Return the nth Delannoy Number. int Delannoy(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 << Delannoy(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 Delannoy(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(Delannoy(n, m)); } } // This code is contributed by Nikita Tiwari.
Python3
# Python3 Program for finding nth # Delannoy Number. # Return the nth Delannoy Number. def Delannoy (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(Delannoy(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 Delannoy(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(Delannoy(n, m)); } } // This code is contributed by vt_m.
PHP
<?php // PHP Program of finding // nth Delannoy Number. // Return the nth Delannoy Number. function Delannoy($n, $m) { $dp[$m + 1][$n + 1] = 0; // Base cases for ($i = 0; $i <= $m; $i++) $dp[$i][0] = 1; for ( $i = 0; $i <= $m; $i++) $dp[0][$i] = 1; for ($i = 1; $i <= $m; $i++) for ($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 Code $n = 3; $m = 4; echo Delannoy($n, $m) ; // This code is contributed by SanjuTomar ?>
Javascript
<script> // Javascript Program of finding // nth Delannoy Number. // Return the nth Delannoy Number. function Delannoy(n, m) { var dp = Array.from(Array(m+1), () => Array(n+1)); // Base cases for (var i = 0; i <= m; i++) dp[i][0] = 1; for (var i = 0; i <= m; i++) dp[0][i] = 1; for (var i = 1; i <= m; i++) for (var 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 var n = 3, m = 4; document.write( Delannoy(n, m)); </script>
Producción :
129