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> ...
随机推荐
- 如何在标题栏的title前添加网站logo
之前在哪个网上看到的,今天没事给写下来了: 1.将像素大小为16*16的图片命名为 favicon.ico,(图片使用的颜色好像不能超过16色),放到网站根目录下就可以了; 2.在head标签中添加代 ...
- Float 的那些事
css float 定义元素浮动到左侧或者右侧.其出现的本意是让文字环绕图片而已. left.right.inherit(从父级元素获取float值).none 一.浮动的性质 1. 包裹性 disp ...
- Kafka connect快速构建数据ETL通道
摘要: 作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 业余时间调研了一下Kafka connect的配置和使用,记录一些自己的理解和心得,欢迎 ...
- oracle数据库连接串格式
oracle常用链接串格式:jdbc:oracle:thin:@//<host>:<port>/<service_name> jdbc:oracle:thin:@& ...
- CoreJavaE10V1P3.10 第3章 Java的基本编程结构-3.10 数组(Arrays)
数组是存储同一类型数据的数据结构 数组的声明与初始化 int[] a; int a[]; int[] a = new int[100]; int[] a = new int[100]; for (in ...
- jQuery.cssHooks
概述 直接向 jQuery 中添加钩子,用于覆盖设置或获取特定 CSS 属性时的方法,目的是为了标准化 CSS 属性名或创建自定义属性. $.cssHooks 对象提供了一种通过定义函数来获取或设置特 ...
- 浙大玉泉ubuntu L2TP VPN连接设置
网络连接设置 1.内网有线 如果是笔记本且只用无线,剩下的就不需要看了.实验室台式机没有无线网卡不得不折腾-- 玉泉有线都是要绑定固定ip的,实验室无需和mac地址绑定,命令如下sudo gedit ...
- linux下正确安装jsoncpp
要安装jsoncpp,首先要下载好scons,再去安装jsoncpp scons下载地址:wget http://prdownloads.sourceforge.NET/scons/scons-2.2 ...
- 初识git--基础命令
重要:远程分支是一些无法移动的本地分支,本地分支,本地分支,三遍!是对远程库中分支的索引,只有在git进行网络交互时才会更新,用 (远程仓库名)/(分支名) 这样的形式表示远程分支 一.基础命令1 1 ...
- hdu 5833 Zhu and 772002 异或方程组高斯消元
ccpc网赛卡住的一道题 蓝书上的原题 但是当时没看过蓝书 今天又找出来看看 其实也不是特别懂 但比以前是了解了一点了 主要还是要想到构造异或方程组 异或方程组的消元只需要xor就好搞了 数学真的是硬 ...