- [1568] walk, walk, walk, walk.
- Time limit: Ms Memory limit: 65535 K
- Problem description
Vegetables to make money back, remembered to buy a lot of barrels back, different places barrels quality is different, he at (at) point of departure because the plane ticket is a bit expensive so he can only move to our location (n,m), that is, can only move to the right and down,
We have barrels that may be tired of eating. So (I,J) the value of the point is not to be taken, but the vegetables themselves will be hungry so in some cities will eat part, even first overdraft part, so a (I, j) can be negative,
To reward us, he'll try to bring him a little more, ask him how much quality he brings back.
- Input
- Input n,m (n,m <= 1000)
Then enter n rows m column value A (i,j) a (i,j) between-1000 ~ 1000. and a (I,J) can be taken without taking. Just go right or down.
Ask you, from the upper left corner to the lower right corner, you can get the maximum value is how much.
- Output
- Maximum output value
- Sample input
3 31 2 34 5 64 8 9
- Sample output
27
- Tips
1, 4, 5, 8, 9
The principle is similar to the number of towers, four states: above and to the left (2) * Take or not take (2).
Code:
#include <iostream> #include <algorithm> #include <cstdlib> #include <sstream> #include < cstring> #include <cstdio> #include <string> #include <deque> #include <stack> #include < cmath> #include <queue> #include <set> #include <map>using namespace std;typedef long Long ll;# Define INF 0x3f3f3f3fint pos[1010][1010];int dp[1010][1010];int Main (void) {int n,m,temp,i,j;while (~scanf ("%d%d", &n,&m) {memset (pos,0,sizeof (POS)), memset (Dp,0,sizeof (DP)), for (i=0; i<n; i++) {for (j=0; j<m; J + +) { scanf ("%d", &pos[i][j]);}} dp[0][0]=pos[0][0];//here to initialize for (i=0; i<n; i++) {for (j=0; j<m; J + +) {Dp[i][j]=max (Dp[i-1][j],max (dp[i][j-1), Max (pos[i][j]+dp[i-1][j],pos[i][j]+dp[i][j-1]));}} printf ("%d\n", Dp[n-1][m-1]);} return 0;}
noj--1568 Walk, walk, walk, walk, go. (Super starter DP)