标签: ACM


题目:

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.

题意:

第一行输入草地长宽,后面输入该地方能不能使用,输出可以使用的所有方案

解题思路

从例题来看第一层有五种可能分别为000,001,010,100,101,都标记为1种可能

第二层可以有000010两种状态,但是与上一层比较000与上一层五种状态都不冲突标记为5种可能,而010与上一层010状态冲突,所以标记为4种可能

第二层为最底层,将最后一层的可能性全部相加得到9

使用状态压缩,将所有可能存在状态储存到数组里面

然后从第一层存在的状态标记为1

从第二层开始遍历到最后一层,第二层存在的状态且不和上一层冲突将上一层的状态标记加到该层的标记上

遍历到最后一层时将最后一层的状态总和加起来就是所有的可能性

注:根据题意答案要对100000000取余

AC代码

#include <iostream>
#include <string.h>
#define M 4100
#define N 15
using namespace std;
int map[N]; //该行的输入状态
int m,n;
int dp[N][M];
int p;//该列最大状态
int s[M]; //储存每一行拥有的状态最大4096种状态
int mod=100000000;
bool checkLine(int i) //该行是否满足条件
{
return !(i&(i>>1));
}
bool checkTwoLine(int i,int j) //与上一行是否冲突
{
return !(i&j);
}
bool include(int i,int j) //是否是包含关系
{
return ((i|j)==i);
}
void init()
{
p=0;
int i,j;
for(i=0;i<(1<<m);i++)
if(checkLine(i))
s[p++]=i;
}
void solve()
{
int i,j,k;
int ans=0;
for(i=0;i<p;i++)
if(include(map[0],s[i]))
dp[0][i]=1; for(i=1;i<n;i++)
for(j=0;j<p;j++) //该行的状态
{
if(!include(map[i],s[j]))
continue;
else
for(k=0;k<p;k++) //上一行的状态
{
if(include(map[i-1],s[k])&&checkTwoLine(s[j],s[k]))
dp[i][j]=(dp[i][j]+dp[i-1][k])%mod;
}
}
for(i=0;i<p;i++)
ans=(ans+dp[n-1][i])%mod;
cout<<ans<<endl;
}
int main()
{
while(cin>>n>>m)
{
memset(map,0,sizeof(map));
int i,j;
for(i=0;i<n;i++)
for(j=0;j<m;j++)
{
int plant;cin>>plant;
if(plant){
map[i]+=(1<<j); //将输入转换成二进制储存
}
}
init();
solve();
} return 0;
}

状态压缩---状态压缩dp第一题的更多相关文章

  1. 状态压缩dp第一题

    标签: ACM 题目: Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; ...

  2. 状态压缩---区间dp第一题

    标签: ACM 题目 Gappu has a very busy weekend ahead of him. Because, next weekend is Halloween, and he is ...

  3. TTTTTTTTTTT hdu 1520 Anniversary party 生日party 树形dp第一题

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. HihoCoder 1055 : 刷油漆 树形DP第一题(对象 点)

    刷油漆 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho有着一棵灰常好玩的树玩具!这棵树玩具是由N个小球和N-1根木棍拼凑而成,这N个小球都被小Ho标上了 ...

  5. hdu1520树形dp第一题

    判断最大的欢喜值,如果上司来了,直系下属就不来 如果子节点j不来那么dp[i][1]+=dp[j][0];如果子节点j来那么dp[i][0]+=max(dp[j][0],dp[j][1]);//因为j ...

  6. HDU3555 Bomb 数位DP第一题

    The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the ti ...

  7. POJ 1155 TELE 背包型树形DP 经典题

    由电视台,中转站,和用户的电视组成的体系刚好是一棵树 n个节点,编号分别为1~n,1是电视台中心,2~n-m是中转站,n-m+1~n是用户,1为root 现在节点1准备转播一场比赛,已知从一个节点传送 ...

  8. ZOJ 3471 压缩状态DP

    这个问题要看状态怎么想,第一种直接的想法是1代表未合并,状态就从1111111 转移到 带有1个0,然后带有两个0, 但是这样子编程非常不直观.换一种思路,0代表未合并,但是我可以先合并前几个,就是说 ...

  9. POJ 3254 压缩状态DP

    题意:一个矩形网格,可以填0或1, 但有些位置什么数都不能填,要求相邻两个不同时为1,有多少种填法.矩形大小最大 12*12. 压缩状态DP大多有一个可行的state的范围,先求出这个state范围, ...

随机推荐

  1. A - Combination Lock

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Scroog ...

  2. PHP实现人脸识别技术

    这次人脸识别技术,是实现在微信端的,也就是说利用公众微信平台,调用第三的API来实现人脸识别这项技术的. 实现的思路: 首先呢,将收集的照片,建立一个照片库,然后利用在微信平台发送的照片,去到照片库进 ...

  3. fitnesse(gradle构建)安装步骤

    1.安装jdk.ant.gradle(参考http://www.cnblogs.com/274914765qq/p/4401525.html) 2.下载Fitnesse https://github. ...

  4. 模板 - 数据结构 - ST表 + 二维ST表

    区间最大值,$O(nlogn)$ 预处理,$O(1)$ 查询,不能动态修改.在查询次数M显著大于元素数量N的时候看得出差距. 令 $f[i][j]$ 表示 $[i,i+2^j-1]$ 的最大值. 显然 ...

  5. Lightoj1002 【搜索】

    题意: 两两之间的点的花费就是:从A点到B的一条路上某段的最大权值:给一个起点,求到各起点的最小花费. 思路: 一开始的思路: n不是才500,我先建个图,然后DFS一下,不对,是2500: 如果直接 ...

  6. hoj2188 WordStack

    WordStack My Tags   (Edit)   Source : Mid-Atlantic 2005   Time limit : 5 sec   Memory limit : 32 M S ...

  7. ADO学途 five day 连接数据库

    用一个程序的目的就是为了方便对数据进行操作,没有数据的支持,程 序就成了一个空壳子.一般我们常用的数据库有三种mysql, SQL server, Oracle. C#中常用的就是SQL server ...

  8. 48个国际音标-/iː/

    /iː/ 是单元音前元音,是个长元音. ***ee,ea,ie,ei*** 1)张开你的嘴巴,好像你在微笑,露出你的牙齿,嘴唇向两边伸开,成扁平形. 2)将舌前部向硬腭尽量抬起.舌头轻微接触下齿背部. ...

  9. Gitlab备份,Crontab定时备份

    1:Gitlab备份非常简单,只需要一条命令就可以创建完整的备份 gitlab-rake gitlab:backup:create 使用以上命令,就相当于在/var/opt/gitlab/backup ...

  10. [題解/狀壓dp]POJ_2411_Mondriaan's dream

    关于“我读过很多书,到后来大部分都被我忘记了,那阅读的意义是什么?”的疑问,我看过最巧妙的一个回答:当我还是个孩子的时候,我吃过很多的食物,大部分已经一去不复返而且被我忘记了,但可以肯定的是,它们中的 ...