(简单) POJ 3254 Corn Fields,状压DP。
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.
题目就是求方案数,一看数据范围,差不多就是状态压缩啥的,然后DP就好。
dp[i][j] 表示前i列,第i列的分布状态为j时的方案数。
代码如下:
// ━━━━━━神兽出没━━━━━━ // ┏┓ ┏┓ // ┏┛┻━━━━━━━┛┻┓ // ┃ ┃ // ┃ ━ ┃ // ████━████ ┃ // ┃ ┃ // ┃ ┻ ┃ // ┃ ┃ // ┗━┓ ┏━┛ // ┃ ┃ // ┃ ┃ // ┃ ┗━━━┓ // ┃ ┣┓ // ┃ ┏┛ // ┗┓┓┏━━━━━┳┓┏┛ // ┃┫┫ ┃┫┫ // ┗┻┛ ┗┻┛ // // ━━━━━━感觉萌萌哒━━━━━━ // Author : WhyWhy // Created Time : 2015年07月21日 星期二 09时43分34秒 // File Name : 3254.cpp #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> using namespace std; const int mod=100000000; int N,M; int map1[15][15]; int rem[15]; long long dp[15][5000]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); long long ans; while(~scanf("%d %d",&N,&M)) { for(int i=1;i<=N;++i) for(int j=1;j<=M;++j) scanf("%d",&map1[i][j]); for(int i=1;i<=M;++i) { rem[i]=0; for(int j=1;j<=N;++j) if(!map1[j][i]) rem[i]|=(1<<(j-1)); } memset(dp,0,sizeof(dp)); for(int j=0;j<(1<<N);++j) if(!(j&rem[1] || j&(j<<1))) ++dp[1][j]; for(int i=2;i<=M;++i) for(int j=0;j<(1<<N);++j) { if(j&(j<<1) || j&rem[i]) continue; for(int k=0;k<(1<<N);++k) { if(j&k) continue; (dp[i][j]+=dp[i-1][k])%=mod; } } ans=0; for(int j=0;j<(1<<N);++j) (ans+=dp[M][j])%=mod; printf("%lld\n",ans); } return 0; }