poj3254 Corn Fields (状压dp)
2016-07-15 20:20
323 查看
Corn Fields
DescriptionFarmer 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.
InputLine 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)OutputLine 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.Sample Input2 3
1 1 1
0 1 0Sample Output9
就是一个状压dp
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 12463 | Accepted: 6548 |
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.
InputLine 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)OutputLine 1: One integer: the number of ways that FJ can choose the squares modulo 100,000,000.Sample Input2 3
1 1 1
0 1 0Sample Output9
就是一个状压dp
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const int mod=(int)1e8; int n,m; int a[13]; int dp[13][1<<13]; bool J(int i,int j) { if((a[i]&j)^j)return false; if((j<<1)&j)return false; return true; } int main() { while(~scanf("%d%d",&n,&m)) { memset(a,0,sizeof(a)); for(int i=1; i<=n; ++i) { for(int j=1; j<=m; ++j) { int num; scanf("%d",&num); a[i]=(a[i]<<1)+num; } } memset(dp,0,sizeof(dp)); dp[0][0]=1; int ans=0; for(int i=1; i<=n; ++i) { for(int j=0; j<(1<<m); ++j) { if(!J(i,j))continue; for(int k=0; k<(1<<m); ++k) { if(j&k)continue; dp[i][j]+=dp[i-1][k]; dp[i][j]%=mod; } } } for(int i=0; i<(1<<m); ++i) { (ans+=dp [i])%=mod; } printf("%d\n",ans); } return 0; }
相关文章推荐
- 随机森林算法的源码分析(一)
- [noip模拟题]合理种植
- Codeforces Round #362 (Div. 2) B 模拟
- 指针数组 数组指针 指针函数 函数指针
- UVA 10916 Factstone Benchmark
- 如何在 Linux 上永久挂载一个 Windows 共享
- Android studio libpng error: Not a PNG file
- 运算符和表达式
- jzoj 1568. 【普及模拟】石子游戏 解题报告
- csu 1110 RMQ with Shifts 线段树
- HTTP
- Java JVM 内存泄漏--全解析和处理办法 [ 转载 ]
- centos6.5 下安装 sqlplus
- 关于 const,以下正确的是
- Hibernate 简单使用(三)一对一关联映射
- 【HIHOCODER】The Cats' Feeding Spots
- 如何让多个输入框对齐
- 【AD】取消普通域用户帐号加域权限&授权特定普通域用户加域权限
- android service 学习
- UVA-10815 Andy's First Dictionary