• 「POJ3254」「LG1879」Corn Fields


    Problem

    Describetion

    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.

    Solution

    思路

    这道题目显然可以状压,然后只要对于每一行枚举他的状态就好了.判断一下行与行,行之间的可行性.最后输出一下就好了

    Code

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<queue>
    #include<iostream>
    #define ll long long
    #define re register
    using namespace std;
    inline int gi(){
    	int f=1,sum=0;char ch=getchar();
    	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
    	return f*sum;
    }
    const int Mod=100000000;
    int S,n,m,F[1000010],a[1000010],dp[20][100010];
    int main(){
    	m=gi();n=gi();S=1<<n;
    	for(re int i=1;i<=m;i++){
    		for(re int j=1;j<=n;j++)
    			a[i]=(a[i]<<1)+gi();
    		//printf("%d
    ",a[i]);
    	}
    	for(re int i=0;i<S;i++){
    		F[i]=(((i<<1)&i) || ((i>>1)&i));
    		dp[1][i]=(!F[i]) && ((i&a[1])==i);
    	}
    	for(re int i=2;i<=m;i++)
    		for(re int j=0;j<S;j++)
    			if(!F[j] && ((j&a[i])==j)){
    				for(re int k=0;k<S;k++)
    					if(!F[k] && (k&a[i-1])==k && ((k&j)==0))
    						dp[i][j]+=dp[i-1][k]%=Mod;
    			}
    	int ans=0;
    	for(re int i=0;i<S;i++)ans+=dp[m][i]%=Mod;
    	printf("%d
    ",ans%=Mod);
    	return 0;
    }
    
  • 相关阅读:
    Java ExecutorService四种线程池的例子与说明
    当代中国社会划分为十大阶层
    各大公司Java面试题超详细总结
    浅谈Java中的hashcode方法
    Java 9的JDK中值得期待的:不仅仅是模块化
    整理Excel数据,10秒搞定别人半天的工作量
    零基础搭建Hadoop大数据处理-环境
    Java性能优化权威指南第三章虚拟机概览第一部分
    15个顶级Java多线程面试题及答案
    最全面的Java多线程用法解析
  • 原文地址:https://www.cnblogs.com/cjgjh/p/9501868.html
Copyright © 2020-2023  润新知