Nyist OJ 171 smart kk (Dynamic Planning)

Source: Internet
Author: User
Smart KK time limit: 1000 MS | memory limit: 65535 kb difficulty: 3
Description
Smart "KK"
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
Row 1: n m (1 ≤ n m ≤ 20 0 ≤ Xij ≤ 500 (I = ?. N, j = 1, 2 ?, 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 43 1 2 85 3 4 61 0 2 3
Sample output
24
Source

Third Henan Program Design Competition

After a simple DP problem, I began to see that this question was always intended to be searched in two directions. Later I found that I could not write it, because the starting values of the two directions here are the same each time, there should be a problem with the idea, so I changed my mind and used dynamic planning for it, the idea of dynamic planning is to start from the back to ensure the optimal current position. Therefore, we directly simulate the route that KK has traveled. kk can only go to the right or down, we compare the two directions and select a maximum value. The number of current steps depends on the position of the previous step;

So we can get the state relationship, DP [I] [J] = max (DP [I] [J-1], DP [I-1] [J]) + map [I] [J]; because it is recursion starting from the back, the first step of the maximum value is upward or left;

The following is the code;

# Include <cstdio> # include <cstring> # define max (A, B) A> B? A: bint map [25] [25], DP [25] [25]; int main () {int n, m, I, J; scanf ("% d", & N, & M); for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) scanf ("% d", & map [I] [J]); for (I = 1; I <= N; I ++) for (j = 1; j <= m; j ++) // DP [I] [J] = max (DP [I-1] [J] + map [I] [J], DP [I] [J-1] + map [I] [J]); // The two Recursive Relations can map [I] [J] + = max (Map [I] [J-1], map [I-1] [J]); printf ("% d \ n", map [N] [m]);}



Nyist OJ 171 smart kk (Dynamic Planning)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.