zoukankan      html  css  js  c++  java
  • poj 3254Corn Fields (入门状压dp)

    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.

    被hdu 多校第3场c题教育了,来刷状压dp

    先处理下所有的满足的状态;i&i<<1

    然后和上一个状态比较。满足的状态里选出和上一行满足的状态

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    //#include <algorithm>
    #include <vector>
    using namespace std;
    #define ll long long
    //#define mod 998244353
    const int mod=100000000;
    int low[15];
    int temp;
    int state[1<<15];
    int dp[15][1<<15];
    int main()
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                int num;
                scanf("%d",&num);
                low[i]=low[i]|(num<<j);
            }
        }//处理每一行的状态
        temp=0;
        for(int i=0;i<(1<<m);i++)
        {
            if((i&(i<<1))==0)
            {
                state[temp++]=i;
            }
        }//处理所有满足的状态
        memset(dp,0,sizeof dp);
        for(int i=0;i<temp;i++)
        {
            if((low[0]&state[i])==state[i])
            dp[0][i]=1;
            
            
        }//处理第一行
        
        for(int i=1;i<n;i++)
            for(int j=0;j<temp;j++)
            {
                if((low[i]&state[j])==state[j])
                {
                    for(int k=0;k<temp;k++)
                    {
                        if((state[j]&state[k])==0)
                            dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
                    }
                }
            }
        int ans=0;
        for(int i=0;i<temp;i++)
        {
            ans=ans+dp[n-1][i];
            ans=ans%mod;
        }
        cout<<ans<<endl;
        
    return 0;
    }
  • 相关阅读:
    JAVA错误:Unable to find config file. Creating new servlet engine config file: /WEBINF/serverconfig.wsdd
    java axis发布web service(二) 发布web service
    JAVA错误:AXIS Web Service Problem: No compiler found in your classpath! (you may need to add ‘tools.jar’)
    JAVA错误:java.lang.UnsupportedClassVersionError: Bad version number in .class file
    JavaScript中的div和filter错误
    拉格朗日乘数法
    Timeout Detection & Recovery (TDR)
    游戏程序员关心的Autodesk Maya 2013相关操作
    Eclipse开发Android程序如何在手机上运行
    我的第一篇随笔
  • 原文地址:https://www.cnblogs.com/2014slx/p/9399726.html
Copyright © 2011-2022 走看看