zoukankan      html  css  js  c++  java
  • 【poj3254】 Corn Fields

    http://poj.org/problem?id=3254 (题目链接)

    题意

      给出一块n*m的田地,有些能够耕种,有些不能。要求将牛两两不相邻的放在田中,牛的个数至少为1个。问有多少种放法。

    Solution

      状压dp水题。

      f[i][j]表示第i行状态为j时,前i行的总方案数。

    代码

    // poj3254
    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #define LL long long
    #define MOD 100000000
    #define inf 2147483640
    #define Pi acos(-1.0)
    #define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
    using namespace std;
    
    int a[1000010],f[20][100010],n,m;
    
    bool check(int x,int flag) {
    	if ((a[x]&flag)!=flag) return 0;   //是否符合地图
    	if ((flag&(flag<<1))!=0 || (flag&(flag>>1))!=0) return 0;   //是否有相邻
    	return 1;
    }
    int main() {
    	scanf("%d%d",&n,&m);
    	for (int i=1;i<=n;i++) {
    		int x;a[i]=0;
    		for (int j=1;j<=m;j++) {
    			scanf("%d",&x);
    			a[i]=(a[i]<<1)+x;
    		}
    	}
    	f[0][0]=1;
    	int maxl=1<<m;
    	for (int i=1;i<=n;i++) 
    		for (int j=0;j<maxl;j++) {   //枚举当前行状态
    			if (check(i,j)==0) continue;
    			for (int k=0;k<maxl;k++) {   //枚举上一行状态
    				if ((j&k)!=0) continue;
    				f[i][j]=f[i][j]+f[i-1][k];
    				f[i][j]%=MOD;
    			}
    		}
    	int ans=0;
    	for (int i=0;i<maxl;i++) ans=(ans+f[n][i])%MOD;
    	printf("%d
    ",ans);
    	return 0;
    }
    

      

  • 相关阅读:
    高仿爱鲜蜂购物应用源码
    控制ClistCtrl的滚动的位置
    VC保存当面某个区域的图片
    MFC 屏幕截图方法
    回调函数使用(三)
    回调函数使用方法二
    VS2010编译Boost 1.57 静态链接库
    Windows7+VS2010下OpenGL的环境配置
    CxImage图像库的使用 .
    VS2010+PCL配置
  • 原文地址:https://www.cnblogs.com/MashiroSky/p/5928331.html
Copyright © 2011-2022 走看看