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

    题目链接:

    Corn Fields

    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 12175   Accepted: 6398

    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

    题意:

    给n*m的格子,相邻的格子不能都种植物,现在给的0的格子也不能种植物,问你一共有多少种种植物的方法;

    思路:

    dp[i][j]表示第i行的状态为j前i行一共有多少种方法,然后就是把合法的状态找出来并且转移,具体的看代码;

    AC代码:
    //#include <bits/stdc++.h>
    #include <vector>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #include <map>
    #include <cstring>
    #include <algorithm>
    #include <cstdio>
    
    using namespace std;
    #define Riep(n) for(int i=1;i<=n;i++)
    #define Riop(n) for(int i=0;i<n;i++)
    #define Rjep(n) for(int j=1;j<=n;j++)
    #define Rjop(n) for(int j=0;j<n;j++)
    #define mst(ss,b) memset(ss,b,sizeof(ss));
    typedef long long LL;
    template<class T> void read(T&num) {
        char CH; bool F=false;
        for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
        for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
        F && (num=-num);
    }
    int stk[70], tp;
    template<class T> inline void print(T p) {
        if(!p) { puts("0"); return; }
        while(p) stk[++ tp] = p%10, p/=10;
        while(tp) putchar(stk[tp--] + '0');
        putchar('
    ');
    }
    
    const LL mod=1e8;
    const double PI=acos(-1.0);
    const LL inf=1e18;
    const int N=(1<<12)+10;
    const int maxn=1005;
    
    LL dp[13][N];
    int a[15][15],temp[15],num[15],cnt,b[N];
    int n,m;
    
    int ok(int x,int y)
    {
        if(x&y)return 0;
        return 1;
    }
    int check(int x)
    {
        mst(temp,0);
        int s=0;
        while(x)
        {
            temp[++s]=(x&1);
            x>>=1;
        }
        for(int i=1;i<=s;i++)
        {
            if(temp[i]&&temp[i+1])return 0;
        }
        return 1;
    }
    void Init()
    {
         cnt=0;
        for(int i=0;i<(1<<m);++i)
        {
            if(check(i))b[++cnt]=i;
        }
    }
    int main()
    {
        read(n);read(m);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                read(a[i][j]);
        for(int i=1;i<=n;i++)
        {
            int x=0,w=1;
            for(int j=1;j<=m;j++)
            {
                if(!a[i][j])x+=w;
                w*=2;
            }
            num[i]=x;
        }
        Init();
        for(int i=1;i<=cnt;i++)
        {
            if(ok(num[1],b[i]))dp[1][i]=1;//ans+=dp[1][i];
        }
    
        for(int i=2;i<=n;i++)
        {
            for(int j=1;j<=cnt;j++)
            {
                if(ok(num[i],b[j]))
                {
                    for(int k=1;k<=cnt;k++)
                    if(ok(num[i-1],b[k])&&ok(b[j],b[k]))
                        dp[i][j]+=dp[i-1][k],dp[i][j]%=mod;//ans+=dp[i][j],ans%=mod;
                }
            }
        }
        LL ans=0;
            for(int j=1;j<=cnt;j++)
                if(ok(num[n],b[j]))
                        ans+=dp[n][j],ans%=mod;
        cout<<ans<<"
    ";
            return 0;
    }


  • 相关阅读:
    GridControl主从表设置
    Asp.net Ajax框架教程
    实现类似百度下拉框自动匹配功能
    将一个DataTable分解成多个DataTable
    找不到可安装的ISAM ,asp.net读取数据丢失,解决的一列里有字符与数字的
    StringCollection FAQ [C#, BCL]
    从枚举的初始化说起 [C#]
    当多态遇上数组 ... [C++] (Rewritten)
    我并不是不闻不问![C#]
    当多态遇上数组 ... [C++, C++/CLI, C#]
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5605815.html
Copyright © 2011-2022 走看看