Description
Farmer John bought a new rectangular farm which is divided into M columns and N rows (1 <= m <= 12; 1 <= n <= 12 ), each grid is a square area. FJ intends to plant delicious grass in a few plots of land on the farm for their cows to enjoy. Unfortunately, some land is quite poor and cannot be used for grazing. In addition, the cows like the feeling of monopolizing a lawn, so FJ will not choose two adjacent lands, that is, no two lawns have a public edge. Of course, FJ has not yet decided on which land to plant grass. As a curious farmer, FJ wants to know how many planting schemes are available if the total number of lawns is not considered. Of course, it is also a solution to abandon a new farm and not plant grass on any land. Please help FJ calculate the total number of solutions. Input
* Row 1st: two positive integers m and n, separated by Spaces
* Row 2nd. m + 1: Each line contains N integers separated by spaces, which describe the status of each land. The entered line I + 1 describes the land of line I. If all integers are 0 or 1, if it is 1, it indicates that the land is fertile enough. If it is 0, it indicates that the land is not suitable for planting grass output.
* Row 1st: an integer is output, that is, the total number of farm distribution solutions divided by the remainder of 100,000,000.
Question:
The question allows us to place grass on a matrix of M * n, some locations cannot be placed, and the grass cannot be 4-connected, and the number of solutions is calculated.
Because m, n <= 12, an int can be used to indicate the status of a row.
You can use DP [I] [s] to represent the number of solutions when the status is S.
Then '2 ^ 12*2 ^ 12' enumeration transfer to determine whether it can be transferred.
Σ DP [N] [k] is the answer.
Code:
#include<cstdio>#include<cstring>#include<algorithm>//by zrt//problem:using namespace std;typedef long long LL;LL dp[13][4097];int map[13];// can not useLL m,n;LL mod=1000000000;bool judge(int x){ for(int i=1;i<m;i++){ if(x&(1<<i)){ if((x&(1<<(i-1)))||(x&(1<<(i+1)))) return 0; } } return 1;}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif scanf("%lld%lld",&n,&m); for(int i=1;i<=n;i++){ for(int j=0,x;j<m;j++){ scanf("%d",&x); if(!x) map[i]|=1<<j; } } dp[0][0]=1; for(int i=0;i<n;i++){ for(int j=0;j<(1<<m);j++){ if(!dp[i][j]) continue; for(int k=0;k<(1<<m);k++){ if(judge(k)&&(!(k&map[i+1]))&&!(k&j)){ dp[i+1][k]+=dp[i][j]; if(dp[i+1][k]>=mod) dp[i+1][k]-=mod; } } } } LL ans=0; for(int i=0;i<(1<<m);i++){ ans+=dp[n][i]; if(ans>=mod) ans-=mod; } printf("%lld\n",ans); return 0;}
Bzoj 1725: [usaco Nov] corn fields farm Arrangement