poj 3254 Corn Fields
来源:互联网 发布:冰箱什么牌子静音 知乎 编辑:程序博客网 时间:2024/06/05 17:58
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
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
Sample Input
2 31 1 10 1 0
Sample Output
9
Hint
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.
题意:一个n*m的矩阵,每个格子是0或者1,1表示土壤肥沃可以种植草地,0则不可以。在种草地的格子可以放牛,但边相邻的两个格子不允许同时放牛,问总共有多少种放牛的方法?(不放牛也算一种情况)
思路:这是一道状态压缩dp;设dp[i][j]为前i行,第i行状态为j的方法总数,则状态转移方程为:dp[i][j]+=dp[i-1][k](其中j与k不冲突);
代码如下:
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MOD = 100000000;int dp[15][500],num[500],n,m,cnt,map[15];bool ok(int x){ if(x&(x<<1))return false; return true;}void init(){ memset(num,0,sizeof(num)); for(int i=0;i<(1<<n);i++) { if(ok(i)) num[cnt++]=i; }}int main(){int i,j,k,t; while(scanf("%d%d",&m,&n)!=EOF) { for(i=0;i<m;i++) for( j=0;j<n;j++) { scanf("%d",&t); if(t==0)map[i]=map[i]|(1<<j); } cnt=0; init(); for(i=0;i<cnt;i++) if(!(num[i]&map[0])) dp[0][i]=1; for(i=1;i<m;i++) { for(j=0;j<cnt;j++) { if(map[i-1]&num[j])continue; for(k=0;k<cnt;k++) { if(map[i]&num[k])continue; if(num[k]&num[j]) continue; dp[i][k]=(dp[i][k]+dp[i-1][j])%MOD; } } } int ans=0; for(int i=0;i<cnt;i++) ans=(ans+dp[m-1][i])%MOD; printf("%d\n",ans); }}
- poj 3254Corn Fields
- POJ 3254 Corn Fields
- poj 3254 Corn Fields
- POJ 3254 Corn Fields
- poj 3254---Corn Fields
- poj 3254 Corn Fields
- poj 3254 Corn Fields
- POJ 3254 Corn Fields
- (poj)3254 Corn Fields
- poj 3254 Corn Fields
- POJ 3254 Corn Fields
- poj 3254 Corn Fields
- POJ 3254 Corn Fields
- poj 3254 Corn Fields
- POJ 3254 Corn Fields
- POJ 3254 Corn Fields
- poj-3254-Corn Fields
- poj 3254 Corn Fields
- Spring 实现容器销毁该bean的时候获得一次回调
- iDLNA
- 浏览器对象
- 在centos7/ubuntu15.04 上通过bosh-lite 搭建单机环境Cloud Foundry
- IE6中CSS方法实现PNG图片透明
- poj 3254 Corn Fields
- Serivce通知Activity更新UI方法总结
- CMake/CPack变量之CPACK_SET_DESTDIR
- JAVA当中的I/O
- cocos2dx 3.x win7+VS2012开发环境搭建及HelloWorld
- 为Bootstrap模态对话框添加拖拽移动功能
- 快速排序
- volatile 保证变量内存中值的同步
- linux下搭建vsftp,并使自动作业上传文件到ftp