Description
Recently, cows are keen to pack gold coins in flour and then bake them into pies. The I-th Pie contains Ni (1 <= Ni <= 25) gold coins, which are prominently marked on the pie surface. The cows arranged all the cooked pies on the grass into a matrix of column C (1 <= r <= 100) (1 <= C <= 100. You are standing on the side of the pie with the coordinates of (). Of course, you can get all the gold coins in the pie. You must go from the current position to the other side of the lawn and stop walking next to the pie in the coordinates of (R, c. Every time you move, you must move to a pie in the next column, and the number of rows cannot exceed 1 (that is, if you are currently standing at the coordinates of (R, c) on the pie side, next you can go to coordinates for (R-1, C + 1), (R, C + 1), or (R + 1, C + 1) ). When you pass by a pie, you can take all the gold coins in the pie. Of course, you will not be willing to lose the gold coins at your fingertips because you leave the lawn halfway, but you will have to stop next to the pie in the coordinates of (R, c. Input
* Row 1st: two integers separated by spaces, R and C
* Row 2nd. R + 1: Each line contains C positive integers separated by spaces, which in turn indicate the number of gold coins in each pie from left to right in a row. Output
* Row 1st: Output a positive integer, indicating the maximum number of gold coins you can collect
Question:
Simple DP.
DP [I] [J] = max {DP [I + 1] [J-1], DP [I] [J-1], DP [I-1] [J-1]} + A [I] [J].
Code:
#include<cstdio>#include<cstring>#include<algorithm>//by zrt//problem:using namespace std;int n,m;int a[105][105];int dp[105][105];int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%d",&a[i][j]); } } dp[1][1]=a[1][1]; for(int i=2;i<=n+1;i++){ dp[i][1]=-(1<<30); } dp[0][1]=-(1<<30); for(int i=2;i<=m;i++){ dp[0][i]=-(1<<30); for(int j=1;j<=n;j++){ dp[j][i]=max(max(dp[j][i-1],dp[j-1][i-1]),dp[j+1][i-1])+a[j][i]; } dp[n+1][i]=-(1<<30); } printf("%d\n",dp[n][m]); return 0;}
Bzoj 1668: [usaco2006 Oct] wealth in the cow pie treasures pie