noi 97 积木游戏

思路:黑书的例题

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Maxn 110
#define Maxm 100010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 0x7fffffff
#define Mod 1000000007
using namespace std;
int dp[110][110][110][4];
struct PP{
    int h[4];
}p[Maxn];
int check(PP a,int s1,PP b,int s2)
{
    int x1,x2,y1,y2;
    if(s1==1){
        x1=a.h[2],y1=a.h[3];
    }
    if(s1==2){
        x1=a.h[1],y1=a.h[3];
    }
    if(s1==3){
        x1=a.h[1],y1=a.h[2];
    }
    if(s2==1){
        x2=b.h[2],y2=b.h[3];
    }
    if(s2==2){
        x2=b.h[1],y2=b.h[3];
    }
    if(s2==3){
        x2=b.h[1],y2=b.h[2];
    }
    if(x1<=x2&&y1<=y2||x1<=y2&&y1<=x2)
        return 1;
    return 0;
}
int main()
{
    int n,i,j,m,k,u,v;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(dp,0,sizeof(dp));
        for(i=1;i<=n;i++){
            scanf("%d%d%d",&p[i].h[1],&p[i].h[2],&p[i].h[3]);
        }
        for(i=1;i<=3;i++){
            dp[1][1][1][i]=p[1].h[i];
        }
        for(i=1;i<=n;i++){
            for(u=1;u<=3;u++){
                dp[1][i][i][u]=p[i].h[u];
            }
        }
        for(i=1;i<=m;i++){
            for(j=i;j<=n;j++){
                for(k=0;k<j;k++){
                    for(u=1;u<=3;u++){
                        dp[i][j][k][u]=max(dp[i][j][k][u],dp[i][j-1][k][u]);
                        for(v=1;v<=3;v++){
                            if(i<m){
                                dp[i+1][j][j][u]=max(dp[i+1][j][j][u],dp[i][j-1][k][v]+p[j].h[u]);
                            }
                            if(check(p[j],u,p[k],v)){
                                dp[i][j][j][u]=max(dp[i][j][j][u],dp[i][j-1][k][v]+p[j].h[u]);
                            }
                        }
                    }
                }
            }
        }
        int ans=0;
        for(i=m;i<=n;i++){
            for(j=1;j<=3;j++){
                ans=max(ans,dp[m][n][i][j]);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

posted @ 2013-08-23 21:52  fangguo  阅读(270)  评论(0编辑  收藏  举报