POJ 3254 状态压缩DP

来源:互联网 发布:pandora软件 编辑:程序博客网 时间:2024/04/30 17:03

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11442 Accepted: 5997
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.

题意:

有一片区域,区域里有的土地肥沃有的贫瘠。把一些牛放到肥沃的土地上,要求牛不能相邻。问最多有多少种放法。

题解:

状态压缩
这一类题目的特点是通常数字都不大(通常小于31),而且通过转化每一个点的位置上只有两种不同的状态。这样就可以用一个数字来记录多个点组成的一个状态,通过一些位运算来执行一些操作。

本题中。M,N都小于31。我们可以用一个枚举每一行放牛的方式,判断是否可以这么放后。当前这行这时的状态等于上一行所有不和其冲突的状态的和。最后递推到最后一行就是结果。

#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <stack>#include <string>#include <set>#include <cmath>#include <map>#include <queue>#include <sstream>#include <vector>#include <iomanip>#define m0(a) memset(a,0,sizeof(a))#define mm(a) memset(a,0x3f,sizeof(a))#define m_1(a) memset(a,-1,sizeof(a))#define f(i,a,b) for(i = a;i<=b;i++)#define fi(i,a,b) for(i = a;i>=b;i--)#define lowbit(a) ((a)&(-a))#define FFR freopen("data.in","r",stdin)#define FFW freopen("data.out","w",stdout)#define INF 0x3f3f3f3ftypedef long long ll;typedef long double ld;const ld PI = acos(-1.0);using namespace std;#define SIZE ( )const ll MOD = 100000000;ll dp[20][1 << 14];int G[20][20];int n,m;inline bool IsTrue(int a, int b) {    int i;    f(i, 0, m-1)        if (G[a][i] == 0 && ((1 << i)&b))            return false;    return true;}int main(){    //ios_base::sync_with_stdio(false); cin.tie(0);    while (~scanf("%d%d",&n,&m))    {        int i, j,k;        f(i, 1, n)            f(j, 0, m-1)                scanf("%d", &G[i][j]);        m0(dp);        f(i, 0, (1 << m) - 1)            if (IsTrue(1, i))                if (i&(i << 1));                else                    dp[1][i] = 1;        f(i,2,n)            f(j, 0, (1 << m) - 1)                if (!(j&(j<<1))&&IsTrue(i, j)) {                    f(k, 0, (1 << m) - 1)                        if (k&j);                        else {                            dp[i][j] = (dp[i][j] + dp[i - 1][k]) % MOD;                        }                }        ll ans = 0;        f(i, 0, (1 << m) - 1) ans = (ans + dp[n][i]) % MOD;        printf("%I64d\n", ans);    }    return 0;}
0 0
原创粉丝点击