洛谷1879 [USACO06NOV]玉米田Corn Fields

(http://www.elijahqi.win/2017/07/07/%E6%B4%9B%E8%B0%B71879-usaco06nov%E7%8E%89%E7%B1%B3%E7%94%B0corn-fields/)
数据范围挺小的 滚动不滚动都可以

数组滚动

#include 
#include 
#define N1 100000000
int t,data[2][(1<<13)+100],row[20],m,n,state[1<<13],w,idx=0,cur=0;
int main(){
    freopen("1879.in","r",stdin);
    freopen("1879.out","w",stdout);
    scanf("%d%d",&m,&n);
    for (int i=1;i<=m;++i)
        for (int j=1;j<=n;++j) scanf("%d",&t),row[i]=(row[i]<<1)|!t; //交换0&1 1表示田不能种 
    //枚举所有可行状态   防止两边相邻 
    for(int i=0;i<=(1<1;i++) if((i&(i<<1))==0) state[++w]=i;
    for (int i=1;i<=w;++i) if ((row[1]&state[i])==0) data[idx][state[i]]=1;  //给予第一行可行状态 1
    for (int i=2;i<=m;++i){
        cur=idx^1;memset(data[cur],0,sizeof(data[cur]));
        for (int j=1;j<=w;++j)
            for (int z=1;z<=w;++z)
                if (((row[i]&state[j])==0)&&((state[j]&state[z])==0)) {
                    data[cur][state[j]]+=data[idx][state[z]],data[cur][state[j]]%=N1;
                }
        idx=cur;
    }
    int ans=0;
    for (int i=1;i<=w;++i){
        ans+=data[cur][state[i]];ans%=N1;
    }   
    printf("%d",ans);

    return 0;
}

数组不滚动

#include 
#define N1 100000000
int t,data[20][(1<<13)+100],row[20],m,n,state[1<<13],w;
int main(){
    //freopen("1879.in","r",stdin);
    //freopen("1879.out","w",stdout);
    scanf("%d%d",&m,&n);
    for (int i=1;i<=m;++i)
        for (int j=1;j<=n;++j) scanf("%d",&t),row[i]=(row[i]<<1)|!t; //交换0&1 1表示田不能种 
    //枚举所有可行状态   防止两边相邻 
    for(int i=0;i<=(1<1;i++) if((i&(i<<1))==0) state[++w]=i;
    for (int i=1;i<=w;++i) if ((row[1]&state[i])==0) data[1][state[i]]=1;  //给予第一行可行状态 1
    for (int i=2;i<=m;++i)
        for (int j=1;j<=w;++j)
            for (int z=1;z<=w;++z)
                if (((row[i]&state[j])==0)&&((state[j]&state[z])==0)) data[i][state[j]]+=data[i-1][state[z]],data[i][state[j]]%=N1;
    int ans=0;
    for (int i=1;i<=w;++i){
        ans+=data[m][state[i]];ans%=N1;
    }    
    printf("%d",ans);

    return 0;
}

你可能感兴趣的:(动态规划)