用户登录
用户注册

分享至

玉米田(状压dp)

  • 作者: 我来自未来丶希望你懂我
  • 来源: 51数据库
  • 2021-07-28

思路:dp[i][j]表示前i行已经摆好且第i行的状态为j,那么假设状态a是他的上一行状态可得dp[i][j]+=dp[i-1][a],所以我们枚举可行状态,因为玉米地不是每个地方都能放,所以我们再进行dp的时候需要先判断一下当前状态是否可以在i行的玉米地放置。

#pragma GCC optimize(2)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#include<stack>
#include<iomanip>
#include<cstring>
#include<time.h>
using namespace std;
typedef long long ll;
#define SIS std::ios::sync_with_stdio(false)
#define space putchar(' ')
#define enter putchar('\n')
#define lson root<<1
#define rson root<<1|1
typedef pair<int,int> PII;
const int mod=1e8;
const int N=2e6+10;
const int M=1e3+10;
const int inf=0x7f7f7f7f;
const int maxx=2e5+7;
const double eps=1e-6;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*(b/gcd(a,b));
}

template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-')
            op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op)
        x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0)
        x = -x, putchar('-');
    if(x >= 10)
         write(x / 10);
    putchar('0' + x % 10);
}
ll qsm(int a,int b,int p)
{
    ll res=1%p;
    while(b)
    {
        if(b&1)
            res=res*a%p;
        a=1ll*a*a%p;
        b>>=1;
    }
    return res;
}
int g[N];
int n,m;
vector<int> state,head[N];
int dp[15][100005];
bool check(int x)
{
    return !(x&x>>1);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<m;j++)
        {
             int x;
             scanf("%d",&x);
             g[i]+=!x<<j; 
        }
    }
    for(int i=0;i<(1<<m);i++)
      if(check(i))
        state.push_back(i);
    for(int i=0;i<state.size();i++)
    {
        for(int j=0;j<state.size();j++)
        {
            int a=state[i],b=state[j];
            if((a&b)==0)
            head[i].push_back(j);
        }
    }
    dp[0][0]=1;
    for(int i=1;i<=n+1;i++)
    {
        for(int a=0;a<state.size();a++)
        {
            for(int b:head[a])
            {
                if(g[i]&state[a]) continue;
                dp[i][state[a]]=(dp[i][state[a]]+dp[i-1][state[b]])%mod;
            }
        }
    }
    printf("%d",dp[n+1][0]);
   

    return 0;
}


软件
前端设计
程序设计
Java相关