Corn Fields
2015-11-25 13:42:33

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 10658   Accepted: 5602

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

 
poj3254 Corn Fields 利用状态压缩d求方案数;
 
 
/**
题意:就是你给一个n行m列的矩阵,矩阵里的元素由0和1组成,1代表肥沃的土地可以种植作物,
0则不可以种植作物,并且相邻的土地不能同时种植作物,问你有多少种种植方案。
定义dp[i][j]表示第i行在j状态时,前i行种植的方法数;
状态转移方程:
dp[i][j] += dp[i-1][k]; //正确性,dp[0][0] = 1,边界;每一个dp[n][j]都是一个根;向下的分支总数就是方案总数;
他们的分支已经包含了到当前位置的所有情况;
答案:
sum += dp[m][j];
*/
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<set>
#include<algorithm>
#include<vector>
#include<map>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<iomanip>
using namespace std;
typedef pair<int,int> pr;
typedef long long ll;
const int maxn = ;
const ll mod = 1e8;
const ll inf = 0x3f3f3f3f;
ll dp[][<<];
ll mp[], valid[<<], vz;
bool ok(ll x,ll i)
{
if((x&mp[i])!=x) return false;// 在x二进制表示,不是mp[i]的子集,则不行;
return true;
}
void getvalid(ll m)//预处理有效的,把一行中存在相邻的状态清除;
{
vz = ;
ll t = (<<m);
for(ll i = ; i < t; i++){
if(((i<<)&i)==){
valid[vz++] = i;
}
}
}
int main()
{
ll n, m;
while(scanf("%lld%lld",&n,&m)==)
{
ll e;
//从(1,1)开始好;
for(ll i = ; i <= n; i++){
mp[i] = ;
for(ll j = ; j <= m; j++) {
scanf("%lld",&e);
mp[i] = (mp[i]<<)+e;
}
}
getvalid(m);
memset(dp,,sizeof(dp));
dp[][] = ;//不种植一棵时,方案数为1;
for(ll i = ; i <= n; i++){
for(ll j = ; j < vz; j++){
if(!ok(valid[j],i)) continue;
for(ll k = ; k < vz; k++){
if(valid[j]&valid[k]) continue;//上下两行不可以有相邻的;
dp[i][valid[j]] = (dp[i][valid[j]]+dp[i-][valid[k]])%mod;
}
}
}
ll sum = ;
for(ll j = ; j < vz; j++){
sum = (sum+dp[n][valid[j]])%mod;
}
printf("%lld\n",sum);
}
return ;
}
 

poj3254 Corn Fields 利用状态压缩求方案数;的更多相关文章

  1. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...

  2. 洛谷P1879 [USACO06NOV]玉米田Corn Fields (状态压缩DP)

    题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...

  3. POJ 3254 Corn Fields(状态压缩)

    一道状态压缩的题,错了好多次....应该先把满足的情况预处理出来 #include<iostream> #include<cstdio> #include<cstring ...

  4. POJ 3254:Corn Fields(状态压缩DP)

    题目大意:一个矩形的草地,分为多个格子,有的格子可以有奶牛(标为1),有的格子不可以放置奶牛(标为0),计算摆放奶牛的方案数. 分析: f[i,j]表示第i行状态为j的方案总数. 状态转移方程f[i, ...

  5. POJ 3254 Corn Fields(状态压缩DP)

    题目大意:给出一个M*N的矩阵,元素为0表示这个地方不能种玉米,为1表示这个地方能种玉米,现在规定所种的玉米不能相邻,即每行或者没列不能有相邻的玉米,问一共有多少种种植方法. 举个例子: 2 3 1 ...

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

    一开始根本不会状压dp,上网各种找题解,但发现他们写的都很......反正我作为一个没有接触过状态压缩的,根本看不懂! 然后看了好多状态压缩的题的题解,总结了一下思路,思路很重要,有了思路转换成计算机 ...

  7. POJ3254 - Corn Fields(状态压缩DP)

    题目大意 给定一个N*M大小的土地,土地有肥沃和贫瘠之分(每个单位土地用0,1来表示贫瘠和肥沃),要求你在肥沃的单位土地上种玉米,如果在某个单位土地上种了玉米,那么与它相邻的四个单位土地是不允许种玉米 ...

  8. poj3254 Corn Fields (状压DP)

    http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

  9. poj3254 Corn Fields

    orn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17989   Accepted: 9474 Descr ...

随机推荐

  1. OpenDigg - 挖掘优质开源项目库

    OpenDigg - 挖掘优质开源项目库 OpenDigg专注于挖掘优质的开源项目库,通过技术和人工将软件项目分类整理,同时辅助简要的编译,方便广大程序员便捷地找到需要的开源项目. OpenDigg刚 ...

  2. win8 下脚本安装IIS

    @echo off      echo 正在添加IIS8.0 功能,依据不同的网络速率,全程大约需要5分钟时间...      start /w pkgmgr /iu:IIS-WebServerRol ...

  3. kernel简介

    内存管理 一般来看有三种类型的地址:物理地址.线性地址和逻辑地址,逻辑地址的精髓在于将地址分成两部分:段基地址+偏移,翻译的过程如下: 线性地址的精髓在于将所有的内存按照一定的大小分成了一页一页,对多 ...

  4. OpenWrt 路由器过滤广告的N种方法

    路由器已经成为每个家庭不可缺少的角色,手机.电脑.电视,凡是需要互联网的设备都要用到它.那么路由器除了给我们的网络设备分发网络外,还有其他用途吗? 现在很多人家里都用着智能路由器,智能路由器究竟怎么智 ...

  5. linux基础-第二十单元_计划任务crond服务

    第二十单元 计划任务crond服务 什么是计划任务:后台运行,到了预定的时间就会自动执行的任务,前提是:事先手动将计划任务设定好.这就用到了crond服务 crond服务相关的软件包[root@MiW ...

  6. Listener监听器之HttpSessionListener

    编写一个OnlineUserListener. package anni; import java.util.List; import javax.servlet.ServletContext; im ...

  7. eclipse sun.net 下包无法导入问题

    项目中用到了:sun.net.ConnectionResetException.但是sun.net包里的类,在eclipse里默认是不让用的. 解决办法是自定义access rules 工程上右键-& ...

  8. Robomongo与MongoDB的故事

    Robomongo,Mongo可视化工具 哇唔,事实上她是三(阴险脸). 你看你看,界面清新,让人家心旷神怡(害羞).谁还想win+R+mongo呀呀呀?! 哎呀呀,继续···说正事. 在这里···借 ...

  9. IIC读写AT24C02代码2——串口命令控制多页读写

    通过串口输入 R .W 进行控制程序读写IIC设备.波特率9600bps,晶振115200HZ. main.c /*------------------------------------------ ...

  10. python——socket模块与列表映射

    从socket模块学习中的一段奇怪代码说起 前言:在学习python标准库中的Socket模块中,发现了一段奇怪的代码. import socket def get_constants(prefix) ...