天天看點

BZOJ1187: [HNOI2007]神奇遊樂園

Day1前挖的坑現在才補好…

就是一個三進制描述狀态..

感覺最近狀态不是很好啊..還是好好準備一下市統測吧

垃圾新聯考誤我競賽毀我青春妨我把妹

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;


char c;
bool flag;
inline void read(int &a)
{

    a=;do c=getchar();while(c!='-'&&(c<'0'||c>'9'));
    if(c=='-')flag=true,c=getchar();
    while(c<='9'&&c>='0')a=(a<<)+(a<<)+c-'0',c=getchar();
    if(flag)flag=false,a=-a;
}
int V[][];
int n,m;
const
  int INF=<<;
int f[][][];
bool us[][];
int Div[];
int getpc(int x,int pos){return (x/Div[pos])%3;}    
int pc(int pos,int op){return op*Div[pos];}
int get1(int x,int pos)
{
    int l=x%Div[pos];
    if(!l)return -;
    while(l<Div[pos])pos--;
    if(pos<)return -;
    return l/Div[pos]!=?-:pos;
}

int get2(int x,int pos)
{
    int l=x/Div[pos];
    if(!l)return -;
    while(l%3==)l/=,pos++;
    if(pos+>m)
        return -;
    return l/==?-:pos;
}

int main()
{
    int i,j,t;
    read(n),read(m);
    Div[]=;
    for(int i=;i<=;i++)
       Div[i]=Div[i-]*3;
    for(i=;i<=n;i++)
      for(j=;j<=m;j++)
        read(V[i][j]);
    int base;
    int Lim=;
    for(int i=;i<=m;i++)
        Lim*=;
    Lim--;
    int ans=-INF;
    for(i=;i<=n;i++)
     for(j=;j<=m;j++)
      for(t=;t<=;t++)
        f[i][j][t]=-INF;
    for(i=;i<=n;i++)
        for(j=;j<=m;j++)
            for(t=;t<=Lim;t++)
            {
                if(i==&&j==&&t==)
                    t++,t--;
                int rightpc=t%3,lowpc=getpc(t,j);
                if(t==){f[i][j][t]=;continue;}
                else
                {
                    if(i==&&j==)
                        if(t^)continue;
                        else f[i][j][t]=V[i][j];
                    else if(i==)
                    {
                     if(lowpc==)
                            if(rightpc==)
                                f[i][j][t]=f[i][j-][t];
                            else if(rightpc==)
                                f[i][j][t]=f[i][j-][t]+V[i][j];
                            else 
                                f[i][j][t]=f[i][j-][t]+V[i][j];
                        else if(lowpc==)
                            if(rightpc==)
                                f[i][j][t]=f[i][j-][t-*Div[j]+]+V[i][j];
                            else if(rightpc==)
                                f[i][j][t]=-INF;
                            else
                                f[i][j][t]=f[i][j-][t--Div[j]]+V[i][j];
                        else
                            if(rightpc==)
                                f[i][j][t]=f[i][j-][t-*Div[j]+]+V[i][j];
                            else if(rightpc==)
                                f[i][j][t]=-INF;
                            else 
                                f[i][j][t]=-INF; 

                    }
                    else if(j==)
                    {
                        if(lowpc==)
                            if(rightpc==)
                                f[i][j][t]=f[i-][m][t];
                            else if(rightpc==)
                                f[i][j][t]=f[i-][m][t-+Div[j]]+V[i][j];
                            else 
                                f[i][j][t]=f[i-][m][t-+*Div[j]]+V[i][j];
                        else if(lowpc==)
                            if(rightpc==)
                                f[i][j][t]=f[i-][m][t]+V[i][j];
                            else if(rightpc==)
                                f[i][j][t]=-INF;
                            else
                                f[i][j][t]=f[i-][m][t--Div[j]]+V[i][j];
                        else
                            if(rightpc==)
                                f[i][j][t]=f[i-][m][t]+V[i][j];
                            else if(rightpc==)
                                f[i][j][t]=-INF;
                            else 
                                f[i][j][t]=-INF;
                    }
                    else
                    {
                        if(lowpc==)
                            if(rightpc==)
                                {
                                int p1=get1(t,j),p2=get2(t,j);
                                f[i][j][t]=f[i][j-][t];
                                if(p1!=-)
                                    f[i][j][t]=max(f[i][j][t],f[i][j-][t-Div[p1]++Div[j]*2]+V[i][j]);
                                if(p2!=-)
                                    f[i][j][t]=max(f[i][j][t],f[i][j-][t+Div[p2]++Div[j]*1]+V[i][j]);
                                f[i][j][t]=max(f[i][j][t],f[i][j-][t++Div[j]*1]+V[i][j]);;
                                }
                            else if(rightpc==)
                                f[i][j][t]=max(f[i][j-][t],f[i][j-][t-+Div[j]])+V[i][j];
                            else 
                                f[i][j][t]=max(f[i][j-][t],f[i][j-][t-+*Div[j]])+V[i][j];
                        else if(lowpc==)
                            if(rightpc==)
                                f[i][j][t]=max(f[i][j-][t],f[i][j-][t-*Div[j]+])+V[i][j];
                            else if(rightpc==)
                                f[i][j][t]=-INF;
                            else
                                f[i][j][t]=f[i][j-][t--Div[j]]+V[i][j];
                        else
                            if(rightpc==)
                                f[i][j][t]=max(f[i][j-][t],f[i][j-][t-*Div[j]+])+V[i][j];
                            else if(rightpc==)
                                f[i][j][t]=-INF;
                            else 
                                f[i][j][t]=-INF;
                    }
                }
            }
       for(i=;i<=n;i++)
        for(j=;j<=m;j++)
            ans=max(ans,f[i][j-][+*Div[j]]+V[i][j]);
   printf("%d\n",ans);
    return ;
}