# Maximum points collected by two persons allowed to meet once in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

In this tutorial, we will be discussing a program to find maximum points collected by two persons allowed to meet once

For this we will be provided with a matrix with cells containing points. Our task is to find the path when two people starting from two corners meet such that they are having maximum points collected.

## Example

Live Demo

#include<bits/stdc++.h>
#define M 3
#define N 3
using namespace std;
int findMaxPoints(int A[][M]) {
//storing points
int P1S[M+1][N+1], P1E[M+1][N+1];
memset(P1S, 0, sizeof(P1S));
memset(P1E, 0, sizeof(P1E));
int P2S[M+1][N+1], P2E[M+1][N+1];
memset(P2S, 0, sizeof(P2S));
memset(P2E, 0, sizeof(P2E));
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];
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];
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];
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];
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;
}
int main() {
int A[][M] = {
{100, 100, 100},
{100, 1, 100},
{100, 100, 100}
};
cout << "Max Points : " << findMaxPoints(A);
return 0;
}

## Output

Max Points : 800
Updated on 09-Sep-2020 12:06:27