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;
    }
  • 相关阅读:
    数据结构与算法 ||设计模式
    前端页面知识点
    easyui的dialog中的输入框在关闭后如何清空输入框中的内容
    设计模式之单例模式(通俗易懂,超详细)
    分布式锁--Java1234
    spring cloud Alibaba
    easyui
    SQL查询最大或最新的一条数据
    java中的异常(exception、throw、throws、try···catch)
    git error: The following untracked working tree files would be overwritten by merge:
  • 原文地址:https://www.cnblogs.com/2014slx/p/9399726.html
Copyright © 2011-2022 走看看