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

📅  最后修改于: 2021-09-22 10:27:10             🧑  作者: Mango

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

方法:思路是求矩阵的每个单元格对达到(M, N)的贡献,即每个ij的贡献,其中0 <= i <= M and 0 <= j <=否
下面是每个单元格对从 (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


Javascript


输出:
150

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程