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;
    }
  • 相关阅读:
    登陆验证前对用户名和密码加密之后传输数据---base64加密
    用HTML5实现的各种排序算法的动画比较 及算法小结
    jquery mobile 请求数据方法执行时显示加载中提示框
    settimeout如何调用方法的时候,传递参数
    为什么html5用的jQuery Mobile在手机浏览器/微信中打开字体很小
    MVC中view页面用jquery方法绑定select控件值
    查看谷歌浏览器保存的本地密码,临时表创建索引
    docker安装mysql
    安装docker
    charles模拟弱网操作
  • 原文地址:https://www.cnblogs.com/2014slx/p/9399726.html
Copyright © 2011-2022 走看看