题目链接:http://lightoj.com/volume_showproblem.php?problem=1004
#include <cstdio> #include <cstring> #include <iostream> #include <cmath> #include <algorithm> #include <queue> #include <vector> using namespace std; const int maxe = 50000; const int maxn = 105; const int INF = 0x3f3f3f; int num[maxn*2][maxn]; int dp[maxn*2][maxn]; int main() { // freopen("E:\acm\input.txt","r",stdin); int T; cin>>T; for(int t=1;t<=T;t++){ int N; cin>>N; for(int i=1;i<=N;i++) for(int j=1;j<=i;j++) cin>>num[i][j]; for(int i=N+1;i<=2*N-1;i++) for(int j=i-N+1;j<=N;j++) cin>>num[i][j]; memset(dp,0,sizeof(dp)); dp[N*2-1][N] = num[N*2-1][N]; for(int i=2*N-2;i>=N;i--) for(int j=i-N+1;j<=N;j++) dp[i][j] = num[i][j] + max(dp[i+1][j],dp[i+1][j+1]); for(int i=N-1;i>=1;i--) for(int j=1;j<=i;j++) dp[i][j] = num[i][j] + max(dp[i+1][j],dp[i+1][j+1]); printf("Case %d: %d ",t,dp[1][1]); } }