Open In App
Related Articles

Maximum points collected by two persons allowed to meet once

Improve Article
Improve
Save Article
Save
Like Article
Like

Given a 2-D matrix A[N][M] where A[i][j] denotes the points available on this cell. Two persons, P1 and P2, start from two corners of this matrix. P1 starts from top left and needs to reach bottom right. On the other hand, P2 starts bottom left and needs to reach top right. P1 can move right and down. P2 can right and up. As they visit a cell, points A[i][j] are added to their total. The task is to maximize the sum of total points collected by both of them under the condition that they shall meet only once and the cost of this cell shall not be included in either of their total.

Examples: 

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 needs to move from top left (0, 0) to bottom right (n-1, m-1). P1 can move right and down, i.e., from A[i][j] to A[i][j+1] or A[i+1][j] 
P2 needs to move from bottom left (n-1, 0) to top right (0, m-1). P2 can move right and up, i.e., from A[i][j] to A[i][j+1] or A[i-1][j].

The idea is to consider every point as a meeting point and find maximum of all meeting points. When we consider a point A[i][j] as meeting point, we need to have following four values to find maximum total points collected when A[i][j] is meeting point.

  1. Points collected by P1 from (0, 0) to (i, j). 
  2. Points collected by P1 from (i, j) to (n-1, m-1). 
  3. Points collected by P2 from (n-1, 0) to (i, j). 
  4. Points collected by P2 from (i, j) to (0, m-1).

We can compute above four values using Dynamic Programming. Once we have above four values for every point, we can find maximum points at every meeting point.

For every meeting points, there are two possible values to reach it and leave it as we are allowed to have only one meeting point. 

  1. P1 reaches it through a right move and p2 through a up move and they leave same way also. 
  2. P1 reaches it through a down move and p2 through a right move and they leave same way also. 

We take maximum of above two values to find optimal points at this meeting point.

Finally, we return maximum of all meeting points. 

Implementation:

C++




// C++ program to find maximum points that can
// be collected by two persons in a matrix.
#include<bits/stdc++.h>
#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 journey 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<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 = max(ans, max(op1, op2));
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    //input the calories burnt matrix
    int A[][M] = {{100, 100, 100},
                  {100, 1, 100},
                  {100, 100, 100}};
    cout << "Max Points : " << findMaxPoints(A);
    return 0;
}


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 journey 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 journey 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 journey 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




<script>
 
// Javascript program to find
// maximum points that can be
// collected by two persons in a matrix.
let M = 3;
let N = 3;
 
function findMaxPoints(A)
{
     
    // To store points collected by Person P1
    // when he/she begins journey from start and
    // from end.
    let P1S = new Array(M + 2);
    let P1E = new Array(M + 2);
  
    // To store points collected by Person P2
    // when he/she begins journey from start and
    // from end.
    let P2S = new Array(M + 2);
    let P2E = new Array(M + 2);
     
    for(let i = 0; i< M + 2; i++)
    {
        P1S[i] = new Array(N + 2);
        P1E[i] = new Array(N + 2);
        P2S[i] = new Array(N + 2);
        P2E[i] = new Array(N + 2);
        for(let j = 0; j < N + 2; j++)
        {
            P1S[i][j] = 0;
            P1E[i][j] = 0;
            P2S[i][j] = 0;
            P2E[i][j] = 0;
        }
    }
     
    // Table for P1's journey from
    // start to meeting cell
    for(let i = 1; i <= N; i++)
        for(let 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(let i = N; i >= 1; i--)
        for(let 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(let i = N; i >= 1; i--)
        for(let 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(let i = 1; i <= N; i++)
        for(let 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)
    let ans = 0;
    for(let i = 2; i < N; i++)
    {
        for(let j = 2; j < M; j++)
        {
            let op1 = P1S[i][j - 1] + P1E[i][j + 1] +
                         P2S[i + 1][j] + P2E[i - 1][j];
            let 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
let A = [ [ 100, 100, 100 ],
          [ 100, 1, 100 ],
          [ 100, 100, 100 ] ];
document.write("Max Points : " +
               findMaxPoints(A));
                
// This code is contributed by rag2127
 
</script>


Output

Max Points : 800

Time Complexity : O(N * M) 
Auxiliary Space : O(N * M)

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 


Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Last Updated : 07 Jul, 2022
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials