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. 安卓手机的屏幕规格很多。app开发者在设计User Interface的时候,要怎么处理,才能适应不同屏幕大小?

    在app store下载应用时经常看到:此App已针对iPhone 5 进行优化.可是Android手机屏幕规格这么多,相差这么远.难道要针对每个尺寸都进行一次优化吗?(题主非专业人士,看到2014年 ...

  2. ABC155F - Perils in Parallel

    简述题意 给你N个数对 表示坐标与状态(0/1), M个操作,给定一个区间,区间内的坐标的状态翻转 思路:看到区间修改,很容易想到差分,对数对sort,每个a_i与a_i-1异或构造差分数组b,每次对 ...

  3. Ansible ssh-key密钥认证配置

    对于被管理服务器做免密码登录设置 1.在管理服务器生成ssh-key密钥 #ssh-keygen  //生成秘钥 root@hsz:/etc/ansible# ssh-keygen Generatin ...

  4. 笔记||Pyhton3进阶之多线程原理

    # 多线程 # 一个进程相当于一个或多个线程 # 当没有多线程编程时,一个进程也是一个主线程 # 但有多线程编程时,一个进程包含多个线程,包括主线程 # 使用线程 可以实现程序的并发 # python ...

  5. 在WAMP环境下搭建ZendDebugger php调试工具的方法

    东西不是新货,所以介绍就不做介绍了,下面主要是配置流程. 首先,下载ZendDebugger,下载链接:http://downloads.zend.com/pdt/server-debugger/,因 ...

  6. nginx反向代理(2)

    目录 nginx缓存 基本概念 常用模块 proxy_cache 超时相关 常见架构 ========================================================= ...

  7. 子组件props接受父组件传递的值 能修改吗?

    vue2.0 子组件props接受父组件传递的值,能不能修改的问题整理 父组件代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...

  8. 学会使用Google hacking

    https://klionsec.github.io/2014/12/14/search-hacking/ 熟练利用Google hacking 来辅助我们快速渗透 http://www.sec-re ...

  9. threading 多线程

    # coding:utf- import time from threading import Thread def foo(x):#这里可以带参数def foo(x) print "foo ...

  10. 「SCOI2009」windy数

    传送门 Luogu 解题思路 数位 \(\text{DP}\) 设状态 \(dp[now][las][0/1][0/1]\) 表示当前 \(\text{DP}\) 到第 \(i\) 位,前一个数是 \ ...