Nyoj Eating potatoes (dynamic planning)

Source: Internet
Author: User

Describe
Bean-eating is a interesting game, everyone owns an m*n matrix, which are filled with different qualities beans. Meantime, there is the only one bean in any 1*1 grid. Now your want to eat the beans and collect the qualities, but everyone must obey by the following rules:if you eat the BEA n at the coordinate (x, y), you can ' t eat the beans anyway at the coordinates listed (if exiting): (x, Y-1), (x, y+1), and The both rows whose Abscissas are x-1 and x+1.
Now, how much qualities can do you eat and then get?
Input
There is a few cases. In each case, there is the integer M (row number) and N (column number). The next M lines each contain N integers, representing the qualities of the beans. We can make sure that the quality of beans isn ' t beyond, and 1<=m,n<=500.
Output
For each case, you just output the MAX qualities you can eat and then get.
Sample input
4 6
11 0 7 5 13 9
78 4 81 6 22 4
1 40 9 34 16 10
11 22 0 33 39 6
Sample output

242


If it is a line, that is, when the n==1, Dp[i] said from the beginning to the number of I can obtain the maximum and, there is Dp[i]=max (Dp[i-1], dp[i-2]+a[i]); This is the case of each row, calculate the maximum sum of each line can be obtained, and then vertical, the adjacent row can not be taken, you can use the same method to find the largest sum of the whole matrix, the overall idea is to first calculate each line and then two dimensions into one dimension.


AC Code:


# include <cstdio># include <cstring># include <algorithm>using namespace Std;int a[510][510], dp_y[ 510][510], Ans[510];int main () {int n, m, I, J, K;while (scanf ("%d%d", &n, &m)!=eof) {memset (A, 0, sizeof (a)); for (i= 1; i<=n; i++) {for (j=1; j<=m; J + +) {scanf ("%d", &a[i][j]);}} memset (dp_y, 0, sizeof (dp_y)), for (I=1; i<=n; i++) {dp_y[i][1]=a[i][1];for (j=2; j<=m; J + +) {Dp_y[i][j]=max (dp_y[i ][j-1], dp_y[i][j-2]+a[i][j]);}} memset (ans, 0, sizeof (ans)), ans[1]=dp_y[1][m];for (i=2; i<=n; i++) {Ans[i]=max (ans[i-1], ans[i-2]+dp_y[i][m]);} printf ("%d\n", Ans[n]);} return 0;}


Nyoj Eating potatoes (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.