poj3254 Corn Fields

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.

第一道状压dp,看了别人的思路自己写出来了.题目大意是给你一块地,有能放牛的草地也有不能放牛的草地,而且两头牛不能同时安排在相邻的草地上,问有多少种放法,草地长和宽都小于等于12。

这题的思路是这样:先求出每一行的可以放的方案数,用num[i][j]记录下来各个方案所对应的十进制数,用num1[i]记录第i行的方案数,再初始化第一行dp[1][k]为1,然后使i从2到n循环,用dp[i][j]表示第i行第j种放置状态下的总方案数,依次累加,动规方程为dp[i][j]=dp[i][j]+(num[i][j]&num[i-1][k])?0:dp[i-1][k].(k=1,2,...num1[i-1])。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
int num[15][5000],n,m,num1[15],dp[15][5000];
void hang(int i,int temp)
{
	int t=0,j;
	for(j=0;j<(1<<m);j++){
		if(j&(j<<1))continue;
		if(j&temp)continue;
		t++;num[i][t]=j;
	}
	num1[i]=t;
}

int main()
{
	int i,j,c,temp,k,sum;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		memset(num,0,sizeof(num));
		memset(num1,0,sizeof(num1));
		memset(dp,0,sizeof(dp));
		for(i=1;i<=n;i++){
			temp=0;
			for(j=1;j<=m;j++){
				scanf("%d",&c);
				c=1-c;
				temp=temp*2+c;
			}
			hang(i,temp);
		}
        for(j=1;j<=num1[1];j++){
        	dp[1][j]=1;
        }
        for(i=2;i<=n;i++){
        	for(j=1;j<=num1[i];j++){
	        	for(k=1;k<=num1[i-1];k++){
	        		if(num[i-1][k]&num[i][j])continue;
	        		dp[i][j]+=dp[i-1][k];
	        	}
	        }
        }
        sum=0;
        for(j=1;j<=num1[n];j++){
        	sum=(sum+dp[n][j])%100000000;
        }
        printf("%d\n",sum);
	}
	return 0;
}
时间: 2025-02-01 17:49:12

poj3254 Corn Fields的相关文章

POJ3254 Corn Fields 状态压缩DP

题目大意是在一块M行N列的农场上种谷物,但是不希望彼此相邻(共用一条边),并且有些地方不能种植谷物,给定M,N(范围都不超过12)以及一些不能种谷物的位置,求出一共有多少种方法种谷物. 状态压缩DP,设dp(i, k) 为种到第i行时,第i行状态为k的总共方案数,可以知道dp(i, k) = ∑dp(i -1, k'),其中我们要判断彼此相邻的情况以及不能种植的情况即可. #include <stdio.h> #include <vector> #include <math.

poj3254 Corn Fields (状压DP)

http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7588   Accepted: 4050 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parc

【状压DP】poj3254 Corn Fields

题意: 一块n*m的田,1表示这个地方可以种植,0代表这个地方不能种植.植物种植还必须满足两株植物不能相邻(横竖都不行).问共有几种种植方法,而且当什么都不种时认为是一种方法. 解题思路: 种植用1表示,不种植用0表示.每一行的情况就可以用一个二进制数state来存储.state的范围是 [0 ~ 1<< state). dp[i][state]表示第i行状态为state的情况下满足条件的数目. 状态转移方程为:dp[i][state] += dp[i-1][pre_state];这个stat

POJ 3254 poj3254 Corn Fields

题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法. 思路: DP[i][j]=sum(dp[i-1][k]); i表示当前这一行,状态为j有多少种方案 首先,i行能放牛的状态由前一行i-1决定.所以我们只要知道前一行的状态就知道这一行的方案.因此要初始化第一行的状态dp[1][i] 要使当前这一行能用状态j表示则应瞒足两种条件 1 与当前这一行的地形符合 2 这一行状态与前一行的某一状态不冲突 x&(x<<1

状态压缩dp入门 (poj3254 Corn Fields)

题目链接:http://poj.org/problem?id=3254 题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法. 分析:假如我们知道第 i-1 行的所有的可以放的情况,那么对于第 i 行的可以放的一种情况,我们只要判断它和 i - 1 行的所有情况的能不能满足题目的所有牛不相邻,如果有种中满足,那么对于 i 行的这一中情况有 x 中放法. 前面分析可知满足子状态,我们我们确定可以用dp来解决. 但是我们又发现

【原创】【状态压缩DP】POJ3254 Corn Fields【新手向】

一开始根本不会状压dp,上网各种找题解,但发现他们写的都很......反正我作为一个没有接触过状态压缩的,根本看不懂! 然后看了好多状态压缩的题的题解,总结了一下思路,思路很重要,有了思路转换成计算机语言就好了.因此我先讲一下思路: 先说说地图,地图上每一行的01代表一个状态,比如输入样例中的111.010,表示第一行的三个位置都可以种稻子,第二行中间的位置可以种稻子,然后,不能种稻子的地方一定不能种稻子(废话...) 可以种稻子的地方可以选择种也可以选择不种,然后有一个前提条件,就是上下左右相

poj3254(Corn Fields)状压dp

题意:在n*m(1<=n,m<=12)的矩阵上种植玉米,任意共边的方格不能同时种,并且有些特定方格也不能种.问能有多少种种植的方案: 解法:很经典的状压模型.先将每一行的合法状态求出来,12的时候最多377个合法状态.然后进行与行之间的状态转移.最坏复杂度12*(377^2) 代码: /**************************************************** * author:xiefubao **********************************

POJ3254:Corn Fields(状态压缩)

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

Corn Fields

poj3254:http://poj.org/problem?id=3254 题意:给以n*m的方格,方格中有1或者0,在1的地方可以放置一个物品,但是在物品的上下左右不能有不物品,也可以不放,问你最够有多少种放法. 题解:很简单的状态dp.这里先统计数最多能放多少个,然后和棋盘放k的kind那题一样,最后的结果就是第n行各种状态以及放置0到最多的种类之和.其实,这一题,个数那一维是完全可以删除的,只要2维就可以了. 1 #include<iostream> 2 #include<cst