基础的DP问题
#include <iostream>
#include <cmath>
#include <cstring>
const int MAX = 205;
int arr[MAX][MAX];
int dp[MAX][MAX];
int main()
{
int T, cas = 0;
std::cin >> T;
while (T--)
{
memset(arr, 0, sizeof(arr));
memset(dp, 0, sizeof(arr));
int n;
std::cin >> n;
for (int i = 1; i <= 2 * n -1; ++i)
{
if (i <= n)
{
for (int j = 1; j <= i; ++j)
{
std::cin >> arr[i][j];
}
}
else
{
for (int j = 1; j <= 2 * n - i; ++j)
{
std::cin >> arr[i][j];
}
}
}
dp[1][1] = arr[1][1];
//std::cout << dp[1][1] << std::endl;
for (int i = 2; i <= 2 * n - 1; ++i)
{
if (i <= n)
{
for (int j = 1; j <= i; ++j)
{
dp[i][j] = std::max(dp[i-1][j], dp[i - 1][j - 1]) + arr[i][j];
}
}
else
{
for (int j = 1; j <= 2 * n - i; ++j)
{
dp[i][j] = std::max(dp[i-1][j], dp[i - 1][j + 1]) + arr[i][j];
}
}
}
/*for (int i = 1; i <= 2 * n - 1; ++i)
{
if (i <= n)
{
for (int j = 1; j <= i; ++j)
{
std::cout << dp[i][j] << " ";
}
std::cout << '\n';
}
else
{
for (int j = 1; j <= 2 * n - i; ++j)
{
std::cout << dp[i][j] << " ";
}
std::cout << '\n';
}
}*/
std::cout << "Case " << ++cas << ": ";
std::cout << dp[2 * n - 1][1] << std::endl;
}
return 0;
}