标签: 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;
}

最新文章

  1. visual studio for mac的安装初体验
  2. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(四)地图导航控件模块
  3. ASP.NET泛型List的各种用法Skip、Take等
  4. Ace - Responsive Admin Template
  5. Facebook或成云领域黑马 冲击亚马逊
  6. zabbix: failed to accept an incoming connection
  7. PPTP服务器配置选项详解
  8. 《Genesis-3D开源游戏引擎完整实例教程-2D射击游戏篇06:计分》
  9. References &amp; the Copy-Constructor
  10. mysql中enum的用法
  11. IT谁谁说女子不如男行业
  12. HTTPDNS成为移动互联网的标配–原因与原理解析(转)
  13. 求n!末尾0的个数
  14. zookeeper-3.4.5安装&amp;3台机器安装之后 ./zkServer.sh status 之后会显示“Error contacting service. It is probably not running.”的解决办法
  15. js通过a链接控制多个DIV只显示其中一个其它隐藏
  16. SVN与资源库同步后动作的字母意义
  17. oracle无效索引重建
  18. 桌面应用开发之WPF页面导航
  19. [UE4]虚幻4蓝图使用小技巧
  20. 转:如何解决VC &quot;应用程序无法启动,因为应用程序的并行配置不正确 sxstrace.exe&quot;问题

热门文章

  1. 华为codecraft2018总结
  2. Laravel中的路由管理
  3. Installing cmake 2.8.8 or higher on Ubuntu 12.04 (Precise Pangolin) (转载)
  4. 如何将基于对话框的MFC工程改成基于BCG的
  5. Lightoj 1021【状压DP(未搞)】
  6. 如何实现一个无边框Form的移动和改变大小(二)
  7. C# 基础之类与结构体的区别
  8. 用apache commons-pool2建立thrift连接池
  9. [软件工程基础]2017.11.01 第五次 Scrum 会议
  10. G-华华对月月的忠诚