📌  相关文章
📜  最小化从矩阵的左上角到达右下角的成本,每次移动都给定单独的成本

📅  最后修改于: 2022-05-13 01:56:04.266000             🧑  作者: Mango

最小化从矩阵的左上角到达右下角的成本,每次移动都给定单独的成本

给定一个整数N和三个N x N矩阵X[][]Y[][]Z[][] ,任务是计算从矩阵的左上角到达右下角所需的最小成本使用以下动作:

  • (i, j)移动到正确方向的单元格,这将花费X[i][j]
  • (i, j)向下移动到将花费Y[i][j]的单元格。
  • (i, j)沿对角线方向移动到将花费Z[i][j]的单元格,

例子:

方法:给定问题遵循与标准最小成本路径问题类似的结构。可以使用动态规划来解决。到达(i, j)的路径必须通过以下 3 个单元之一: (i-1, j-1)(i-1, j)(i, j-1) 。因此,到达(i, j)的最小成本可以写成以下关系:

因此,创建一个二维数组dp[][] ,其中dp[i – 1][j – 1]存储从(0, 0)到达单元格(i, j ) 并填充dp[][ ]以自下而上的方式排列。存储在dp[N – 1][N – 1]的值是所需的答案。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
#include 
#define R 3
#define C 3
using namespace std;
 
// A utility function that returns
// minimum of 3 integers
int min(int x, int y, int z)
{
    if (x < y)
        return (x < z) ? x : z;
    else
        return (y < z) ? y : z;
}
 
// Function to find the min cost path
int minCost(int X[R][C], int Y[R][C],
            int Z[R][C], int n)
{
    int i, j;
 
    // 2D array to store DP states
    int dp[R][C];
 
    dp[0][0] = 0;
 
    // Initialize first row of dp array
    for (j = 1; j < n; j++)
        dp[0][j] = dp[0][j - 1] + X[0][j - 1];
 
    // Initialize first column of dp array
    for (i = 1; i < n; i++)
        dp[i][0] = dp[i - 1][0] + Y[i - 1][0];
 
    // Construct rest of the dp array
    for (i = 1; i < n; i++)
        for (j = 1; j < n; j++)
 
            // Calculating the minimum over
            // the downward, right or the
            // diagonal movement
            dp[i][j]
                = min(
                    dp[i - 1][j - 1] + Z[i - 1][j - 1],
                    dp[i - 1][j] + Y[i - 1][j],
                    dp[i][j - 1] + X[i][j - 1]);
 
    // Return answer
    return dp[n - 1][n - 1];
}
 
// Driver code
int main()
{
    int N = 3;
    int X[R][C] = { { 1, 2, 1 },
                    { 3, 4, 5 },
                    { 1, 1, 1 } };
    int Y[R][C] = { { 2, 3, 4 },
                    { 1, 3, 1 },
                    { 2, 1, 1 } };
    int Z[R][C] = { { 1, 8, 9 },
                    { 1, 4, 5 },
                    { 6, 5, 4 } };
 
    cout << minCost(X, Y, Z, 3);
 
    return 0;
}


Java
// Java program of the above approach
 
//include 
 
import java.util.*;
class GFG{
  static final int R = 3;
  static final int C = 3;
 
  // A utility function that returns
  // minimum of 3 integers
  static int min(int x, int y, int z)
  {
    if (x < y)
      return (x < z) ? x : z;
    else
      return (y < z) ? y : z;
  }
 
  // Function to find the min cost path
  static int minCost(int X[][], int Y[][],
                     int Z[][], int n)
  {
    int i, j;
 
    // 2D array to store DP states
    int dp[][] = new int[R][C];
 
    dp[0][0] = 0;
 
    // Initialize first row of dp array
    for (j = 1; j < n; j++)
      dp[0][j] = dp[0][j - 1] + X[0][j - 1];
 
    // Initialize first column of dp array
    for (i = 1; i < n; i++)
      dp[i][0] = dp[i - 1][0] + Y[i - 1][0];
 
    // Conrest of the dp array
    for (i = 1; i < n; i++)
      for (j = 1; j < n; j++)
 
        // Calculating the minimum over
        // the downward, right or the
        // diagonal movement
        dp[i][j]
        = Math.min(Math.min(
          dp[i - 1][j - 1] + Z[i - 1][j - 1],
          dp[i - 1][j] + Y[i - 1][j]),
                   dp[i][j - 1] + X[i][j - 1]);
 
    // Return answer
    return dp[n - 1][n - 1];
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int N = 3;
    int X[][] = { { 1, 2, 1 },
                 { 3, 4, 5 },
                 { 1, 1, 1 } };
    int Y[][] = { { 2, 3, 4 },
                 { 1, 3, 1 },
                 { 2, 1, 1 } };
    int Z[][] = { { 1, 8, 9 },
                 { 1, 4, 5 },
                 { 6, 5, 4 } };
 
    System.out.print(minCost(X, Y, Z, 3));
 
  }
}
 
// This code is contributed by shikhasingrajput


Python3
# Python code for the above approach
R = 3
C = 3
 
# A utility function that returns
# minimum of 3 integers
def min(x, y, z):
    if (x < y):
        return x if (x < z) else z;
    else:
        return y if (y < z) else z;
 
# Function to find the min cost path
def minCost(X, Y, Z, n):
    i = None
    j = None
 
    # 2D array to store DP states
    dp = [0] * R;
 
    for i in range(len(dp)):
        dp[i] = [0] * C
 
    dp[0][0] = 0;
 
    # Initialize first row of dp array
    for j in range(1, n):
        dp[0][j] = dp[0][j - 1] + X[0][j - 1];
 
    # Initialize first column of dp array
    for i in range(1, n):
        dp[i][0] = dp[i - 1][0] + Y[i - 1][0];
 
    # Construct rest of the dp array
    for i in range(1, n):
        for j in range(1, n):
 
            # Calculating the minimum over
            # the downward, right or the
            # diagonal movement
            dp[i][j] = min(
                    dp[i - 1][j - 1] + Z[i - 1][j - 1],
                    dp[i - 1][j] + Y[i - 1][j],
                    dp[i][j - 1] + X[i][j - 1]);
 
    # Return answer
    return dp[n - 1][n - 1];
 
# Driver code
N = 3;
X = [[1, 2, 1], [3, 4, 5], [1, 1, 1]];
Y = [[2, 3, 4], [1, 3, 1], [2, 1, 1]];
Z = [[1, 8, 9], [1, 4, 5], [6, 5, 4]];
print(minCost(X, Y, Z, 3));
 
  # This code is contributed by gfgking


C#
// C# program of the above approach
using System;
class GFG {
 
  static int R = 3;
  static int C = 3;
 
  // A utility function that returns
  // minimum of 3 integers
  static int min(int x, int y, int z)
  {
    if (x < y)
      return (x < z) ? x : z;
    else
      return (y < z) ? y : z;
  }
 
  // Function to find the min cost path
  static int minCost(int [,]X, int [,]Y,
                     int [,]Z, int n)
  {
    int i, j;
 
    // 2D array to store DP states
    int [,]dp = new int[R, C];
 
    dp[0, 0] = 0;
 
    // Initialize first row of dp array
    for (j = 1; j < n; j++)
      dp[0, j] = dp[0, j - 1] + X[0, j - 1];
 
    // Initialize first column of dp array
    for (i = 1; i < n; i++)
      dp[i, 0] = dp[i - 1, 0] + Y[i - 1, 0];
 
    // Conrest of the dp array
    for (i = 1; i < n; i++)
      for (j = 1; j < n; j++)
 
        // Calculating the minimum over
        // the downward, right or the
        // diagonal movement
        dp[i, j]
        = Math.Min(Math.Min(
          dp[i - 1, j - 1] + Z[i - 1, j - 1],
          dp[i - 1, j] + Y[i - 1, j]),
                   dp[i, j - 1] + X[i, j - 1]);
 
    // Return answer
    return dp[n - 1, n - 1];
  }
 
  // Driver code
  public static void Main()
  {
    int N = 3;
    int [,]X = { { 1, 2, 1 },
                { 3, 4, 5 },
                { 1, 1, 1 } };
    int [,]Y = { { 2, 3, 4 },
                { 1, 3, 1 },
                { 2, 1, 1 } };
    int [,]Z = { { 1, 8, 9 },
                { 1, 4, 5 },
                { 6, 5, 4 } };
 
    Console.Write(minCost(X, Y, Z, 3));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
4

时间复杂度: O(N 2 )
辅助空间: O(N 2 )