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

📅  最后修改于: 2021-04-27 09:40:14             🧑  作者: Mango

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

  1. 阶段1:如果我们位于单元格mat [i] [j],那么我们只能转到单元格mat [i] [j + 1]mat [i + 1] [j],而无需更改相位,否则我们可以转到电池垫[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时,我们有三种可能的路径,
    1. 转到单元格垫[i + 1] [j]
    2. 转到单元格垫[i] [j + 1]
    3. 转到单元格mat [i – 1] [j]并更新S = 1
  • 情况2:S = 1时,我们有两条可能的路径,
    1. 转到单元格垫[i – 1] [j]
    2. 转到单元格垫[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..... */


输出:
15