花店布置(一本通例题)

//这个题,吴同学让我深深明白了if  else  的差别,尤其是在动规里面
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=105; const int INF=0x3f3f3f3f; int f,v,dp[maxn][maxn],a[maxn][maxn]; bool b[maxn][maxn]; void shuchu(int x,int y) { if(x==0) return; if(b[x][y]==0) shuchu(x,y-1); if(b[x][y]==1) {shuchu(x-1,y-1);printf("%d ",y);} } int main() { freopen("hana.in","r",stdin); freopen("hana.out","w",stdout); //还需要记录方案数 scanf("%d%d",&f,&v); for(int i=1;i<=f;i++) for(int j=1;j<=v;j++) scanf("%d",&a[i][j]); for(int i=1;i<=f;i++) for(int j=0;j<=v;j++) dp[i][j] =-INF; //边界的设置有问题 for(int i=1;i<=f;i++) for(int j=i;j<=v;j++) //分为放和不放两种情况 { if(dp[i][j-1]>=dp[i-1][j-1]+a[i][j]) {dp[i][j]=dp[i][j-1];} else { dp[i][j]=dp[i-1][j-1]+a[i][j]; b[i][j]=true;//标识这条路是走过的 } } printf("%d\n",dp[f][v]); shuchu(f,v); }

 

posted @ 2017-07-08 10:54  xinyimama  阅读(107)  评论(0)    收藏  举报