📜  允许两人见面一次的最高积分

📅  最后修改于: 2021-09-17 07:42:09             🧑  作者: Mango

给定一个二维矩阵 A[N][M],其中 A[i][j] 表示该单元格上可用的点。两个人,P1 和 P2,从这个矩阵的两个角开始。 P1 从左上角开始,需要到达右下角。另一方面,P2 从左下角开始,需要到达右上角。 P1 可以左右移动。 P2可以对上。当他们访问一个单元格时,点 A[i][j] 被添加到他们的总数中。任务是在他们只相遇一次并且这个单元格的成本不包括在他们的总数中的条件下最大化他们收集的总点数之和。

例子:

Input : A[][] = { {100, 100, 100},
                   {100, 1, 100},
                   {100, 100, 100}};
Output : 800
P1 needs to go from (0,0) to (2,2)
P2 needs to go from (2,0) to (0,2)
Explanation: P1 goes through A[0][0]->A[0][1]->A[1][1]->
                             A[2][1]->A[2][2]. 
             P2 goes through A[2][0]->A[1][0]->A[1][1]->
                             A[1][2]->A[0][2].
They meet at A[1][1]. So total points collected: 
A[0][0] + A[0][1] + A[2][1] + A[2][2] + 
A[2][0] + A[1][0] + A[1][2] + A[0][2] = 800


Input : A[][] = {{100, 100, 100, 100},
                 {100, 100, 100, 100},
                 {100, 0, 100, 100},
                 {100, 100, 100, 100}};
Output : 1200

P1 需要从左上角 (0, 0) 移动到右下角 (n-1, m-1)。 P1可以左右移动,即从A[i][j]到A[i][j+1]或A[i+1][j]
P2 需要从左下角 (n-1, 0) 移动到右上角 (0, m-1)。 P2 可以向右和向上移动,即从 A[i][j] 到 A[i][j+1] 或 A[i-1][j]。

这个想法是将每个点视为一个交汇点,并找到所有交汇点中的最大值。当我们考虑一个点 A[i][j] 作为交汇点时,我们需要有以下四个值来找到当 A[i][j] 是交汇点时收集的最大总点数。
1) P1从(0, 0)到(i, j)收集的点。
2) P1从(i, j)到(n-1, m-1)收集的点。
3) P2从(n-1, 0)到(i, j)收集的点。
4) P2从(i, j)到(0, m-1)收集的点。
我们可以使用动态规划计算以上四个值。一旦我们对每个点都有以上四个值,我们就可以在每个交汇点找到最大点。

对于每个会面点,有两个可能的值到达和离开它,因为我们只允许有一个会面点。
1)P1通过向右移动到达它,p2通过向上移动到达它,他们也以同样的方式离开。
2)P1通过向下移动到达它,而p2通过向右移动到达它并且它们也以相同的方式离开。
我们取以上两个值的最大值来找到这个交汇点的最佳点。

最后,我们返回所有会面点的最大值。

C++
// C++ program to find maximum points that can
// be collected by two persons in a matrix.
#include
#define M 3
#define N 3
using namespace std;
 
int findMaxPoints(int A[][M])
{
    // To store points collected by Person P1
    // when he/she begins journy from start and
    // from end.
    int P1S[M+1][N+1], P1E[M+1][N+1];
    memset(P1S, 0, sizeof(P1S));
    memset(P1E, 0, sizeof(P1E));
 
    // To store points collected by Person P2
    // when he/she begins journey from start and
    // from end.
    int P2S[M+1][N+1], P2E[M+1][N+1];
    memset(P2S, 0, sizeof(P2S));
    memset(P2E, 0, sizeof(P2E));
 
    // Table for P1's journey from
    // start to meeting cell
    for (int i=1; i<=N; i++)
        for (int j=1; j<=M; j++)
            P1S[i][j] = max(P1S[i-1][j],
                  P1S[i][j-1]) + A[i-1][j-1];
 
    // Table for P1's journey from
    // end to meet cell
    for (int i=N; i>=1; i--)
        for (int j=M; j>=1; j--)
            P1E[i][j] = max(P1E[i+1][j],
                  P1E[i][j+1]) + A[i-1][j-1];
 
    // Table for P2's journey from start to meeting cell
    for (int i=N; i>=1; i--)
        for(int j=1; j<=M; j++)
            P2S[i][j] = max(P2S[i+1][j],
                  P2S[i][j-1]) + A[i-1][j-1];
 
    // Table for P2's journey from end to meeting cell
    for (int i=1; i<=N; i++)
        for (int j=M; j>=1; j--)
            P2E[i][j] = max(P2E[i-1][j],
                  P2E[i][j+1]) + A[i-1][j-1];
 
    // Now iterate over all meeting positions (i,j)
    int ans = 0;
    for (int i=2; i


Java
// Java program to find maximum points that can
// be collected by two persons in a matrix.
 
class GFG
{
    static final int M = 3;
    static final int N = 3 ;
static int findMaxPoints(int A[][])
{
    // To store points collected by Person P1
    // when he/she begins journy from start and
    // from end.
    int [][]P1S = new int[M + 2][N + 2];
    int [][]P1E = new int[M + 2][N + 2];
 
    // To store points collected by Person P2
    // when he/she begins journey from start and
    // from end.
    int [][]P2S = new int[M + 2][N + 2];
    int [][]P2E = new int[M + 2][N + 2];
 
    // Table for P1's journey from
    // start to meeting cell
    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= M; j++)
            P1S[i][j] = Math.max(P1S[i - 1][j],
                P1S[i][j - 1]) + A[i - 1][j - 1];
 
    // Table for P1's journey from
    // end to meet cell
    for (int i = N; i >= 1; i--)
        for (int j = M; j >= 1; j--)
            P1E[i][j] = Math.max(P1E[i + 1][j],
                P1E[i][j + 1]) + A[i - 1][j - 1];
 
    // Table for P2's journey from start to meeting cell
    for (int i = N; i >= 1; i--)
        for(int j = 1; j <= M; j++)
            P2S[i][j] = Math.max(P2S[i + 1][j],
                P2S[i][j - 1]) + A[i - 1][j - 1];
 
    // Table for P2's journey from end to meeting cell
    for (int i = 1; i <= N; i++)
        for (int j = M; j >= 1; j--)
            P2E[i][j] = Math.max(P2E[i - 1][j],
                P2E[i][j + 1]) + A[i - 1][j - 1];
 
    // Now iterate over all meeting positions (i, j)
    int ans = 0;
    for (int i = 2; i < N; i++)
    {
        for (int j = 2; j < M; j++)
        {
            int op1 = P1S[i][j - 1] + P1E[i][j + 1] +
                    P2S[i + 1][j] + P2E[i - 1][j];
            int op2 = P1S[i - 1][j] + P1E[i + 1][j] +
                    P2S[i][j - 1] + P2E[i][j + 1];
            ans = Math.max(ans, Math.max(op1, op2));
        }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    // input the calories burnt matrix
    int A[][] = {{100, 100, 100},
                {100, 1, 100},
                {100, 100, 100}};
    System.out.print("Max Points : " + findMaxPoints(A));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python program to find maximum points that can
# be collected by two persons in a matrix.
M = 3
N = 3
def findMaxPoints(A):
 
    # To store points collected by Person P1
    # when he/she begins journy from start and
    # from end.
    P1S = [[0 for i in range(N + 2)] for j in range(M + 2)]
    P1E = [[0 for i in range(N + 2)] for j in range(M + 2)]
     
    # To store points collected by Person P2
    # when he/she begins journey from start and
    # from end.
    P2S = [[0 for i in range(N + 2)] for j in range(M + 2)]
    P2E = [[0 for i in range(N + 2)] for j in range(M + 2)]
     
    # Table for P1's journey from
    # start to meeting cell
    for i in range(1, N + 1):
        for j in range(1,M + 1):
            P1S[i][j] = max(P1S[i - 1][j],
                        P1S[i][j - 1]) + A[i - 1][j - 1]
             
    # Table for P1's journey from
    # end to meet cell
    for i in range(N, 0, -1):
        for j in range(M, 0, -1):
            P1E[i][j] = max(P1E[i + 1][j],
                        P1E[i][j + 1]) + A[i - 1][j - 1]
             
    # Table for P2's journey from start to meeting cell
    for i in range(N, 0, -1):
        for j in range(1, M + 1):
            P2S[i][j] = max(P2S[i + 1][j],
                        P2S[i][j - 1]) + A[i - 1][j - 1]
             
    # Table for P2's journey from end to meeting cell
    for i in range(1, N + 1):
        for j in range(M, 0, -1):
            P2E[i][j] = max(P2E[i - 1][j],
                        P2E[i][j + 1]) + A[i - 1][j - 1]
             
    # Now iterate over all meeting positions (i,j)
    ans = 0
    for i in range(2, N):
        for j in range(2, M):
            op1 = P1S[i][j - 1] + P1E[i][j + 1] + \
                  P2S[i + 1][j] + P2E[i - 1][j]
            op2 = P1S[i - 1][j] + P1E[i + 1][j] + \
                  P2S[i][j - 1] + P2E[i][j + 1]
            ans = max(ans, max(op1, op2))
     
    return ans
 
# Driver code
# input the calories burnt matrix
A= [[100, 100, 100], [100, 1, 100], [100, 100, 100]]
print("Max Points : ", findMaxPoints(A))
 
# This code is contributed by shubhamsingh10


C#
// C# program to find maximum points that can
// be collected by two persons in a matrix.
using System;
 
class GFG
{
    static readonly int M = 3;
    static readonly int N = 3 ;
    static int findMaxPoints(int [,]A)
    {
        // To store points collected by Person P1
        // when he/she begins journy from start and
        // from end.
        int [,]P1S = new int[M + 2, N + 2];
        int [,]P1E = new int[M + 2, N + 2];
     
        // To store points collected by Person P2
        // when he/she begins journey from start and
        // from end.
        int [,]P2S = new int[M + 2, N + 2];
        int [,]P2E = new int[M + 2, N + 2];
     
        // Table for P1's journey from
        // start to meeting cell
        for (int i = 1; i <= N; i++)
            for (int j = 1; j <= M; j++)
                P1S[i, j] = Math.Max(P1S[i - 1, j],
                    P1S[i, j - 1]) + A[i - 1, j - 1];
     
        // Table for P1's journey from
        // end to meet cell
        for (int i = N; i >= 1; i--)
            for (int j = M; j >= 1; j--)
                P1E[i, j] = Math.Max(P1E[i + 1, j],
                    P1E[i, j + 1]) + A[i - 1, j - 1];
     
        // Table for P2's journey from start to meeting cell
        for (int i = N; i >= 1; i--)
            for(int j = 1; j <= M; j++)
                P2S[i, j] = Math.Max(P2S[i + 1, j],
                    P2S[i, j - 1]) + A[i - 1, j - 1];
     
        // Table for P2's journey from end to meeting cell
        for (int i = 1; i <= N; i++)
            for (int j = M; j >= 1; j--)
                P2E[i, j] = Math.Max(P2E[i - 1, j],
                    P2E[i, j + 1]) + A[i - 1, j - 1];
     
        // Now iterate over all meeting positions (i, j)
        int ans = 0;
        for (int i = 2; i < N; i++)
        {
            for (int j = 2; j < M; j++)
            {
                int op1 = P1S[i, j - 1] + P1E[i, j + 1] +
                        P2S[i + 1, j] + P2E[i - 1, j];
                int op2 = P1S[i - 1, j] + P1E[i + 1, j] +
                        P2S[i, j - 1] + P2E[i, j + 1];
                ans = Math.Max(ans, Math.Max(op1, op2));
            }
        }
        return ans;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // input the calories burnt matrix
        int [,]A = {{100, 100, 100},
                    {100, 1, 100},
                    {100, 100, 100}};
        Console.Write("Max Points : " + findMaxPoints(A));
    }
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

Max Points : 800

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

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