zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    这2天参加WinHEC大会,园子里以有很多介绍,就不多说了,会上用手机录了一段windows 最新触摸屏操作技术演示,可以看看
    自启动U盘,一个会流行的好玩意
    为什么数据库导入是自动增量属性自动消失乐呢?
    网络带宽利用率的一般计算方法
    防止ARP病毒的一个小窍门
    Windows 系统补丁管理策略
    PDC大会就要召开了,园里有去的吗,看到一片文章,不知道windows7是否真的很好
    门户框架在项目和产品中的使用心得
    这段时间开发了一个共享软件,主要做IP资源管理的(SmartIPView),大家有兴趣可以看看,或给指点指点
    OpenGL自学教程1(窗口建立)
  • 原文地址:https://www.cnblogs.com/jackge/p/3095969.html
Copyright © 2011-2022 走看看