HDU 2845 Beans

Source: Internet
Author: User

Beans
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 1945 Accepted Submission (s): 984

 

Problem Description
Bean-eating is an interesting game, everyone owns an M * N matrix, which is filled with different qualities beans. meantime, there is only one bean in any 1*1 grid. now you want to eat the beans and collect the qualities, but everyone must obey by the following rules: if you eat the bean 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 you eat and then get?


Input
There are a few cases. in each case, there are two 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 bean isn't beyond 1000, and 1 <= M * N <= 200000.


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

Source
2009 Multi-University Training Contest 4-Host by HDU


Recommend
Gaojie
This topic is DP. The number of independent DP projects is small, but this time it was made independently. Hahaha, I'm a little excited.
In fact, after analyzing this question, we will find that we can use dp to calculate the maximum value of the entire row first, know the maximum value of each row, and then use DP to calculate the overall maximum value. The idea is the same. 0 indicates that the maximum value of the row is not selected, and 1 indicates that the row is selected.
Dp [j] [0] = max (dp [J-1] [1], dp [J-1] [0]);
Dp [j] [1] = max (dp [J-2] [0], dp [J-2] [1]);
Val = max (dp [j] [0], dp [j] [1]);
The same method is used to handle the whole process. Handle the boundary.
[Cpp]/********************************** ***************************
> File Name: a. cpp
> Author: SDUT_GYX
> Mail: 2272902662@qq.com
> Created Time: 23:48:46
**************************************** **********************/
 
# Include <iostream>
# Include <cstring>
# Include <algorithm>
# Include <cstdio>
# Include <queue>
# Include <cstdlib>
# Include <iomanip>
# Include <string>
# Include <vector>
# Include <map>
# Include <cmath>
# Include <stack>
# Define LL long
Using namespace std;
Int a [210000], dp_row [210000] [2], dp_col [210000] [2];
Int main ()
{
// Freopen ("data1.in", "r", stdin );
Int n, m;
While (scanf ("% d", & n, & m )! = EOF)
{
For (int I = 0; I <= n-1; I ++)
{
For (int j = 0; j <= m-1; j ++)
{
Int x = I * m + j;
Scanf ("% d", & a [x]);
}
}
Int val, res = 0;
For (int I = 0; I <= n-1; I ++)
{
Val = 0;
For (int j = 0; j <= m-1; j ++)
{
Int x = I * m + j;
If (j = 0)
{
Dp_row [j] [0] = 0;
Dp_row [j] [1] = a [x];
Val = max (val, dp_row [j] [1]);
Continue;
} Else if (j = 1)
{
Dp_row [j] [0] = max (dp_row [J-1] [0], dp_row [J-1] [1]);
Dp_row [j] [1] = a [x];
Val = max (val, dp_row [j] [0]);
Val = max (val, dp_row [j] [1]);
Continue;
}
Dp_row [j] [0] = max (dp_row [J-1] [0], dp_row [J-1] [1]);
Dp_row [j] [1] = max (dp_row [J-2] [0], dp_row [J-2] [1]) + a [x];
Val = max (val, dp_row [j] [0]);
Val = max (val, dp_row [j] [1]);
}
If (I = 0)
{
Dp_col [I] [0] = 0;
Dp_col [I] [1] = val;
Res = max (res, dp_col [I] [1]);
Continue;
} Else if (I = 1)
{
Dp_col [I] [0] = max (dp_col [I-1] [0], dp_col [I-1] [1]);
Dp_col [I] [1] = val;
Res = max (res, dp_col [I] [1]);
Res = max (res, dp_col [I] [0]);
Continue;
}
Dp_col [I] [0] = max (dp_col [I-1] [0], dp_col [I-1] [1]);
Dp_col [I] [1] = max (dp_col [I-2] [0], dp_col [I-2] [1]) + val;
Res = max (res, dp_col [I] [0]);
Res = max (res, dp_col [I] [1]);
}
Printf ("% d \ n", res );
}
Return 0;
}

/*************************************** ************************
> File Name: a. cpp
> Author: SDUT_GYX
> Mail: 2272902662@qq.com
> Created Time: 23:48:46
**************************************** **********************/

# Include <iostream>
# Include <cstring>
# Include <algorithm>
# Include <cstdio>
# Include <queue>
# Include <cstdlib>
# Include <iomanip>
# Include <string>
# Include <vector>
# Include <map>
# Include <cmath>
# Include <stack>
# Define LL long
Using namespace std;
Int a [210000], dp_row [210000] [2], dp_col [210000] [2];
Int main ()
{
// Freopen ("data1.in", "r", stdin );
Int n, m;
While (scanf ("% d", & n, & m )! = EOF)
{
For (int I = 0; I <= n-1; I ++)
{
For (int j = 0; j <= m-1; j ++)
{
Int x = I * m + j;
Scanf ("% d", & a [x]);
}
}
Int val, res = 0;
For (int I = 0; I <= n-1; I ++)
{
Val = 0;
For (int j = 0; j <= m-1; j ++)
{
Int x = I * m + j;
If (j = 0)
{
Dp_row [j] [0] = 0;
Dp_row [j] [1] = a [x];
Val = max (val, dp_row [j] [1]);
Continue;
} Else if (j = 1)
{
Dp_row [j] [0] = max (dp_row [J-1] [0], dp_row [J-1] [1]);
Dp_row [j] [1] = a [x];
Val = max (val, dp_row [j] [0]);
Val = max (val, dp_row [j] [1]);
Continue;
}
Dp_row [j] [0] = max (dp_row [J-1] [0], dp_row [J-1] [1]);
Dp_row [j] [1] = max (dp_row [J-2] [0], dp_row [J-2] [1]) + a [x];
Val = max (val, dp_row [j] [0]);
Val = max (val, dp_row [j] [1]);
}
If (I = 0)
{
Dp_col [I] [0] = 0;
Dp_col [I] [1] = val;
Res = max (res, dp_col [I] [1]);
Continue;
} Else if (I = 1)
{
Dp_col [I] [0] = max (dp_col [I-1] [0], dp_col [I-1] [1]);
Dp_col [I] [1] = val;
Res = max (res, dp_col [I] [1]);
Res = max (res, dp_col [I] [0]);
Continue;
}
Dp_col [I] [0] = max (dp_col [I-1] [0], dp_col [I-1] [1]);
Dp_col [I] [1] = max (dp_col [I-2] [0], dp_col [I-2] [1]) + val;
Res = max (res, dp_col [I] [0]);
Res = max (res, dp_col [I] [1]);
}
Printf ("% d \ n", res );
}
Return 0;
}

 

 

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.