📌  相关文章
📜  到达矩阵中给定像元的所有路径的成本总和

📅  最后修改于: 2021-04-23 05:24:45             🧑  作者: Mango

给定一个矩阵grid [] []以及两个整数MN ,任务是通过向下或向右移动一个像元来找到从(0,0)(M,N)的所有可能路径的成本之和。每条路径的成本定义为该路径中访问的像元值的总和。

例子:

方法:这个想法是找到矩阵中每个单元达到(M,N)的贡献,即每个ij的贡献,其中0 <= i <= M并且0 <= j <= N.
下图说明了每个单元格对通过相应单元格从(0,0)到(M,N)的所有路径的贡献:

下面是上述方法的实现:

C++
// C++ implementation to find the
// sum of cost of all paths
// to reach (M, N)
  
#include 
using namespace std;
  
const int Col = 3;
int fact(int n);
  
// Function for computing
// combination
int nCr(int n, int r)
{
    return fact(n) / (fact(r)
                      * fact(n - r));
}
  
// Function to find the
// factorial of N
int fact(int n)
{
    int res = 1;
  
    // Loop to find the factorial
    // of a given number
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
  
// Function for coumputing the
// sum of all path cost
int sumPathCost(int grid[][Col],
                int m, int n)
{
    int sum = 0, count;
  
    // Loop to find the contribution
    // of each (i, j) in the all possible
    // ways
    for (int i = 0; i <= m; i++) {
        for (int j = 0; j <= n; j++) {
  
            // Count number of
            // times (i, j) visited
            count
                = nCr(i + j, i)
                  * nCr(m + n - i - j, m - i);
  
            // Add the contribution of
            // grid[i][j] in the result
            sum += count * grid[i][j];
        }
    }
    return sum;
}
  
// Driver Code
int main()
{
  
    int m = 2;
    int n = 2;
    int grid[][Col] = { { 1, 2, 3 },
                        { 4, 5, 6 },
                        { 7, 8, 9 } };
  
    // Function Call
    cout << sumPathCost(grid, m, n);
    return 0;
}


Java
// Java implementation to find the
// sum of cost of all paths
// to reach (M, N)
import java.util.*;
  
class GFG{
  
static int Col = 3;
  
// Function for computing
// combination
static int nCr(int n, int r)
{
    return fact(n) / (fact(r) * 
                      fact(n - r));
}
  
// Function to find the
// factorial of N
static int fact(int n)
{
    int res = 1;
  
    // Loop to find the factorial
    // of a given number
    for(int i = 2; i <= n; i++)
       res = res * i;
    return res;
}
  
// Function for coumputing the
// sum of all path cost
static int sumPathCost(int grid[][],
                       int m, int n)
{
    int sum = 0, count;
  
    // Loop to find the contribution
    // of each (i, j) in the all possible
    // ways
    for(int i = 0; i <= m; i++)
    {
       for(int j = 0; j <= n; j++)
       {
            
          // Count number of
          // times (i, j) visited
          count = nCr(i + j, i) * 
                  nCr(m + n - i - j, m - i);
            
          // Add the contribution of
          // grid[i][j] in the result
          sum += count * grid[i][j];
       }
    }
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
    int m = 2;
    int n = 2;
    int grid[][] = { { 1, 2, 3 },
                     { 4, 5, 6 },
                     { 7, 8, 9 } };
  
    // Function Call
    System.out.println(sumPathCost(grid, m, n));
}
}
  
// This code is contributed by offbeat


Python3
# Python3 implementation to find the sum 
# of cost of all paths to reach (M, N)
  
Col = 3;
  
# Function for computing
# combination
def nCr(n, r):
      
    return fact(n) / (fact(r) *
                      fact(n - r));
  
# Function to find the
# factorial of N
def fact(n):
      
    res = 1;
  
    # Loop to find the factorial
    # of a given number
    for i in range(2, n + 1):
        res = res * i;
    return res;
  
# Function for coumputing the
# sum of all path cost
def sumPathCost(grid, m, n):
      
    sum = 0;
    count = 0;
  
    # Loop to find the contribution
    # of each (i, j) in the all possible
    # ways
    for i in range(0, m + 1):
        for j in range(0, n + 1):
              
            # Count number of
            # times (i, j) visited
            count = (nCr(i + j, i) * 
                     nCr(m + n - i - j, m - i));
  
            # Add the contribution of
            # grid[i][j] in the result
            sum += count * grid[i][j];
  
    return sum;
  
# Driver code
if __name__ == '__main__':
      
    m = 2;
    n = 2;
    grid = [ [ 1, 2, 3 ], 
             [ 4, 5, 6 ],
             [ 7, 8, 9 ] ];
  
    # Function Call
    print(int(sumPathCost(grid, m, n)));
  
# This code is contributed by 29AjayKumar


C#
// C# implementation to find the
// sum of cost of all paths
// to reach (M, N)
using System;
  
class GFG{
  
// Function for computing
// combination
static int nCr(int n, int r)
{
    return fact(n) / (fact(r) * 
                      fact(n - r));
}
  
// Function to find the
// factorial of N
static int fact(int n)
{
    int res = 1;
  
    // Loop to find the factorial
    // of a given number
    for(int i = 2; i <= n; i++)
       res = res * i;
    return res;
}
  
// Function for coumputing the
// sum of all path cost
static int sumPathCost(int [,]grid,
                       int m, int n)
{
    int sum = 0, count;
  
    // Loop to find the contribution
    // of each (i, j) in the all possible
    // ways
    for(int i = 0; i <= m; i++)
    {
       for(int j = 0; j <= n; j++)
       {
             
          // Count number of
          // times (i, j) visited
          count = nCr(i + j, i) * 
                  nCr(m + n - i - j, m - i);
                    
          // Add the contribution of
          // grid[i][j] in the result
          sum += count * grid[i, j];
       }
    }
    return sum;
}
  
// Driver code
public static void Main()
{
    int m = 2;
    int n = 2;
    int [, ]grid = { { 1, 2, 3 },
                     { 4, 5, 6 },
                     { 7, 8, 9 } };
  
    // Function Call
    Console.Write(sumPathCost(grid, m, n));
}
}
  
// This code is contributed by Code_Mech


输出:
150