• POJ 3254 Corn Fields (状压DP)


    Corn Fields
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 4876   Accepted: 2573

    Description

    Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number of squares. Regrettably, some of the squares are infertile and can't be planted. Canny FJ knows that the cows dislike eating close to each other, so when choosing which squares to plant, he avoids choosing squares that are adjacent; no two chosen squares share an edge. He has not yet made the final choice as to which squares to plant.

    Being a very open-minded man, Farmer John wants to consider all possible options for how to choose the squares for planting. He is so open-minded that he considers choosing no squares as a valid option! Please help Farmer John determine the number of ways he can choose the squares to plant.

    Input

    Line 1: Two space-separated integers: M and N 
    Lines 2..M+1: Line i+1 describes row i of the pasture with N space-separated integers indicating whether a square is fertile (1 for fertile, 0 for infertile)

    Output

    Line 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.

    Sample Input

    2 3
    1 1 1
    0 1 0

    Sample Output

    9

    Hint

    Number the squares as follows:
    1 2 3
      4  

    There are four ways to plant only on one squares (1, 2, 3, or 4), three ways to plant on two squares (13, 14, or 34), 1 way to plant on three squares (134), and one way to plant on no squares. 4+3+1+1=9.

    Source

     
     
    1. 题意:在一片M行N列的草地上(用0和1矩阵表示),1表示能放牛,0表示不能放。 
    2.       在草地上放牛并且牛不能相邻,问有多少种放法(一头牛都不放也算一种)。 
    3.  
    4. 题解:对于每一行来说,放牛的可能数有2^N种,但是根据以下限制条件就能排除很多: 
    5.       1.每行中的牛不能相邻,经计算当N=12时,满足条件的状态只有377个 
    6.       2.每行中放牛要满足草地的硬件条件,只有1处可放,排除一些 
    7.       3.上一行中与本行对应的位置处不能放牛,排除一些 
    8.        
    9.       由于N值最大为12,所以可以用一个二进制数来表示一行的状态,这就是“状态压缩”了。 
    10.       定义dp[i][j]:第i行的状态为state[j]时,前i行能放牛方法的总数.
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int mod=100000000;
    
    int m,n;
    int dp[13][520];    //dp[i][j]:第i行的状态为state[j]时,前i行能放牛方法的总数.
    int cnt,row[13],state[520];
    
    void init(){
        cnt=0;
        for(int i=0;i<(1<<n);i++)
            if((i&(i<<1))==0)
                state[cnt++]=i;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        /*   求满足条件的状态总数  p=377
        int p=0;
        for(int i=0;i<(1<<12);i++)
            if((i&(i<<1))==0)
                p++;
        printf("p=%d\n",p);
        */
    
        while(~scanf("%d%d",&m,&n)){
            init();
            int x;
            for(int i=0;i<m;i++){
                row[i]=0;
                for(int j=0;j<n;j++){
                    scanf("%d",&x);
                    row[i]+=(x<<j);
                }
            }
            memset(dp,0,sizeof(dp));
            for(int j=0;j<cnt;j++)  // 求dp[0],若state[j]满足第0行草地的硬件条件,则dp[0][j]=1
                if((row[0]&state[j])==state[j])
                    dp[0][j]=1;
            for(int i=1;i<m;i++)    // 求dp[i],如果state[j]和第i-1行的状态state[k]不冲突,则dp[i][j]=Σ(dp[i-1][k])
                for(int j=0;j<cnt;j++)
                    if((row[i]&state[j])==state[j]) //判断是否满足草地硬件条件 
                        for(int k=0;k<cnt;k++)
                            if((state[j]&state[k])==0 && dp[i-1][k]!=0)
                                dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
            int ans=0;
            for(int j=0;j<cnt;j++)
                if(dp[m-1][j]!=0)
                    ans=(ans+dp[m-1][j])%mod;
            printf("%d\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    平时代码中用不到设计模式?Are you kidding me?
    关于kubernetes我们还有什么可做的?
    R语言︱文本挖掘——词云wordcloud2包
    描述性统计分析-用脚本将统计量函数批量化&分步骤逐一写出
    R语言读写中文编码方式
    R画图中英文字体完美解决方案
    Python 和 R 数据分析/挖掘工具互查
    关于ř与tableau的集成---- k均值聚类
    tableau 连接R语言
    小识Tableau
  • 原文地址:https://www.cnblogs.com/jackge/p/3095969.html
Copyright © 2020-2023  润新知