【lightoj 1004 Monkey Banana Problem (简单动规,就是数字三角形嘛。。。不过发现有个小问题)】

1004 - Monkey Banana Problem
Time Limit: 2 second(s) Memory Limit: 32 MB

You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dimensional array and can jump in any of the adjacent cells down from its current position (see figure). While moving from one cell to another, the monkey eats all the bananas kept in that cell. The monkey enters into the array from the upper part and goes out through the lower part. Find the maximum number of bananas the monkey can eat.

 

Input

Input starts with an integer T (≤ 50), denoting the number of test cases.

Every case starts with an integer N (1 ≤ N ≤ 100). It denotes that, there will be 2*N - 1 rows. The ith (1 ≤ i ≤ N) line of next N lines contains exactly inumbers. Then there will be N - 1 lines. The jth (1 ≤ j < N) line contains N - j integers. Each number is greater than zero and less than 215.

Output

For each case, print the case number and maximum number of bananas eaten by the monkey.

Sample Input

Output for Sample Input

2

4

7

6 4

2 5 10

9 8 12 2

2 12 7

8 2

10

2

1

2 3

1

Case 1: 63

Case 2: 5

Note

Dataset is huge, use faster I/O methods.


SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)
 
 
 
 
  1 // Project name : 1004 ( Monkey Banana Problem ) 
  2 // File name    : main.cpp
  3 // Author       : iCoding
  4 // E-mail       : honi.linux@gmail.com
  5 // Date & Time  : Thu Aug  9 12:13:15 2012
  6 
  7 
  8 #include <iostream>
  9 #include <stdio.h>
 10 #include <string>
 11 #include <cmath>
 12 #include <algorithm>
 13 using namespace std;
 14 
 15 /*************************************************************************************/
 16 /* data */
 17 #ifndef MAXN
 18 #define MAXN 110
 19 #endif
 20 int iMap[MAXN*2][MAXN];
 21 
 22 const int BLOCK = -1;
 23 
 24 int n;
 25 
 26 /*************************************************************************************/
 27 /* procedure */
 28 bool iCanGo(int x, int y)
 29 {
 30     if (iMap[x][y] == -1)
 31     {
 32         return false;
 33     }
 34     else
 35     {
 36         return true;
 37     }
 38 }
 39 
 40 void iInit()
 41 {
 42     for (int i = 0; i < MAXN * 2; i++)
 43     {
 44         for (int j = 0; j < MAXN; j++)
 45         {
 46             iMap[i][j] = BLOCK;
 47         }
 48     }
 49 
 50     scanf("%d", &n);
 51     for (int i = 1; i <= n; i++)
 52     {
 53         for (int j = 1; j <= i; j++)
 54         {
 55             scanf("%d", &iMap[i][j]);
 56         }
 57     }
 58     for (int i = n + 1; i < 2 * n; i++)
 59     {
 60         for (int j = 1; j <= (2 * n - i); j++)
 61         {
 62             scanf("%d", &iMap[i][j]);
 63         }
 64     }
 65 }
 66 
 67 void iDynamicProgramming()
 68 {
 69     for (int i = (n * 2 - 2); i >= n; i--)
 70     {
 71         for (int j = 1; j <= (2 * n - i); j++)
 72         {
 73             int iMax = -1;
 74             if (iCanGo(i+1,j-1) && iMap[i+1][j-1] > iMax)
 75             {
 76                 iMax = iMap[i+1][j-1];
 77             }
 78 
 79             if (iCanGo(i+1,j) && iMap[i+1][j] > iMax)
 80             {
 81                 iMax = iMap[i+1][j];
 82             }
 83 
 84             iMap[i][j] = iMax + iMap[i][j];
 85         }
 86     }
 87 
 88     for (int i = n - 1; i >= 1; i--)
 89     {
 90         for (int j = 1; j <= i; j++)
 91         {
 92             if (iMap[i+1][j] > iMap[i+1][j+1])
 93             {
 94                 iMap[i][j] += iMap[i+1][j];
 95             }
 96             else
 97             {
 98                 iMap[i][j] += iMap[i+1][j+1];
 99             }
100         }
101     }
102 }
103 
104 void iShowMap()
105 {
106     for (int i = 0; i <= n * 2; i++)
107     {
108         for (int j = 0; j <= n; j++)
109         {
110             printf("%d ", iMap[i][j]);
111         }
112         printf("\n");
113     }
114 }
115 
116 void iShowResult()
117 {
118     printf("%d\n", iMap[1][1]);
119 }
120 
121 /*************************************************************************************/
122 /* main */
123 int main()
124 {
125     int iT;
126     scanf("%d", &iT);
127     for (int iCaseCount = 1; iCaseCount <= iT; iCaseCount++)
128     {
129         printf("Case %d: ", iCaseCount);
130         iInit();
131         iDynamicProgramming();
132         //iShowMap();
133         iShowResult();
134 
135     }
136     return 0;
137 }
138 
139 // end 
140 // Code by Sublime text 2
141 // iCoding@CodeLab 
原文地址:https://www.cnblogs.com/ismdeep/p/2630117.html