给定二维矩阵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
Python
# 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
输出:
Max Points : 800