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. C/C++ - 多线程

    前几天简单对C和C++中的创建多线程的函数进行了测试,这篇随笔就简单介绍一下创建线程的相关函数. C中三个创建线程函数:pthread_create()._beginthread().CreateTh ...

  2. eslint检测规则中,括弧和函数名之间去掉空格的配置

    在.eslintrc.js中配置: // add your custom rules here rules: { // no space before function name "spac ...

  3. 阿里云服务器ubantu创建新用户登录显示问题

    在root用户下输入:vi /etc/passwd,找到添加的用户,在后面加上/bin/bash 重新登录即回复正常

  4. 吴裕雄--天生自然PYTHON爬虫:使用BeautifulSoup解析中国旅游网页数据

    import requests from bs4 import BeautifulSoup url = "http://www.cntour.cn/" strhtml = requ ...

  5. Subtitles

    1. 字幕Subtitles 2. 字幕类型 3. 字幕格式 4. 常用文本字幕 5. 字幕编辑器 6. 字幕编辑器比较 1. 字幕Subtitles https://en.wikipedia.org ...

  6. Linux CentOS7 VMware正则介绍、grep工具、egrep表达式

    一.正则介绍 正则是学习shell脚本的必学的内容,正则学的好坏直接影响到shell编程能力. 正则表达式:使用单个字符串来描述或匹配一系列符合某个句法规则的字符串.通常用来检索和替换那些符合某个模式 ...

  7. [经验] Cocos Creator使用笔记 --- 调用不同脚本下的函数

    因为 JavaScript 不同于 Java, 想要调用不同文件的函数的话不能直接 ClassName object = new ClassName(); object.function(param) ...

  8. ch8 faux列

    在一个布局中,假设有导航元素和内容元素,切给他们都分别应用了背景,理想情况下,背景应该拉长到整个布局的最大高度,从而形成列的效果,但是实际上,因为导航元素没有扩展到最大高度,所以它们的背景不会拉长,如 ...

  9. 7专题总结-高频题high frequency

    Outline . Single Number I, II, III . Majority Number I, II, III . Best Time to Buy and Sale Stock I, ...

  10. JavaAgent学习小结

    前言 最近因为公司需要,需要了解下java探针,在网上找资料,发现资料还是有很多的,但是例子太少,有的直接把公司代码粘贴出来,太复杂了,有的又特别简单不是我想要的例子, 我想要这样的一个例子: jvm ...