poj3254 Corn Fields
来源:互联网 发布:中国在中东知乎 编辑:程序博客网 时间:2024/05/16 10:37
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.
#include <iostream>#include<stdio.h>#include<string.h>using namespace std;int allstate,state[400],statenum,map[12][12],dp[12][400],m,n,maxall;int statecan(int i, int j)//第i行,第j种状态{ int k; for(k=0;k<m;k++) { if(((map[i][m-k-1]==0))&&(state[j]&(1<<k)))//如果有一个是 { return 0; } } return 1;}void init(){ int allnum=(1<<m)-1,i,j,temp; bool flag ; statenum=0; for(i=0;i<=allnum;i++)//得到所有的状态数377 { flag=true; for(j=0;j<=m-1;j++) { if((i&(1<<j))&&(i&(1<<j+1))) { flag=false; break; } } if(flag) { state[statenum++]=i; } } memset(dp,0,sizeof(dp)); maxall=0; for(i=0;i<n;i++) for(j=0;j<m;j++) { scanf("%d",&map[i][j]);//1表示能放,0表示不能放 } for(i=0;i<statenum;i++)//第一行进行初始化 { if(statecan(0,i)) dp[0][i]=1; }}void makedp(){ int i,j,k; for(i=1;i<n;i++) { for(j=0;j<statenum;j++) { if(statecan(i,j)) { for(k=0;k<statenum;k++) { if(!(state[j]&state[k])) { dp[i][j]=dp[i][j]+dp[i-1][k]; } } } } }}int main(){ __int64 re; int i,j; while(scanf("%d%d",&n,&m)!=EOF) { init(); makedp(); re=0; for(i=0;i<statenum;i++) { re+=dp[n-1][i]; } printf("%I64d\n",re%100000000); } return 0;}
- 【DP】 poj3254 Corn Fields
- DP poj3254 Corn Fields
- poj3254 Corn Fields
- POJ3254--Corn Fields
- poj3254 Corn Fields
- POJ3254--Corn Fields
- 【状压】Corn Fields POJ3254
- POJ3254 Corn Fields
- [POJ3254] Corn Fields
- 【POJ3254】Corn Fields
- POJ3254 Corn Fields
- poj3254 Corn Fields
- POJ3254--Corn Fields
- poj3254 Corn Fields
- poj3254 Corn Fields
- POJ3254 Corn Fields
- POJ3254 Corn Fields
- POJ3254:Corn Fields
- linux scp使用详解
- goto,void,extern,sizeof分析
- 学会科学的制定项目质量计划工作的输人和输出
- Using apt-file to track down build errors(fatal error: X11/Intrinsic.h: No such file or directory)
- 杭电 1312 Red and Black
- poj3254 Corn Fields
- HTML 布局常见问题
- 敏捷开发_WorkBalance
- 逻辑运算符&&、||、!使用分析
- Vim+cscope+ctags+tags阅读源代码
- sphinx使用心得
- 【二分+广搜】poj 2485 Highways
- 位运算符分析
- Java中的多态的详细讲解