📜  通过遵循给定的路径最大化矩阵和

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

给定一个由正整数组成的二维矩阵mat[][] ,任务是找到如果我们必须从mat[0][0开始到单元格mat[0][N – 1] 时可以达到的最大分数] 。我们必须分两个阶段覆盖矩阵:

  1. 阶段 1:如果我们在单元格mat[i][j]那么我们只能转到单元格mat[i][j + 1]mat[i + 1][j]而不改变阶段,否则我们可以转到cell mat[i – 1][j]并切换到阶段 2。
  2. 阶段 2:如果我们在单元格mat[i][j]那么我们只能去单元格mat[i][j + 1]mat[i – 1][j]
    我们不能越界,阶段之间的切换最多会发生一次。

注意:我们可能能够访问我们两次切换相位的列的单元格。
例子:

先决条件:矩阵中从上到下的最大求和路径。
方法:这个问题可以使用动态规划解决假设我们在单元格mat[i][j] 处S是收缩因子。如果它是 0,那么我们处于阶段 1(增长阶段),否则我们处于阶段 2(收缩阶段)。因此, S只能取两个值。一旦我们向下一步,就设置S = 1
dp[i][j][S]将定义为如果我们从单元格mat[i][j]mat[0][N – 1]可以获得的最大分数。现在,让我们讨论我们可以采取的路径。
让我们假设我们在单元格mat[i][j]

  • 情况 1:S = 0 时,我们有 3 条可能的路径,
    1. 转到单元格mat[i + 1][j]
    2. 转到单元格mat[i][j + 1]
    3. 转到单元格mat[i – 1][j]并更新S = 1
  • 情况 2:S = 1 时,我们有两条可能的路径,
    1. 转到单元格mat[i – 1][j]
    2. 转到单元格mat[i][j + 1]

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define n 3
 
// To store the states of the DP
int dp[n][n][2];
bool v[n][n][2];
 
// Function to return the maximum
// of the three integers
int max(int a, int b, int c)
{
    int m = a;
    if (m < b)
        m = b;
    if (m < c)
        m = c;
    return m;
}
 
// Function to return the maximum score
int maxScore(int arr[][n], int i, int j, int s)
{
    // Base cases
    if (i > n - 1 || i < 0 || j > n - 1)
        return 0;
    if (i == 0 and j == n - 1)
        return arr[i][j];
 
    // If the state has already
    // been solved then return it
    if (v[i][j][s])
        return dp[i][j][s];
 
    // Marking the state as solved
    v[i][j][s] = 1;
 
    // Growing phase
    if (!s)
        dp[i][j][s] = arr[i][j] + max(maxScore(arr, i + 1, j, s),
                                      maxScore(arr, i, j + 1, s),
                                      maxScore(arr, i - 1, j, !s));
 
    // Shrinking phase
    else
        dp[i][j][s] = arr[i][j] + max(maxScore(arr, i - 1, j, s),
                                      maxScore(arr, i, j + 1, s));
 
    // Returning the solved state
    return dp[i][j][s];
}
 
// Driver code
int main()
{
    int arr[n][n] = { { 1, 1, 1 },
                      { 1, 5, 1 },
                      { 1, 1, 1 } };
 
    cout << maxScore(arr, 0, 0, 0);
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
 
    static int n = 3;
 
    // To store the states of the DP
    static int[][][] dp = new int[n][n][2];
    static boolean[][][] v = new boolean[n][n][2];
 
    // Function to return the maximum
    // of the three integers
    static int max(int a, int b, int c)
     
    {
        int m = a;
        if (m < b)
        {
            m = b;
        }
        if (m < c)
        {
            m = c;
        }
        return m;
    }
 
// Function to return the maximum score
    static int maxScore(int arr[][], int i, int j, int s)
    {
        // Base cases
        if (i > n - 1 || i < 0 || j > n - 1)
        {
            return 0;
        }
        if (i == 0 && j == n - 1)
        {
            return arr[i][j];
        }
 
        // If the state has already
        // been solved then return it
        if (v[i][j][s])
         
         
        {
            return dp[i][j][s];
        }
 
        // Marking the state as solved
        v[i][j][s] = true;
 
        // Growing phase
        if (s != 1)
        {
            dp[i][j][s] = arr[i][j] + Math.max(maxScore(arr, i + 1, j, s),
                                    Math.max(maxScore(arr, i, j + 1, s),
                                    maxScore(arr, i - 1, j, (s==1)?0:1)));
        } // Shrinking phase
        else
        {
            dp[i][j][s] = arr[i][j] + Math.max(maxScore(arr, i - 1, j, s),
                    maxScore(arr, i, j + 1, s));
        }
 
        // Returning the solved state
        return dp[i][j][s];
    }
 
    // Driver code
    public static void main(String args[])
    {
        int arr[][] = {{1, 1, 1},
        {1, 5, 1},
        {1, 1, 1}};
 
        System.out.println(maxScore(arr, 0, 0, 0));
 
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python3 implementation of the approach
import numpy as np
 
n = 3
 
# To store the states of the DP
dp = np.zeros((n,n,2));
v = np.zeros((n,n,2));
 
# Function to return the maximum
# of the three integers
def max_three(a, b, c) :
 
    m = a;
    if (m < b) :
        m = b;
         
    if (m < c) :
        m = c;
         
    return m;
 
 
# Function to return the maximum score
def maxScore(arr, i, j, s) :
 
    # Base cases
    if (i > n - 1 or i < 0 or j > n - 1) :
        return 0;
         
    if (i == 0 and j == n - 1) :
        return arr[i][j];
 
    # If the state has already
    # been solved then return it
    if (v[i][j][s]) :
        return dp[i][j][s];
 
    # Marking the state as solved
    v[i][j][s] = 1;
 
    # Growing phase
    if (not bool(s)) :
        dp[i][j][s] = arr[i][j] + max_three(maxScore(arr, i + 1, j, s),
                                    maxScore(arr, i, j + 1, s),
                                    maxScore(arr, i - 1, j, not bool(s)));
 
    # Shrinking phase
    else :
        dp[i][j][s] = arr[i][j] + max(maxScore(arr, i - 1, j, s),
                                    maxScore(arr, i, j + 1, s));
 
    # Returning the solved state
    return dp[i][j][s];
 
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ [ 1, 1, 1 ],
                    [ 1, 5, 1 ],
                    [ 1, 1, 1 ] ,
                    ];
 
    print(maxScore(arr, 0, 0, 0));
     
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    static int n = 3;
     
    // To store the states of the DP
    static int[,,] dp = new int[n,n,2];
    static bool[,,] v = new bool[n,n,2];
 
    // Function to return the maximum
    // of the three integers
    static int max(int a, int b, int c)
     
    {
        int m = a;
        if (m < b)
        {
            m = b;
        }
        if (m < c)
        {
            m = c;
        }
        return m;
    }
 
    // Function to return the maximum score
    static int maxScore(int [,]arr, int i, int j, int s)
    {
        // Base cases
        if (i > n - 1 || i < 0 || j > n - 1)
        {
            return 0;
        }
        if ((i == 0) && (j == (n - 1)))
        {
            return arr[i, j];
        }
 
        // If the state has already
        // been solved then return it
        if (v[i, j, s])
         
         
        {
            return dp[i, j, s];
        }
 
        // Marking the state as solved
        v[i, j, s] = true;
 
        // Growing phase
        if (s != 1)
        {
            dp[i,j,s] = arr[i,j] + Math.Max(maxScore(arr, i + 1, j, s),
                                    Math.Max(maxScore(arr, i, j + 1, s),
                                    maxScore(arr, i - 1, j, (s==1)?0:1)));
        } // Shrinking phase
        else
        {
            dp[i,j,s] = arr[i,j] + Math.Max(maxScore(arr, i - 1, j, s),
                    maxScore(arr, i, j + 1, s));
        }
 
        // Returning the solved state
        return dp[i, j, s];
    }
 
    // Driver code
    static public void Main ()
    {
        int [,]arr = {{1, 1, 1},
        {1, 5, 1},
        {1, 1, 1}};
 
        Console.WriteLine(maxScore(arr, 0, 0, 0));
    }
}
 
/* This code contributed by @Tushil..... */


Javascript


输出:
15

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