POJ 3254 Corn Fields(状压DP)

来源:互联网 发布:好看的韩国迷你网络剧 编辑:程序博客网 时间:2024/06/07 06:52

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

题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的(用1标记),其他格子则不能放牛(用0标记),并且要求不可以使相邻格子都有牛。求该农夫有多少种放牧方案可以选择(注意:任何格子都不放也是一种选择,不要忘记考虑!)

题解:以 dp[i][state(j)] 来表示对于 前i行 , 第i行 采用 第j种状态 时可以得到的 可行方案总数。状态转移方程dp[i][state(j)]=dp[i-1][state(k1)]+dp[i-1][state(k2)]+……+dp[i-1][state(kn)] (kn即为上一行可行状态的编号,上一行共有n种可行状态)

代码:

#include <iostream>#include <cstring>#include <cstdio>#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>using namespace std;const int mod = 1e9;const int maxn = 505;int m, n, top;//top表示每行最多的状态数int sa[maxn];//sa存放每行所有的可行状态int dp[20][maxn];int c[maxn];//c[i]表示的是第i行整行的情况/**判断第i为是否为1 (这个位是从右数起,从0开始。) if (x&(1<<i) {} 或者 if ((x>>i)&1) {} 。 前面两种写法都是可以的,这里要注意不要忘记了位运算两侧的括号。因为位运算的优先级很低。设置第i位为1 x |= 1<<i;设置第i位为0 x &= ~(1<<i);切换第i位 x ^= 1<<i;*/// x & (x<<1)来判断一个数相邻两位是不是同时为1,假如同时为 1 则返回一个值,否则返回 0//x & y 的布尔值来判断相同为是不是同时为1。int main(){    while (cin >> m >> n)    {        top = 0;        int tot = 1 << n;        for (int i = 0;i < tot;i++)        {            if (!(i&(i << 1)))                sa[top++] = i;        }        memset(dp, 0, sizeof(dp));        for (int i = 1;i <= m;i++)        {            c[i] = 0;            int num;            for (int j = 1;j <= n;j++)            {                cin >> num;                if (!num)                {                    c[i] += (1 << (n - j));                }            }        }        for (int i = 1;i <= top;i++)        {            if (!(sa[i] & c[1]))                dp[1][i] = 1;        }        for (int i = 2;i <= m;i++)        {            for (int j = 1;j <= top;j++) //上一行状态            {                if (!(sa[j] & c[i]))                for (int k = 1;k <= top;k++) //这一行状态                {                    if ((sa[k] & c[i - 1])==0&& (sa[j] & sa[k])==0)                    dp[i][j] = (dp[i][j] + dp[i - 1][k]) % mod;                }            }        }        int res = 0;//累加最后一行所有可能状态的值,即得最终结果        for (int i = 1;i <= top;i++)        {            res = (res + dp[m][i]) % mod;        }        cout << res << endl;    }    return 0;}
原创粉丝点击