Paint on a Wall
Paint on a Wall
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4012
搜索+状态压缩
这题刚开始以为是dp(之前写过墙是一行的题,然而是在艾神讲完之后才调出来的= =),但是想不出来怎么搞,看到n<=8数据量这么小,应该搜索可以解,然后想都没想就上去写了IDA*,结果有段代码怎么都找不到bug,一直WA。
结束后,用bfs写了遍,每次转移数最多是(2n)^2,最差的情况是2n层,所以复杂度是(2n)^(4n),然而状态去重后,状态数最多为2^(2n)种,复杂度为[(2n)^2]*[2^(2n)],由于n很小,所以不会超时。然而如果用之前的IDA*,差不多会把深度小于deep的所有解空间遍历,一定会超时。
debug用了好几个小时,最后发现是
(lower&bit[other*n+i])>
写成了
(lower&bit[other*n+i])==
...不想说什么了...
代码如下:
#include<cstdio>
#include<cstring>
#include<queue>
#define met(a,b) memset(a,b,sizeof(a))
#define mkp(state,deep) make_pair(state,deep)
#define X first
#define Y second
#define N 8
using namespace std;
typedef pair<int,int> P;
int T,n,ans,before,after;
char mp[][N+];
bool state[<<];
int bit[];
void init(){
for(int i=;i<=;++i)
bit[i]=(<<i);
}
void bfs(){
int finish=(<<(*n))-;
met(state,);
queue<P>q;
q.push(mkp(,));
state[]=;
while(!q.empty()){
P s=q.front();q.pop();
if(s.X==finish){
ans=s.Y;
return;
}
for(int c=;c<n;++c)
for(int r=;r<;++r)
if((s.X&bit[r*n+c])==){
char color=mp[r][c];
int other=(int)(!r);
int upper=s.X;
int lower=s.X;
int deep=s.Y;
for(int i=c;i<n;++i){
if((upper&bit[r*n+i])>&&mp[r][i]!=color)
upper^=bit[r*n+i];
else if((upper&bit[r*n+i])==&&mp[r][i]==color)
upper|=bit[r*n+i];
if((lower&bit[other*n+i])>&&mp[other][i]!=color)
lower^=bit[other*n+i];
else if((lower&bit[other*n+i])==&&mp[other][i]==color)
lower|=bit[other*n+i];
if(!state[upper]){
state[upper]=;
q.push(mkp(upper,deep+));
}
int temp;
if(r==)temp=(before&upper)|(after&lower);
else if(r==)temp=(after&upper)|(before&lower);
if(!state[temp]){
state[temp]=;
q.push(mkp(temp,deep+));
}
}
}
}
}
int main(void){
init();
scanf("%d",&T);
for(int t=;t<=T;++t){
scanf("%d\n",&n);
before=after=;
for(int i=;i<*n;++i){
if(i<n)before|=(<<i);
else after|=(<<i);
}
for(int i=;i<;++i)
scanf("%s",mp[i]);
bfs();
printf("Case #%d: ",t);
printf("%d\n",ans);
}
}
Paint on a Wall的更多相关文章
- HDU 4012 Paint on a Wall(状压+bfs)
Paint on a Wall Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- LED Decorative Light Manufacturer - Led Wall Lamp Performance Characteristics
LED Decorative Light Manufacturer introduction: LED wall lamp is a light-emitting diode as a ligh ...
- Daily record-October
October 11. Hope is a good thing, maybe the best of things, and no good thing ever dies. 希望是美好的,也许是人 ...
- 2018 codejam kickstart H轮
被第一题傻逼题卡了很久……好的我也是个傻逼 倒在了最后一题 本来以为小数据过了就能过大数据 结果下载了大数据 发现怎么输出了好多个零 调代码过程中超时了 结束后重新提交了一下 果然是不通过的 A 题目 ...
- HDU 4391 Paint The Wall(分块+延迟标记)
Paint The Wall Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543
学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...
- 【HDU4391】【块状链表】Paint The Wall
Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...
- hdu 1543 Paint the Wall
http://acm.hdu.edu.cn/showproblem.php?pid=1543 #include <cstdio> #include <cstring> #inc ...
- Paint the Wall ZOJ - 2747
点数很多,坐标值很大,然后离散化一下用一个点表示一小块的面积对应的颜色,然后更新的时候一块一块更新,查询的时候一块一块查询 #include<map> #include<set> ...
随机推荐
- Python学习--10 面向对象编程
面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 本节对于面向对象的概念不做 ...
- PHP5.6通过CURL上传图片@符无效的兼容问题
今天本来想试试一个图片云的API,于是本地做了个上传图片的测试,结果灰常郁闷的发现以前一直用的好好的CURL上传图片居然死活不起作用,本来几分钟搞定的事情,结果折腾了大半天才终于找到原因,居然是兼容性 ...
- php 内置函数 ( 随手能写出100个才算高级工程师 )
string 类型 去掉左右空格, trim()字符串的长度 strlen() float类型数组类型 count() 数组个数 资源类型 fopen() 大写转小写 strtolower()小写转大 ...
- nyoj 79 拦截导弹 (动态规划)
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=79 题意即求最长单调递减子序列 #include<iostream> #inc ...
- 微软sqlHelper
//微软的SQLHelper类(含完整中文注释) using System; using System.Data; using System.Xml; using System.Data.SqlCli ...
- Kafka发送消息失败原因
Kafka发送消息方法如下: Properties properties = new Properties(); properties.put("zookeeper.connect" ...
- html和html5学习
html和html5学习 chorme.safari中的input或textarea html超链接(a)详细讲解 html5新增及删除标签 html表格 图片加alt属性 input的type属性 ...
- Redis Cluster 实践
一:关于redis cluster 1:redis cluster的现状 reids-cluster计划在redis3.0中推出,可以看作者antirez的声明:http://antirez.com/ ...
- UNIX基础--Shells
Shells Shell提供了一个和操作系统交互的命令行接口.shell的主要功能就是从输入取得命令然后去执行.FreeBSD内含了一些shell,包括:Bourne shell(sh). exten ...
- C#中的DataSet添加DataTable问题
最近在使用DataTable来给前台控件绑定数据,开始时查了网上的一些给DataSet添加DataTable时需要注意的地方,一般都要添加表名并且使用DataTable.Copy()方法,否则会报错, ...