Corn Fields
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9295   Accepted: 4940

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了,搞一下骨牌覆盖的那种题会瞬间有做这题的思路的。一开始没想到result数组里面的数值可能会超,WA了几次,最后发现结果有负数改过来了就AC了。

代码:

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#pragma warning(disable:4996)
using namespace std; int n,m;
int state[15][15];
long long result[15][(1<<13)-1]; bool pend_ok(int pend,int row[])
{
int i;
for(i=n;i>=1;i--)
{
int wei =pend & 1;
if(row[i]== 0 &&wei==1)//11001
{
return false;
} pend = pend>>1; int wei2=pend&1; if(wei&&wei2)
return false;
}
return true;
} bool pend(int j,int k )
{
int i;
for(i=1;i<=n;i++)
{
int wei1 = j&1;
int wei2 = k&1; if(wei1==1&&wei2==1)
{
return false;
}
j=j>>1;
k=k>>1;
}
return true;
} int main()
{
int i,j,k;
cin>>m>>n; for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
scanf_s("%d",&state[i][j]);
}
} int n1=(1<<n)-1; memset(result,0,sizeof(result)); for(j=0;j<=n1;j++)
{
if(pend_ok(j,state[1]))
{
result[1][j]=1;
}
} for(i=2;i<=m;i++)
{
for(j=0;j<=n1;j++)
{
for(k=0;k<=n1;k++)
{
if(pend_ok(j,state[i])&&pend(j,k))
{
result[i][j] += result[i-1][k];
if(result[i][j]>100000000)//result的值可能会超过,这里要先过滤一下!!!(之前没过滤导致wrong)
result[i][j]=result[i][j]-100000000;
}
}
}
}
long long max=0;
for(j=0;j<=n1;j++)
{
max +=result[m][j];
max=max%100000000;
} cout<<max%100000000<<endl; return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3254:Corn Fields的更多相关文章

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

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

  2. POJ 3254 poj3254 Corn Fields

    题意:给出一个n行m列的草地,1表示肥沃,0表示贫瘠,现在要把一些牛放在肥沃的草地上,但是要求所有牛不能相邻,问你有多少种放法. 思路: DP[i][j]=sum(dp[i-1][k]); i表示当前 ...

  3. POJ3254:Corn Fields(状压dp第一发)

    题目:http://poj.org/problem?id=3254 直接上代码吧,刚开始做时主要的问题就是看不懂二进制,有个博客写的太好了,就直接把题解复制在下面了. #include <ios ...

  4. POJ3254:Corn Fields——题解

    http://poj.org/problem?id=3254 题面来自洛谷:https://www.luogu.org/problemnew/show/1879 农场主John新买了一块长方形的新牧场 ...

  5. Corn Fields POJ - 3254 (状压dp)

    题目链接: Corn Fields  POJ - 3254 题目大意:给你一个n*m的矩阵,矩阵的元素只包括0和1,0代表当前的位置不能放置人,1代表当前的位置可以放人,当你决定放人的时候,这个人的四 ...

  6. POJ 3254. Corn Fields 状态压缩DP (入门级)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9806   Accepted: 5185 Descr ...

  7. poj 3254 Corn Fields

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

  8. 状压DP POJ 3254 Corn Fields

    题目传送门 /* 状态压缩DP:先处理硬性条件即不能种植的,然后处理左右不相邻的, 接着就是相邻两行查询所有可行的种数并累加 写错一个地方差错N久:) 详细解释:http://www.tuicool. ...

  9. POJ 3254 Corn Fields(状压DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13732   Accepted: 7216 Desc ...

随机推荐

  1. Spring_MVC 21问

    1.什么是Spring MVC ?简单介绍下你对springMVC的理解? Spring MVC是一个基于Java的实现了MVC设计模式的请求驱动类型的轻量级Web框架,通过把Model,View,C ...

  2. 5款微信小程序开发工具使用报告,微信官方开发工具还有待提升

    微信小程序已经内测有一段时间了,笔者本着好奇加学习的心态写了几个小demo,虽然在MINA框架上并没有遇到太多的坑,但官方开发工具实在不敢恭维. api提示不全,要一个个查api啊,写代码超级慢啊 很 ...

  3. 中山普及Day17——普及

    今天换教室,本来教室多好嘛,易守难攻,结果...今天今天仅下午就被熊抄了2次,熊超真TMD不是人呐,走路连脚步声都没有. 然后,播报分数: 爆0了!!!

  4. 编写安全 PHP 应用程序的七个习惯

    编写安全 PHP 应用程序的七个习惯   在提及安全性问题时,需要注意,除了实际的平台和操作系统安全性问题之外,您还需要确保编写安全的应用程序.在编写 PHP 应用程序时,请应用下面的七个习惯以确保应 ...

  5. 入门学习C链接

    参考链接:http://c.biancheng.net/view/465.html 在里面链接下载了:code:block,还有C语言入门的PDF文件. 常看网站:https://www.cnblog ...

  6. Android加载手机磁盘上的资源---decodeFile方法的使用

    一般在写Android程序时,通常会将图片资源放在/res/drawable/文件夹下,读取时,通过R.drawable.imageId即可读取图片内容,但用户在使用时,一般会想要读取存放在存储卡上的 ...

  7. oracle练习-day03

    .创建表空间.创建用户赋权限.创建表语法:.常见的数据类型字符             myname ) varchar2:推荐使用这个 可变长度最大字符    myname varchar2() 字 ...

  8. Day2-C-迷宫问题 -POJ3984

    定义一个二维数组: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, ...

  9. IntelliJ如何设置自动导包

    idea 关于自动导包的设置 标签: idea 2016-09-30 18:10 400人阅读 评论(0) 收藏 举报 本文章已收录于: .embody{ padding:10px 10px 10px ...

  10. 前端学习笔记系列一:15vscode汉化、快速复制行、网页背景图有效设置、 dl~dt~dd标签使用

    ctrl+shift+p,调出configure display language,选择en或zh,若没有则选择安装使用其它语言,则直接呼出扩展程序搜索界面,选择,然后安装,重启即可. shift+a ...