Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Maximum points collected by two persons allowed to meet once in C++
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
#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
Advertisements