📌  相关文章
📜  计算距离等于曼哈顿距离的路径

📅  最后修改于: 2021-05-06 21:00:39             🧑  作者: Mango

在二维坐标系中给定两个点(x1,y1)(x2,y2) 。任务是计算所有距离等于两个给定点之间的曼哈顿距离的所有路径。

例子:

方法: (x1,y1)(x2,y2)点之间的曼哈顿距离为abs(x1 – x2)+ abs(y1 – y2)
abs(x1 – x2)= mabs(y1 – y2)= n
每条距离等于曼哈顿距离的路径将始终具有m + n条边, m条水平边和n条垂直边。因此,这是基于组形成的组合学的基本情况。这背后的想法是将(m + n)种不同事物分为两类的方式数量,一组包含m个项目,另一组包含n个项目,这由m + n C n给出,即(m + n) ! /米! * n!

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ll long long int
  
// Function to return the value of nCk
ll binomialCoeff(int n, int k)
{
    ll res = 1;
  
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
  
    // Calculate value of
    // [n * (n-1) *---* (n-k+1)] / 
    // [k * (k-1) *---* 1]
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
  
    return res;
}
  
// Function to return the number of paths
ll countPaths(int x1, int y1, int x2, int y2)
{
  
    // Difference between the 'x' 
    // coordinates of the given points
    int m = abs(x1 - x2);
  
    // Difference between the 'y' 
    // coordinates of the given points
    int n = abs(y1 - y2);
  
    return (binomialCoeff(m + n, n));
}
  
// Driver code
int main()
{
    int x1 = 2, y1 = 3, x2 = 4, y2 = 5;
    cout << countPaths(x1, y1, x2, y2);
    return 0;
}


Java
// Java implementation of the approach 
class GfG 
{
      
//static long ll long long int 
  
// Function to return the value of nCk 
static long binomialCoeff(int n, int k) 
{ 
    long res = 1; 
  
    // Since C(n, k) = C(n, n-k) 
    if (k > n - k) 
        k = n - k; 
  
    // Calculate value of 
    // [n * (n-1) *---* (n-k+1)] / 
    // [k * (k-1) *---* 1] 
    for (int i = 0; i < k; ++i)
    { 
        res *= (n - i); 
        res /= (i + 1); 
    } 
  
    return res; 
} 
  
// Function to return the number of paths 
static long countPaths(int x1, int y1, int x2, int y2) 
{ 
  
    // Difference between the 'x' 
    // coordinates of the given points 
    int m = Math.abs(x1 - x2); 
  
    // Difference between the 'y' 
    // coordinates of the given points 
    int n = Math.abs(y1 - y2); 
  
    return (binomialCoeff(m + n, n)); 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    int x1 = 2, y1 = 3, x2 = 4, y2 = 5; 
    System.out.println(countPaths(x1, y1, x2, y2)); 
}
} 
  
// This code is contributed by 
// Prerna Saini.


Python3
# Python3 implementation of the approach
  
# Function to return the value of nCk
def binomialCoeff(n, k):
  
    res = 1
  
    # Since C(n, k) = C(n, n-k)
    if (k > n - k):
        k = n - k
  
    # Calculate value of
    # [n * (n-1) *---* (n-k+1)] / 
    # [k * (k-1) *---* 1]
    for i in range(k):
        res *= (n - i)
        res //= (i + 1)
  
    return res
  
# Function to return the number
# of paths
def countPaths(x1, y1, x2, y2):
  
    # Difference between the 'x' 
    # coordinates of the given points
    m = abs(x1 - x2)
  
    # Difference between the 'y' 
    # coordinates of the given points
    n = abs(y1 - y2)
  
    return (binomialCoeff(m + n, n))
  
# Driver code
x1, y1, x2, y2 = 2, 3, 4, 5
print(countPaths(x1, y1, x2, y2))
  
# This code is contributed 
# by Mohit Kumar


C#
// C# implementation of the approach 
using System;
  
class GFG 
{
  
// Function to return the value of nCk 
static long binomialCoeff(int n, int k) 
{ 
    long res = 1; 
  
    // Since C(n, k) = C(n, n-k) 
    if (k > n - k) 
        k = n - k; 
  
    // Calculate value of 
    // [n * (n-1) *---* (n-k+1)] / 
    // [k * (k-1) *---* 1] 
    for (int i = 0; i < k; ++i)
    { 
        res *= (n - i); 
        res /= (i + 1); 
    } 
  
    return res; 
} 
  
// Function to return the number of paths 
static long countPaths(int x1, int y1,
                       int x2, int y2) 
{ 
  
    // Difference between the 'x' 
    // coordinates of the given points 
    int m = Math.Abs(x1 - x2); 
  
    // Difference between the 'y' 
    // coordinates of the given points 
    int n = Math.Abs(y1 - y2); 
  
    return (binomialCoeff(m + n, n)); 
} 
  
// Driver code 
public static void Main() 
{ 
    int x1 = 2, y1 = 3, x2 = 4, y2 = 5; 
    Console.Write(countPaths(x1, y1, x2, y2)); 
}
} 
  
// This code is contributed by 
// Akanksha Rai


PHP
 $n - $k) 
        $k = $n - $k; 
  
    // Calculate value of 
    // [n * (n-1) *---* (n-k+1)] / 
    // [k * (k-1) *---* 1] 
    for ($i = 0; $i < $k; ++$i)
    { 
        $res *= ($n - $i); 
        $res /= ($i + 1); 
    } 
  
    return $res; 
} 
  
// Function to return the number of paths 
function countPaths($x1, $y1, $x2, $y2) 
{ 
  
    // Difference between the 'x' 
    // coordinates of the given points 
    $m =abs($x1 - $x2); 
  
    // Difference between the 'y' 
    // coordinates of the given points 
    $n = abs($y1 - $y2); 
  
    return (binomialCoeff($m + $n, $n)); 
} 
  
// Driver code 
{ 
    $x1 = 2; $y1 = 3; $x2 = 4; $y2 = 5; 
    echo(countPaths($x1, $y1, $x2, $y2)); 
}
  
// This code is contributed by 
// Code_Mech.


输出:
6