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用了好几个小时,最后发现是

  1. (lower&bit[other*n+i])>

写成了

  1. (lower&bit[other*n+i])==

...不想说什么了...

代码如下:

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<queue>
  4. #define met(a,b) memset(a,b,sizeof(a))
  5. #define mkp(state,deep) make_pair(state,deep)
  6. #define X first
  7. #define Y second
  8. #define N 8
  9. using namespace std;
  10. typedef pair<int,int> P;
  11. int T,n,ans,before,after;
  12. char mp[][N+];
  13. bool state[<<];
  14. int bit[];
  15. void init(){
  16. for(int i=;i<=;++i)
  17. bit[i]=(<<i);
  18. }
  19. void bfs(){
  20. int finish=(<<(*n))-;
  21. met(state,);
  22. queue<P>q;
  23. q.push(mkp(,));
  24. state[]=;
  25. while(!q.empty()){
  26. P s=q.front();q.pop();
  27. if(s.X==finish){
  28. ans=s.Y;
  29. return;
  30. }
  31. for(int c=;c<n;++c)
  32. for(int r=;r<;++r)
  33. if((s.X&bit[r*n+c])==){
  34. char color=mp[r][c];
  35. int other=(int)(!r);
  36. int upper=s.X;
  37. int lower=s.X;
  38. int deep=s.Y;
  39. for(int i=c;i<n;++i){
  40. if((upper&bit[r*n+i])>&&mp[r][i]!=color)
  41. upper^=bit[r*n+i];
  42. else if((upper&bit[r*n+i])==&&mp[r][i]==color)
  43. upper|=bit[r*n+i];
  44. if((lower&bit[other*n+i])>&&mp[other][i]!=color)
  45. lower^=bit[other*n+i];
  46. else if((lower&bit[other*n+i])==&&mp[other][i]==color)
  47. lower|=bit[other*n+i];
  48. if(!state[upper]){
  49. state[upper]=;
  50. q.push(mkp(upper,deep+));
  51. }
  52. int temp;
  53. if(r==)temp=(before&upper)|(after&lower);
  54. else if(r==)temp=(after&upper)|(before&lower);
  55. if(!state[temp]){
  56. state[temp]=;
  57. q.push(mkp(temp,deep+));
  58. }
  59. }
  60. }
  61. }
  62. }
  63. int main(void){
  64. init();
  65. scanf("%d",&T);
  66. for(int t=;t<=T;++t){
  67. scanf("%d\n",&n);
  68. before=after=;
  69. for(int i=;i<*n;++i){
  70. if(i<n)before|=(<<i);
  71. else after|=(<<i);
  72. }
  73. for(int i=;i<;++i)
  74. scanf("%s",mp[i]);
  75. bfs();
  76. printf("Case #%d: ",t);
  77. printf("%d\n",ans);
  78. }
  79. }

Paint on a Wall的更多相关文章

  1. 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) ...

  2. 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 ...

  3. Daily record-October

    October 11. Hope is a good thing, maybe the best of things, and no good thing ever dies. 希望是美好的,也许是人 ...

  4. 2018 codejam kickstart H轮

    被第一题傻逼题卡了很久……好的我也是个傻逼 倒在了最后一题 本来以为小数据过了就能过大数据 结果下载了大数据 发现怎么输出了好多个零 调代码过程中超时了 结束后重新提交了一下 果然是不通过的 A 题目 ...

  5. HDU 4391 Paint The Wall(分块+延迟标记)

    Paint The Wall Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543

    学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...

  7. 【HDU4391】【块状链表】Paint The Wall

    Problem Description As a amateur artist, Xenocide loves painting the wall. The wall can be considere ...

  8. hdu 1543 Paint the Wall

    http://acm.hdu.edu.cn/showproblem.php?pid=1543 #include <cstdio> #include <cstring> #inc ...

  9. Paint the Wall ZOJ - 2747

    点数很多,坐标值很大,然后离散化一下用一个点表示一小块的面积对应的颜色,然后更新的时候一块一块更新,查询的时候一块一块查询 #include<map> #include<set> ...

随机推荐

  1. 如何在标题栏的title前添加网站logo

    之前在哪个网上看到的,今天没事给写下来了: 1.将像素大小为16*16的图片命名为 favicon.ico,(图片使用的颜色好像不能超过16色),放到网站根目录下就可以了; 2.在head标签中添加代 ...

  2. Float 的那些事

    css float 定义元素浮动到左侧或者右侧.其出现的本意是让文字环绕图片而已. left.right.inherit(从父级元素获取float值).none 一.浮动的性质 1. 包裹性 disp ...

  3. Kafka connect快速构建数据ETL通道

    摘要: 作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 业余时间调研了一下Kafka connect的配置和使用,记录一些自己的理解和心得,欢迎 ...

  4. oracle数据库连接串格式

    oracle常用链接串格式:jdbc:oracle:thin:@//<host>:<port>/<service_name> jdbc:oracle:thin:@& ...

  5. CoreJavaE10V1P3.10 第3章 Java的基本编程结构-3.10 数组(Arrays)

    数组是存储同一类型数据的数据结构 数组的声明与初始化 int[] a; int a[]; int[] a = new int[100]; int[] a = new int[100]; for (in ...

  6. jQuery.cssHooks

    概述 直接向 jQuery 中添加钩子,用于覆盖设置或获取特定 CSS 属性时的方法,目的是为了标准化 CSS 属性名或创建自定义属性. $.cssHooks 对象提供了一种通过定义函数来获取或设置特 ...

  7. 浙大玉泉ubuntu L2TP VPN连接设置

    网络连接设置 1.内网有线 如果是笔记本且只用无线,剩下的就不需要看了.实验室台式机没有无线网卡不得不折腾-- 玉泉有线都是要绑定固定ip的,实验室无需和mac地址绑定,命令如下sudo gedit ...

  8. linux下正确安装jsoncpp

    要安装jsoncpp,首先要下载好scons,再去安装jsoncpp scons下载地址:wget http://prdownloads.sourceforge.NET/scons/scons-2.2 ...

  9. 初识git--基础命令

    重要:远程分支是一些无法移动的本地分支,本地分支,本地分支,三遍!是对远程库中分支的索引,只有在git进行网络交互时才会更新,用 (远程仓库名)/(分支名) 这样的形式表示远程分支 一.基础命令1 1 ...

  10. hdu 5833 Zhu and 772002 异或方程组高斯消元

    ccpc网赛卡住的一道题 蓝书上的原题 但是当时没看过蓝书 今天又找出来看看 其实也不是特别懂 但比以前是了解了一点了 主要还是要想到构造异或方程组 异或方程组的消元只需要xor就好搞了 数学真的是硬 ...