POJ 3254 Corn Fields状态压缩DP
下面有别人的题解报告,并且不止这一个状态压缩题的哦····
http://blog.csdn.net/accry/article/details/6607703
下面是我的代码,代码很挫,绝对有很大的优化的空间····
#include <cstdio>
#include <cstring>
int dp[][( << ) + ];
//检查一行的奶牛放置情形是否合理
//即是否有相邻的两个1,没有返回true
bool checkHori(int state)
{
if(!(state & (state << )) ) return true;
else return false;
} //检查竖直方向的奶牛放置情形是否合理
//即是否竖直方向有相同的1
bool checkVert(int a,int b)
{
if(!(a&b)) return true;
else return false;
} //看该位置是否是肥沃的土地
//关系式自己推出来
bool checkPos(int map,int state)
{
if( (map| state) == map) return true;
else return false ;
} //该算法时间复杂度很高,为O(m*2^(2*n))
//最坏情况下为O(12*2^24),10^8左右,一定是可以改进的
int main()
{
freopen("in.cpp","r",stdin);
int m,n;
scanf("%d%d",&m,&n);
memset(dp,,sizeof(dp));
dp[][] = ;
int x=;
for(int i=; i<=m; ++i)
{
int t,tmp=,map =; //map用来记录第i行土地的土壤状态
for(int d=; d<n ; ++d)
{
scanf("%d",&t);
map += t*tmp;
tmp *=;
}
x =-x;
for(int j=; j< ( << n); ++j)
dp[x][j] = ; //滚动数组,用时间换空间
for(int j=; j< ( << n); ++j)
{
for(int k=; k < ( << n); ++k)
{
if( checkHori(k) && checkPos(map,k) && (i == || checkVert(k,j) ) )
dp[x][k] += dp[-x][j];
}
}
}
int ans=;
for(int i=; i< ( << n); ++i)
{
ans += (dp[x][i] % );
ans %= ;
}
printf("%d\n",ans);
return ;
}
通过计算得出,当n = 12时,2^12 = 4096,而一行能成功放置的方法数仅为377,
由此可先预处理,离散化存储,极大地降低复杂度。新的算法的复杂度最坏情况下为12*377*377,大概为10^6的复杂度。
#include <cstdio>
#include <cstring>
int dp[][];
int st[];
//检查一行的奶牛放置情形是否合理
//即是否有相邻的两个1,没有返回true
bool checkHori(int state)
{
if(!(state & (state << )) ) return true;
else return false;
} //检查竖直方向的奶牛放置情形是否合理
//即是否竖直方向有相同的1
bool checkVert(int a,int b)
{
if(!(a&b)) return true;
else return false;
} //看该位置是否是肥沃的土地
//关系式自己推出来
bool checkPos(int map,int state)
{
if( (map| state) == map) return true;
else return false ;
} int init(int n)
{
int num=;
for( int i=; i<(<<n); ++i )
{
if(checkHori(i))
{
st[num++] = i;
}
}
return num;
}
int main()
{
// freopen("in.cpp","r",stdin);
int m,n;
scanf("%d%d",&m,&n);
int num=init(n);
memset(dp,,sizeof(dp));
dp[][] = ;
int x=;
for(int i=; i<=m; ++i)
{
int t,tmp=,map =; //map用来记录第i行土地的土壤状态
for(int d=; d<n ; ++d)
{
scanf("%d",&t);
map += t*tmp;
tmp *=;
}
x =-x;
for(int j=; j< num; ++j)
dp[x][j] = ; //滚动数组,用时间换空间
for(int j=; j< num; ++j)
{
for(int k=; k < num; ++k)
{
if(checkPos(map,st[k]) && (i == || checkVert(st[k],st[j]) ) )
dp[x][k] += dp[-x][j];
}
}
}
int ans=;
for(int i=; i< num; ++i)
{
ans += (dp[x][i] % );
ans %= ;
}
printf("%d\n",ans);
return ;
}
POJ 3254 Corn Fields状态压缩DP的更多相关文章
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- POJ 3254 Corn Fields (状态压缩DP)
题意:在由方格组成的矩形里面种草,相邻方格不能都种草,有障碍的地方不能种草,问有多少种种草方案(不种也算一种方案). 分析:方格边长范围只有12,用状态压缩dp好解决. 预处理:每一行的障碍用一个状态 ...
- POJ 3254. Corn Fields 状态压缩DP (入门级)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9806 Accepted: 5185 Descr ...
- POJ 3254 Corn Fields 状态压缩DP (C++/Java)
id=3254">http://poj.org/problem? id=3254 题目大意: 一个农民有n行m列的地方,每一个格子用1代表能够种草地,而0不能够.放牛仅仅能在有草地的. ...
- poj - 3254 Corn Fields (状态压缩dp入门)
http://poj.org/problem?id=3254 参考:http://blog.csdn.net/accry/article/details/6607703 农夫想在m*n的土地上种玉米, ...
- [ACM] POJ 3254 Corn Fields(状态压缩)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8062 Accepted: 4295 Descr ...
- poj 3254 Corn Fields 国家压缩dp
意甲冠军: 要在m行n陆行,有一些格您可以种树,别人做不到的.不相邻的树,我问了一些不同的共同拥有的法律. 分析: 从后往前种,子问题向父问题扩展,当种到某一格时仅仅有他和他后面的n-1个格子的情况对 ...
- POJ 3254 Corn Fields 状态压缩
这题对我真的非常难.实在做不出来,就去百度了,搜到了一种状压DP的方法.这是第一种 详细见凝视 #include <cstdio> #include <cstring> #in ...
- poj 3254 Corn Fields_状态压缩dp
感谢:http://www.cnblogs.com/ka200812/archive/2011/08/11/2135607.html 让我搞懂了. #include <iostream> ...
随机推荐
- Codeforces 821C - Okabe and Boxes
821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...
- Python版本微信跳一跳,软件配置
一.安装python3的环境: 直接从python官方网站下载python3的安装包,直接安装. 记得将python3放到PATH环境变量中,安装的过程中在该配置地方打钩就可以了. 如果安装的过程中出 ...
- initctl 创建自己的JOB
我们的项目需要一个启动一个外部的Jetty server.发现每次kill了这个jetty的进程后,系统会自动启动一个jetty.追查下去发现,原来是在/etc/init.d/jetty 脚本的sta ...
- 3-22 Ruby 编码规则(个人整理)
编码规则 https://github.com/thoughtbot/guides/tree/master/style/ruby *Use a trailing comma after each it ...
- Jersey 2.x 基于 Servlet 的服务器端应用
下面的依赖通常应用到应用服务器上(servlet 容器),同时这个应用服务器上没有整合任何 JAX-RS 的实现. 因此,这个应用服务器需要包含有 JAX-RS API 和 Jersey 实现,同时部 ...
- Visio 入门教程
最近做一个新项目,目前在需求确立阶段,所以每天任务是写文档讨论再修改.由于是云端架构设计,避免不了图形图表配合文字说明,需要制作 E-R 图.网络图.时序图.UML 类图等,对比其他可视化图表工具,V ...
- python-day53--前端js
一.基本语法(ECMA) 单行注释 // /* 多行注释 */ 变量赋值 默认以换行符作为结束符,有分好以分号作为结束符号 JS的引入方式: 1. <script> </script ...
- 30. Substring with Concatenation of All Words *HARD*
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- spring-mvc----数据库数据到页面错误--tomcat启动不了
spring-mvc----数据库数据到页面错误 错误为: 解决: 开启管理员cmd.到tomcat的目录下,-->shutdown.bat 不用重启,不用关机.
- 阿里云ECS安装最新版本Node.js
原文 http://www.w3ctech.com/topic/1610 主题 Node.js操作系统服务器 我的ECS实例是Ubuntu操作系统,直接使用 apt-get install node ...