http://lightoj.com/volume_showproblem.php?problem=1004
- #include <bits/stdc++.h>
- using namespace std;
- int a[1020][1020];
- int b[1020][1020];
- void print(int n)
- {
- for(int i = 1; i <=n; i++){
- for(int j = 1; j <=i; j++){
- cout<<b[i][j]<<" ";
- }
- cout<<endl;
- }
- for(int i = n+1; i<=2*n-1; i++){
- for(int j = 1; j <=2*n-i; j++){
- cout<<b[i][j]<<" ";
- }
- cout<<endl;
- }
- }
- int main()
- {
- int ts;
- cin>>ts;
- for(int p = 1; p <= ts; p++){
- memset(a,0, sizeof(a));
- memset(b, 0, sizeof(b));
- int n;
- cin>>n;
- for(int i = 1; i <=n; i++){
- for(int j = 1; j <=i; j++){
- int b;
- scanf("%d", &b);
- a[i][j] = b;
- }
- }
- for(int i = n+1; i <=2*n-1; i++){
- for(int j = 1; j <=2*n-i; j++){
- int b;
- scanf("%d", &b);
- a[i][j] = b;
- }
- }
- b[1][1] = a[1][1];
- for(int i = 2; i <=n; i++){
- for(int j = 1; j <=i; j++){
- b[i][j] =a[i][j]+ max(b[i-1][j], b[i-1][j-1]);
- }
- }
- for(int i = n+1; i <= 2*n-1; i++){
- for(int j = 1; j <= 2*n-i; j++){
- b[i][j] =a[i][j]+ max(b[i-1][j], b[i-1][j+1]);
- }
- }
- int ans = b[2*n-1][1];
- cout<<"Case "<<p<<": "<<ans<<endl;
- }
- //print(n);
- return 0;
- }
No comments:
Post a Comment