状态压缩dp入门 第一题 POJ 3254 Corn Fields

Corn Fields

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 6460   Accepted: 3436

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.

题意:农夫FJ有一块n行m列的矩形土地, 有的土地是肥沃的,可以在这些土地上放牛(用1表示),有的土地不能放牛(用0表示),而且相邻的可以放牛的格子不能同时有牛,问FJ一共有多少种放牛的方案(一头牛都不放也是一种方案)。

分析:以样例为例,我们可以一行一行的考虑。假如对于每一行,用1表示放牛,0表示不放牛,

第一行的状态为:000001
010011(舍弃) 100
101110(舍弃)
111(舍弃)

符合题意的状态只有5个,所以第一行有5种方案。

第二行的状态为:000010

但是第二行中的010和第一行中的010冲突,所以如果第二行状态为010时,共有4种方案;状态为000时,有5种方案,所以一共有4+5=9种方案。

分析完我们会发现,对于每一行,都可以一个01串来表示这一行的状态,而这个状态可以用一个十进制整数来代替,也就是说,把这个状态压缩成了一个十进制整数,所以称为是状态压缩。

本题中,dp[i][j] 就表示第i行状态为j时的方案数。

状态压缩dp中最常见的就是位运算,因为位运算可以很方便的判断当前状态是否合法。例如本题中判断第i行是不是有两块相邻的土地同时都有牛,假设当前状态为X,那么只需要判断X&(X>>1)或者X&(X<<1)的结果是不是0,如果是0,说明没有相邻的,否则就说明有相邻的。

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
#define mod 100000000
const int N = 1<<12 + 4;
int dp[15][N], Map[15][15];
int n, m;
vector<int> vec[14];
int Pow(int a, int x) //2的X次方
{
    int s = 1;
    for(int i = 1; i <= x; i++)
        s <<= 1;
    return s;
}
int fun(int x) //求第X行的土地状态,0表示可以放牛,1表示不能放牛
{
    int s = 0;
    for(int i = 1; i <= m; i++)
        s += (!Map[x][i]) * Pow(2, m-i);
    return s;
}
int main()
{
    int i, j;
    while(~scanf("%d%d",&n,&m))
    {
        memset(dp, 0, sizeof(dp));
        memset(vec, 0, sizeof(vec));
        for(i = 1; i <= n; i++)
            for(j = 1; j <= m; j++)
                scanf("%d",&Map[i][j]);
        vec[0].push_back(0);
        int k = 1<<m;
        for(i = 0; i < k; i++)
            dp[0][i] = 1;
        for(i = 1; i <= n; i++)
        {
            int tmp = fun(i); //当前行的状态
            for(j = 0; j < k; j++)
            {
                if(j & (j>>1)) continue; //j的二进制表示中有两个相邻的1
                if(j & tmp) continue; //排除在当前行不符合条件的
                vec[i].push_back(j);
            }
            for(j = 0; j < vec[i].size(); j++) //排除和上一行冲突的
            {
                int u = vec[i][j];
                for(int z = 0; z < vec[i-1].size(); z++)
                {
                    int v = vec[i-1][z];
                    if(u & v) continue;
                    dp[i][u] = (dp[i][u] + dp[i-1][v]) % mod;
                }
            }
        }
        int ans = 0;
        for(i = 0; i < k; i++)
            ans = (ans + dp[n][i]) % mod;
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-25 08:33:32

状态压缩dp入门 第一题 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  .(为啥取反

Hdu-1565 方格取数(1) (状态压缩dp入门题

方格取数(1) Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4702    Accepted Submission(s): 1782 Problem Description 给你一个n*n的格子的棋盘,每个格子里面有一个非负数. 从中取出若干个数,使得任意的两个数所在的格子没有公共边,就是说所取的数所在的2个格子不能相邻,并且取出

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

hoj 2662 Pieces Assignment 状态压缩dp入门

//hoj 2662 Pieces Assignment //有一个n*m的棋盘(n.m≤80,n*m≤80)要在棋盘上放k(k≤20)个棋子,使得任意两 //个棋子不相邻(每个棋子最多和周围4个棋子相邻).求合法的方案总数. // //算是另一个状态压缩dp入门吧 //dp[i][S][j]表示第i行的棋子状态是S(整数的二进制形式,比如5为 // ...101,省略号表示前导0,那一位上是1就表示哪一列上放了棋子,5表示第一列 //第三列放了棋子.)时已经放了j颗棋子,则转移方程为: //d

POJ 3254 Corn Fields //入门状压dp

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7578   Accepted: 4045 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】

[题目大意]一个矩阵里有很多格子,每个格子有两种状态,可以放牧和不可以放牧,可以放牧用1表示,否则用0表示,在这块牧场放牛,要求两个相邻的方格不能同时放牛,即牛与牛不能相邻.问有多少种放牛方案(一头牛都不放也是一种方案) [解析]根据题意,把每一行的状态用二进制的数表示,0代表不在这块放牛,1表示在这一块放牛.首先很容易看到,每一行的状态要符合牧场的硬件条件,即牛必须放在能放牧的方格上.这样就能排除一些状态.另外,牛与牛之间不能相邻,这样就要求每一行中不能存在两个相邻的1,这样也能排除很多状态.

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: 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(状态压缩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