poj-3254-状态压缩入门-Corn Fields

来源:互联网 发布:手机个人热点软件 编辑:程序博客网 时间:2024/06/04 23:32
Corn Fields

Description

Farmer John has purchased a lush new rectangular pasture composed of M byN (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 andN
Lines 2..M+1: Line i+1 describes row i of the pasture withN 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 31 1 10 1 0

Sample Output

9

Hint

Number the squares as follows:
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.

Source

USACO 2006 November Gold

题意:有M*N的土地,1表示能种庄稼,0则不能,而且庄稼不能相邻
详解:点击打开链接


#include<stdio.h>#include<string.h>int M,N;int state[4000];    //用于存储合法状态,比如1,010,101,1010,0101等int cur[15];        //用于存储每行相反的状态,用十进制表示int top;void Init(){    top=0;    for(int i=0; i<(1<<N); i++)    {        if(i&i<<1)         //用于判断i的二进制是否存在相邻的1                 continue;        state[++top]=i;     //如果不存在说明是合法状态    }}bool fit(int t,int i){    if(t&cur[i])        //用于判断状态t和第i行是否会发生冲突        return false;    return true;        //不会发生冲突}int main(){    int i,j,num,dp[15][4000],k;       //dp[i][j]表示第i行的第j中状态有多少种放牧方式    while(scanf("%d%d",&M,&N)!=EOF)    {        memset(dp,0,sizeof(dp));        memset(cur,0,sizeof(cur));        memset(state,0,sizeof(state));        Init();        for(i=1; i<=M; i++)        {            for(j=1; j<=N; j++)            {                scanf("%d",&num);                if(num==0)                cur[i]+=1<<(N-j);   //将每行的状态以相反的形式存,(存的是十进制)            }        }        for(i=1; i<=top; i++)        {            if(fit(state[i],1))            {                dp[1][i]=1;     //第一行的每种可行状态的放牧方式都只有一种            }        }        for(i=2; i<=M; i++)        {            for(j=1; j<=top; j++)            {                if(!fit(state[j],i))     //求出第i行的可行状态state[j]                    continue;                for(k=1; k<=top; k++)                {                    if(!fit(state[k],i-1))      //找到i-1行的可行状态state[k]                        continue;                    if(state[k]&state[j])  //如果i行的可行状态(j)与i-1行的可行状态(k)不冲突                        continue;                    dp[i][j]=dp[i][j]+dp[i-1][k];                }            }        }        int ans=0;        for(i=1; i<=top; i++)        {            ans=(ans+dp[M][i])%100000000;        }        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击