POJ 3254 【状态压缩DP】

题意:

给一块n*m的田地,1代表肥沃,0代表贫瘠。

现在要求在肥沃的土地上种草,要求任何两个草都不能相邻。

问一共有多少种种草的方法。

种0棵草也是其中的一种方法。

n和m都不大于12.

思路:

状态压缩DP,dp[i][j]代表在第i行状态j一共有多少种可能的种植方法。

j是二进制转化而来的状态,0代表不种草,1代表种草。

dp[i]只受到两个限制,即dp[i-1]的某种状态,和当前土地的贫瘠状况。

只要保证&操作之后重复的为0就可以了

最后输出sum(dp[n][1...w])(w代表一共有w种可行的状态)

#include<stdio.h>
int pho[15][15];
int biao[15];
int dp[15][1<<13];
int may[1<<13];
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            scanf("%d",&pho[i][j]);
            if(pho[i][j])
            {
                pho[i][j]=0;
            }
            else
            {
                pho[i][j]=1;
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        int tmp=pho[i][1];
        for(int j=2;j<=m;j++)
        {
            tmp=tmp<<1;
            tmp+=pho[i][j];
        }
        biao[i]=tmp;
    }
    int num=1;
    for(int s=0;s<(1<<m);s++)
    {
        if((s&(s<<1))==0)
        {
            may[num++]=s;
        }
    }
    for(int i=1;i<num;i++)
    {
        if((may[i]&biao[1])==0)
        {
            dp[1][i]=1;
        }
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<num;j++)
        {
            for(int k=1;k<num;k++)
            {
                if((may[k]&may[j])||(may[j]&biao[i]))
                    continue;
                dp[i][j]+=dp[i-1][k];
                dp[i][j]%=1000000000;
            }
        }
    }
    int ans=0;
    for(int i=1;i<num;i++)
    {
        ans+=dp[n][i];
        ans%=1000000000;
    }
    printf("%d\n",ans);
    return 0;
}
时间: 2024-08-24 09:14:07

POJ 3254 【状态压缩DP】的相关文章

Mondriaan&#39;s Dream(POJ 2411状态压缩dp)

题意:用1*2的方格填充m*n的方格不能重叠,问有多少种填充方法 分析:dp[i][j]表示i行状态为j时的方案数,对于j,0表示该列竖放(影响下一行的该列),1表示横放成功(影响下一列)或上一列竖放成功.状态转移时,枚举每一行可能的状态上一行取反得下一行能放的状态. #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include &

POJ 3311 状态压缩DP

题意:求从0点走到所有点又走回来的最短距离 该题又很多做法,我用的是弗洛伊德+状态压缩 先遍历所有点,求出两点间最短的距离,而后用状态压缩表示该点是否走过,(1<<n)-1则表示所有点都走过. 附AC代码 #include<stdio.h> #include<string.h> int map[12][12]; int dp[(1<<12)+1][12]; int min1(int a,int b) { if(a<b) return a; return

poj 3250 状态压缩dp入门

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7798   Accepted: 4159 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 yumm

poj 3254 状态压缩

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15285   Accepted: 8033 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 yum

poj 2688 状态压缩dp解tsp

题意: 裸的tsp. 分析: 用bfs求出任意两点之间的距离后可以暴搜也可以用next_permutation水,但效率肯定不如状压dp.dp[s][u]表示从0出发访问过s集合中的点,目前在点u走过的最短路程. 代码: //poj 2688 //sep9 #include <iostream> #include <queue> using namespace std; const int maxW=32; const int maxN=12; int dx[4]={-1,1,0,

hdu1143 状态压缩dp 记忆化搜索写法

http://poj.org/problem?id=1143 Description Christine and Matt are playing an exciting game they just invented: the Number Game. The rules of this game are as follows. The players take turns choosing integers greater than 1. First, Christine chooses a

POJ 3254 Corn Fields 状态压缩DP (C++/Java)

http://poj.org/problem?id=3254 题目大意: 一个农民有n行m列的地方,每个格子用1代表可以种草地,而0不可以.放牛只能在有草地的,但是相邻的草地不能同时放牛, 问总共有多少种方法. 思路: 状态压缩的DP. 可以用二进制数字来表示放牧情况并判断该状态是否满足条件. 这题的限制条件有两个: 1.草地限制. 2.相邻限制. 对于草地限制,因为输入的时候1是可以种草地的. 以"11110"草地分析,就只有最后一个是不可以种草的.取反后得00001  .(为啥取反

POJ 3254 Corn Fields 状态压缩DP

题目链接:http://poj.org/problem?id=3254 思路:状态压缩DP,状态方程为dp[i][j] += (dp[i-1][k]) code: #include <stdio.h> #include <string.h> #define N 500 const int MOD = 100000000; int dp[15][N],ant[N],n,m,k,map[15]; bool ok(int x) { if(x&(x<<1))return

poj 3254 Corn Fields ,状态压缩DP

题目链接 题意: 一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一头牛都不放也是一种方案) state[i] 表示对于一行,保证不相邻的方案 状态:dp[i][ state[j] ]  在状态为state[j]时,到第i行符合条件的可以放牛的方案数 状态转移:dp[i][ state[j] ] =Sigma dp[i-1][state'] (state'为符合条

poj 3254 Corn Fields(状态压缩dp)

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