当前位置: 代码迷 >> 综合 >> POJ3254Corn Fields【状压dp】
  详细解决方案

POJ3254Corn Fields【状压dp】

热度:39   发布时间:2023-12-17 08:48:00.0
Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10153   Accepted: 5369

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 34  

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.
题意:在给出一片地有肥沃的地有贫瘠的地只能在肥沃的地里放牛并且相邻两块肥沃的土地不能同时放牛问有多少种放牛方案状压dp
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int MOD=100000000;
int dp[13][1<<13];
int state[1<<13],map[1<<13];
bool judge(int pos){//判断相邻两个是否相同; return pos&(pos<<1);
}
bool check(int a,int b){//判断相同位是否同时为1;return map[a]&state[b];
}
bool ok(int a,int b){return a&b;
}
int main()
{int i,j,n,m,k;while(scanf("%d%d",&n,&m)!=EOF){memset(dp,0,sizeof(dp));memset(map,0,sizeof(map));memset(state,0,sizeof(state));for(i=1;i<=n;++i){for(j=1;j<=m;++j){scanf("%d",&k);if(!k){map[i]+=(1<<(j-1));}}}for(k=0,i=0;i<(1<<m);++i){if(!judge(i)){state[k++]=i;//记录相邻两个不相同的所有状态;}}for(i=0;i<k;++i){if(!check(1,i)){dp[1][i]=1;//第1行状态为i时的方案数;}}for(i=2;i<=n;++i){//第i行 for(j=0;j<k;++j){//状态jif(check(i,j))continue; //判断状态j是否满足第i行; for(int t=0;t<k;++t){if(check(i-1,t))continue;//判断状态t是否满足第i-1行; if(!ok(state[j],state[t]))//判断上下是否有相同的情况 dp[i][j]+=dp[i-1][t]; }}}int ans=0;for(i=0;i<k;++i){//计算最后一行的方案数 ans+=dp[n][i];ans%=MOD;}printf("%d\n",ans);}return 0;} 

  相关解决方案