[ACM] POJ 3254 Corn Fields(状态压缩)

Corn Fields

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8062   Accepted: 4295

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

field=source&key=USACO+2006+November+Gold">USACO 2006 November Gold

解题思路:

题意为有n行m列的长方形,分成n*m个格子,每一个格子标记为0或1,在这些格子里面放牛,当中1代表该格子能够放牛,0代表不能放牛,且相邻的两个格子不能同一时候有牛。问总的方案数。

思想为状态压缩。把每一行放牛的状态看作一个二进制数,dp[i][j]表示第i行状态为j时前i行共同拥有的方案数。

代码:

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
const int mod=100000000;
const int maxn=12;
int dp[maxn+1][(1<<maxn)+1];
int num[maxn+1];
int n,m;

bool check(int i,int x)//检查第i行出现状态x是否合法
{
    if((x&num[i])!=x)//非常巧妙,推断第i行出现的状态x是否合法,为什么这么写。由于0的地方不能放牛。
    //合法状态与原始状态0位且为0,原始状态1位的地方与合法状态相应位且都等于合法状态位上的数字
        return 0;
    if(x&(x>>1)||x&(x<<1))//不能有相邻的两个1
        return 0;
    return 1;
}

int main()
{
    cin>>n>>m;
    int x;
    memset(num,0,sizeof(num));
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
    {
        cin>>x;
        if(x)
            num[i]=num[i]|(1<<(j-1));//把每一行的状态保存到num[i]中去
    }
    int Max=(1<<m);
    dp[0][0]=1;//注意这一句啊!

for(int i=1;i<=n;i++)//枚举每一行
    {
        for(int j=0;j<Max;j++)
        {
            if(!check(i,j))
                continue;
            for(int k=0;k<Max;k++)
                if((j&k)==0)
            {
                dp[i][j]+=dp[i-1][k];
                if(dp[i][j]>=mod)
                    dp[i][j]%=mod;
            }
        }
    }
    int ans=0;
    for(int i=0;i<Max;i++)
    {
        ans+=dp[n][i];
        if(ans>=mod)
            ans%=mod;
    }
    cout<<ans;
    return 0;
}
时间: 2024-10-16 10:27:12

[ACM] POJ 3254 Corn Fields(状态压缩)的相关文章

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 (入门级)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 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(状态压缩DP)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 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(状态压缩)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8291   Accepted: 4409 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 (状态压缩入门)

http://poj.org/problem?id=3254 参考:http://blog.csdn.net/accry/article/details/6607703 农夫想在m*n的土地上种玉米,但是有的土地很贫瘠,所以不能种,每块土地标为1的表示能种,标为0的表示不能种,并且种玉米的土地不能相邻, 问有多少种合法的种植方案.(全部不种也算一种) 第一道状压,理解了比较久的时间. 就是用二进制的0和1代表土地种还是不种,这样每一行都可以用一个2进制数表示,列数<=12,故最多有2<<

poj 3254 Corn Fields 国家压缩dp

意甲冠军: 要在m行n陆行,有一些格您可以种树,别人做不到的.不相邻的树,我问了一些不同的共同拥有的法律. 分析: 从后往前种,子问题向父问题扩展,当种到某一格时仅仅有他和他后面的n-1个格子的情况对它有影响.故对这n个格子进行编码为状态S,表示种完(多米诺骨牌那题是放置前.注意差别,都可行)这n个格子的状态.父问题由稍小子问题逐步解决,正是动态规划的思想. 代码: //poj 3254 //sep9 #include <iostream> using namespace std; const

POJ 3254 Corn Field ( 状态压缩DP )

简单题,边界处理注意.可以优化,就不精益求精了. #include <iostream> #include <cstring> #include <vector> #include <fstream> using namespace std; #define MOD 100000000 int field[20]; int DP[20][1200]; int main(){ int N, M; cin >> N >> M; memse

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'为符合条