您的位置:首页 > 其它

(状态DP)Corn Fields

2014-08-12 21:16 183 查看


Corn Fields


Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)


Total Submission(s) : 17 Accepted Submission(s) : 13


Problem 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


Source

PKU

题目大意:有一个"棋盘",有些格子可以"放子",有些不可以,在每个子都不相邻的前提下,问有多少种"放子"方案?
分析:二进制法压缩每一行的棋盘状态和"放子"方案, DP求总方案数.
#include <iostream>
#include <cstdio>
#include <cstring>
#include <bitset>
#include <algorithm>
using namespace std;
#define maxn (1<<12)+5
#define PRIME 100000000
int dp[15][maxn];
int stk[maxn],top;
int M, N;
int a[15];
int OK(int x)
{
if (x&(x << 1))
return 0;
return 1;
}
void FindStatu(int n)
{
top = 0;
for (int i = 0; i < 1 << n;i++)
if (OK(i))
stk[top++] = i;
}
void Solve()
{
FindStatu(M);

memset(dp, 0, sizeof(dp));
for (int i = 0; i < top; i++)
if ((stk[i] & a[0]) == stk[i])
dp[0][stk[i]] = 1;

for (int r = 0; r < N - 1;r++)
for (int i = 0; i < top; i++)
{
if (dp[r][stk[i]])
{
for (int j = 0; j < top;j++)
if ((stk[j] & a[r + 1]) == stk[j])
if (!(stk[j] & stk[i]))
dp[r + 1][stk[j]] += dp[r][stk[i]];
}
}

}
int main()
{
freopen("f:\\input.txt", "r", stdin);
memset(a, 0, sizeof(a));
scanf("%d%d", &N, &M);
for (int i = 0; i < N;i++)
for (int j = 0; j < M; j++)
{
int temp;
scanf("%d", &temp);
a[i] = (a[i] << 1) | temp;
}

Solve();

int ans = 0;
for (int i = 0; i < top; i++)
ans = (ans + dp[N - 1][stk[i]] % PRIME) % PRIME;
printf("%d\n", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: