Click the open link NYOJ 171
1. Question:
Smart kk
Description
The design of a pavilion in an African country is inspired by the steep and fluctuating sand dune in the legendary desert, reflecting the constant changing and colorful natural scenery and city style of the country. The exhibition hall is composed of five parts. The cinema in the Exhibition Hall plays a wide-screen short film named "a twinkling of an eye", reflecting the amazing changes of people's living standards and urban living environment since the founding of the People's Republic of China.
The movable "Sand Dune" magic is inspired by its unique and magnificent natural landscape-the legendary dangerous sand dune. The magnificent structure and circular building materials complement nature. Around a week, we found that it was inspired by the ever-changing form of the sand dune. The shape is lifelike and can clearly identify the features of the sand dune from any angle.
It's "slope" up to 20 meters, and the breeze blows, do you feel the flow of sand? Touch it with your hands, but you can see that it was a "magic trick ". The stainless steel panel on its surface presents a changeable color. It looks at different colors from different angles, thus imitating the Light of the flow sand dune.
In the third exhibition hall, there is a huge screen. Through the wonderful special effects, the audience will come to the vast desert. What's more, I saw a small animal "KK" moving from the upper left corner of the desert area (rectangle) to the right or down to the lower right corner. KK is so smart that it can choose to eat as many lines as possible during the running process.
Do you know how many bugs it eats?
Input
Line 1: n m (1 ≤ n m ≤ 20 0 ≤ Xij ≤ 500 (I =... N, j =, M)
) Indicates that the desert is a N * M rectangular area.
Next there are N rows: each row has M positive integers, Xi1 Xi2 ...... Xim indicates the number of bugs in each position (separated by a single space)
Suppose "KK" can only go to the right or down.
Output
The output contains an integer that indicates the maximum number of bugs "KK" can eat.
Sample Input
3 4
3 1 2 8
5 3 4 6
1 0 2 3
Sample output
24
2 idea: DP (maximum path and)
3 analysis: assuming the current position is in G [I] [j], only right and down can be performed based on kk, so we know that the previous steps of G [I] [j] come from g [I-1] [j] and G [I] [J-1], assume that dp [I] [j] is the maximum value of G [I] [j, then dp [I] [j] = max (dp [I-1] [j], dp [I] [J-1]) + G [I] [j]; so as long as we push each position, the last ans is dp [n] [m] (here, the subscript starts from 1 for convenience of calculation)
4 Code:
[Cpp]
# Include <algorithm>
# Include <iostream>
# Include <cstring>
# Include <cstdio>
Using namespace std;
# Define MAXN 30
# Define INF-0 xFFFFFFF
Int n, m;
Int G [MAXN] [MAXN];
Int dp [MAXN] [MAXN];
Int max (int a, int B ){
Return a> B? A: B;
}
Void solve (){
Int I, j;
Memset (dp, 0, sizeof (dp ));
For (I = 1; I <= n; I ++ ){
For (j = 1; j <= m; j ++)
Dp [I] [j] = max (dp [I-1] [j], dp [I] [J-1]) + G [I] [j];
}
Printf ("% d \ n", dp [n] [m]);
}
Int main (){
// Freopen ("input.txt", "r", stdin );
While (scanf ("% d % * c", & n, & m )! = EOF ){
For (int I = 0; I <= n; I ++) G [I] [0] = INF;
For (int I = 0; I <= m; I ++) G [0] [I] = INF;
For (int I = 1; I <= n; I ++ ){
For (int j = 1; j <= m; j ++)
Scanf ("% d", & G [I] [j]);
}
Solve ();
}
Return 0;
}