poj 3254 Corn Fields

Corn Fields
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14230 Accepted: 7460

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 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

提示

题意:

农夫约翰有n*m格的农田,约翰想要让农田中播种玉米给牛儿们吃,农田中有贫瘠的土地不可播种玉米。约翰知道牛牛们不喜欢吃自己东西的时候有其他牛在自己身边,因此他想知道有几种播种的方法使得播种的玉米不相邻。(不播种也算一种)

思路:
因为对于玉米地的状态太多需要进行状态压缩再进行dp。仔细想想,把所有可能的每一种情况相加是不是就是答案。(这不废话么)

那么我们设dp[i][j],i为当前第i行,j为当前行即将种植的状态,如5(二进制为101)

dp[i][j]=dp[i][j]+dp[i-1][k],k为上一行存在的一种状态k,如j=5(二进制为101),k=2(二进制为010)。该式子要保证i-1行存在k这一状态,以及该行状态存在j这一状态。

最后计算这里写图片描述取模即可。

给个小技巧,你可以先求出合法种植方式的状态数,可以减少运行时间。

示例程序

Source Code

Problem: 3254		Code Length: 1282B
Memory: 532K		Time: 0MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
#define MOD 100000000
int dp[12][4096],map[12],status[4096];
int judge1(int x)			//它的效果是判断种植方式的合法性
{
    return x&(x<<1);
}
int judge2(int i,int x)		//它的效果是判断该种植方式是否与实际(地图)相符
{
    return ~map[i]&status[x];		//这里为什么要给map取反呢?如map=101,status=001时,至于会产生什么效果留给你思考
}
int main()
{
    int n,m,x,i,i1,i2,top,sum;
    scanf("%d %d",&n,&m);
    memset(dp,0,sizeof(dp));
    sum=0;
    for(i=0;n>i;i++)
    {
        map[i]=0;
        for(i1=0;m>i1;i1++)
        {
            scanf("%d",&x);
            if(x==1)
            {
                map[i]=map[i]+(1<<(m-i1-1));
            }
        }
    }
    top=0;
    for(i=0;(1<<m)>i;i++)		//求出合法的种植方式
    {
        if(judge1(i)==0)
        {
            status[top]=i;
            top++;
        }
    }
    for(i=0;top>i;i++)		//第一行dp
    {
        if(judge2(0,i)==0)
        {
            dp[0][i]=1;
        }
    }
    for(i=1;n>i;i++)			//第i+1行dp
    {
        for(i1=0;top>i1;i1++)
        {
            if(judge2(i,i1)==0)			//是否可以按当前(第i1+1)方式在第i+1行种植
            {
                for(i2=0;top>i2;i2++)
                {
                    if(judge2(i-1,i2)==0&&((status[i1]&status[i2])==0))//上一行存在该(第i2+1)状态且不与当前(第i1+1)状态冲突,即保证两两不相邻
                    {
                        dp[i][i1]=(dp[i][i1]+dp[i-1][i2])%MOD;
                    }
                }
            }
        }
    }
    for(i=0;top>i;i++)
    {
        sum=(sum+dp[n-1][i])%MOD;
    }
    printf("%d\n",sum);
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值