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 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

题意:

一个n*m的矩阵,每个格子是0或者1,1表示土壤肥沃可以种植草地,0则不可以。在种草地的格子可以放牛,但边相邻的两个格子不允许同时放牛,问总共有多少种放牛的方法?(不放牛也算一种情况)

题解:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<set>
#include<queue>
#include<stack>
#include<vector>
#include<map>

#define N 100010
#define Mod 100000000
#define lson l,mid,idx<<1
#define rson mid+1,r,idx<<1|1
#define lc idx<<1
#define rc idx<<1|1
const double EPS = 1e-11;
const double PI = acos ( -1.0 );
const double E = 2.718281828;
typedef long long ll;

const int INF = 1000010;

using namespace std;

int a[14], dp[14][1 << 12 + 1];
int cnt[1010];
int n, m;
int len;

bool ok ( int x )///判断同行是否有相邻的
{
    if ( x & ( x << 1 ) || ( x & ( x >> 1 ) ) ) return false;
    return true;
}

void build()
{
    len = 0;
    for ( int i = 0; i < ( 1 << m ); i++ )
    {
        if ( ok ( i ) )
        {
            cnt[len++] = i;
        }
    }
}

int main()
{
    while ( cin >> n >> m )
    {
        memset ( a, 0, sizeof a );
        int x;
        for ( int i = 0; i < n; i++ )
            for ( int j = 0; j < m; j++ )
            {
                cin >> x;
                if ( !x )
                    a[i] |= ( 1 << j );
            }
        memset ( dp, 0, sizeof dp );
        build();
        for ( int i = 0; i < len; i++ )
            if ( ! ( cnt[i]&a[0] ) )
                dp[0][i] = 1;
        for ( int i = 1; i < n; i++ )
        {
            for ( int j = 0; j < len; j++ )
            {
                if ( a[i] & cnt[j] )
                    continue;
                for ( int k = 0; k < len; k++ )
                {
                    if ( cnt[k]&cnt[j] || ( cnt[k]&a[i - 1] ) )
                        continue;
                    dp[i][j] += dp[i - 1][k];
                    dp[i][j] %= Mod;
                }
            }
        }
        int ans = 0;
        for ( int i = 0; i < len; i++ )
        {
            ans += dp[n - 1][i];
            ans %= Mod;
        }
        cout << ans << endl;
    }
    return 0;
}
时间: 2024-11-05 02:26:35

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

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