(状态压缩dp) poj 3254

Corn Fields

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8729   Accepted: 4651

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

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.

Source

USACO 2006 November Gold

注意读入的时候 ,要将读入的0,1置换。防止多余的重复

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define maxn 10010
#define MOD 100000000
int n,m,a[15],dp[15][maxn],sta[maxn];
bool judge(int x,int y)
{
    if(x&y) return false;
    return true;
}
void fun()
{
    int num=0;
    int sum=(1<<m);
    for(int i=0;i<sum;i++)
    {
        if(i&(i<<1))
            continue;
        sta[num++]=i;
    }
    for(int i=0;i<num;i++)
    {
        if(judge(a[1],sta[i]))
        {
            dp[1][i]=1;
        }
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=0;j<num;j++)
        {
            if(!judge(a[i],sta[j]))
                continue;
            for(int k=0;k<num;k++)
            {
                if(!judge(sta[k],a[i-1]))
                    continue;
                if(!judge(sta[k],sta[j]))
                    continue;
                dp[i][j]=(dp[i][j]+dp[i-1][k])%MOD;
            }
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        memset(dp,0,sizeof(dp));
        int x,ans=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                scanf("%d",&x);
                if(x==0)
                   a[i]+=(1<<(m-j));
            }
        }
        fun();
        for(int i=0;i<(1<<m);i++)
            ans=(ans+dp[n][i])%MOD;
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-10-12 20:08:28

(状态压缩dp) poj 3254的相关文章

状态压缩dp poj 3254 hdu5045

近来感觉状态压缩dp的强大性(灵活利用了二进制运算很关键)...于是做了俩提来看看..毕竟队友是专业的dp,我只是管中窥豹下而已..日后有机会再与之玩耍玩耍...ps:如果上天再给我一次机会,当年我愿意选择状态dp而不是网络流(只针对目前比赛出题潮流) 经典问题,不相邻/禁点方案数问题.poj3254 #include<iostream> #include<cstdio> using namespace std; int n,m; int dp[5000][15]; int yu[

(状态压缩DP) poj 2978

Colored stones Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1734   Accepted: 819 Description You are given a row of m stones each of which has one of k different colors. What is the minimum number of stones you must remove so that no

状态压缩DP——POJ 2923

对应POJ题目:点击打开链接 Exponentiation Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2923 Description Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortu

(状态压缩DP) poj 2441

Arrange the Bulls Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 3709   Accepted: 1422 Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because the

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

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代表一共