因为是计算还原成一种局面的最短步骤,应该想到从最终局面开始做bfs,把所有能到达的情况遍历一遍,把值存下来。

bfs过程中,访问过的局面的记录是此题的关键,9*9的方格在计算过程中直接存储非常占内存。而这个显然是12345678x的不同排列,考虑康拓展开来记录,每个局面hash成一个整数。步骤我先算了一下,最多有31步,我用4进制位hash成了两个整数来保存

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <set>
  4. #include <cmath>
  5. #include <string>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <vector>
  9. #include <map>
  10. #define LL long long
  11. using namespace std;
  12. class cantor
  13. {
  14. public:
  15. #define siz 9
  16. char c[siz]={'','','','','','','','','x'};
  17. LL w[siz];
  18. bool vis[siz];
  19. cantor()
  20. {
  21. w[]=;
  22. for(int i=;i<siz;i++)
  23. w[i]=w[i-]*i;
  24. }
  25. void init()
  26. {
  27. for(int i=;i<siz;i++)
  28. vis[i]=false;
  29. }
  30. LL makeCanto(string s)
  31. {
  32. init();
  33. LL rec=;
  34. for(int i=;i<siz;i++)
  35. {
  36. int d=;
  37. for(int j=;j<siz;j++)
  38. {
  39. if(vis[j])
  40. continue;
  41. if(c[j]!=s[i])d++;
  42. else
  43. {
  44. vis[j]=true;
  45. break;
  46. }
  47. }
  48. rec+=w[siz-i-]*d;
  49. }
  50. return rec;
  51. }
  52. string recover(LL val)
  53. {
  54. init();
  55. string s="";
  56. for(int i=siz-;i>=;i--)
  57. {
  58. LL te=val/w[i];
  59. val-=e*w[i];
  60. for(int j=,cnt=-;j<siz;j++)
  61. {
  62. if(vis[j])continue;
  63. else cnt++;
  64. if(cnt==te&&!vis[j])
  65. {
  66. s+=c[j];
  67. vis[j]=true;
  68. break;
  69. }
  70. }
  71. }
  72. return s;
  73. }
  74. };
  75. LL n,m;
  76. char mp[][];
  77. set<LL> s;
  78. string ss="";
  79. const string las="12345678x";
  80. bool f;
  81. int dir[][]={-,,,,,,,-};
  82. char dfuck[]={'d','u','l','r'};
  83. struct node
  84. {
  85. LL s;
  86. int c;
  87. int x,y;
  88. LL a,b;
  89. node(LL ss,int aa,int X,int Y,LL A,LL B){s=ss;c=aa;x=X;y=Y;a=A;b=B;}
  90. };
  91. struct ax
  92. {
  93. LL a,b,c;
  94. ax(LL A,LL B,LL C){a=A;b=B;c=C;}
  95. ax(){}
  96. };
  97. map<LL,ax> ans;
  98. LL bit[];
  99. int main()
  100. {
  101. cin.sync_with_stdio(false);
  102. cantor fx;
  103. bit[]=;
  104. for(int i=;i<;i++)
  105. bit[i]=bit[i-]*;
  106. queue<node> q;
  107. node ini=node(fx.makeCanto(las),,,,,);
  108. q.push(ini);
  109. s.clear();
  110. ans.clear();
  111. int mx=;
  112. s.insert(fx.makeCanto(las));
  113. while(!q.empty())
  114. {
  115. node now=q.front();
  116. ans[now.s]=ax(now.a,now.b,now.c);
  117. q.pop();
  118. mx=max(now.c,mx);
  119. for(int i=;i<;i++)
  120. {
  121. int yy=now.y+dir[i][];
  122. int xx=now.x+dir[i][];
  123. if(xx<||yy<||xx>=||yy>=)continue;
  124. string nx=fx.recover(now.s);
  125. swap(nx[now.y*+now.x],nx[yy*+xx]);
  126. LL nxv=fx.makeCanto(nx);
  127. if(s.find(nxv)!=s.end())continue;
  128. s.insert(nxv);
  129. LL na=now.a,nb=now.b;
  130. if(now.c<)
  131. {
  132. na+=i*bit[now.c];
  133. }
  134. else
  135. {
  136. nb+=i*bit[now.c-];
  137. }
  138. q.push(node(nxv,now.c+,xx,yy,na,nb));
  139. }
  140. }
  141. while(cin>>mp[][]>>mp[][]>>mp[][]>>mp[][]>>mp[][]>>mp[][]>>mp[][]>>mp[][]>>mp[][])
  142. {
  143. int x,y;
  144. f=false;
  145. s.clear();
  146. ss="";
  147. for(int i=;i<;i++)
  148. for(int j=;j<;j++)
  149. {
  150. ss+=mp[i][j];
  151. if(mp[i][j]=='x')
  152. y=i,x=j;
  153. }
  154. string o="";
  155. if(ss=="12345678x")
  156. {
  157. cout<<"lr"<<endl;
  158. continue;
  159. }
  160. if(ans.find(fx.makeCanto(ss))==ans.end())
  161. {
  162. cout<<"unsolvable"<<endl;
  163. continue;
  164. }
  165. ax fuck=ans[fx.makeCanto(ss)];
  166. for(int i=;i<fuck.c;i++)
  167. {
  168. if(i<)
  169. {
  170. o+=dfuck[fuck.a%];
  171. fuck.a/=;
  172. }
  173. else
  174. {
  175. o+=dfuck[fuck.b%];
  176. fuck.b/=;
  177. }
  178. }
  179. reverse(o.begin(),o.end());
  180. cout<<o<<endl;
  181. }
  182. }

hdu-1043 bfs+康拓展开hash的更多相关文章

  1. HDU 4531 bfs/康拓展开

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4531 吉哥系列故事——乾坤大挪移 Time Limit: 2000/1000 MS (Java/Othe ...

  2. Eight (HDU - 1043|POJ - 1077)(A* | 双向bfs+康拓展开)

    The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've see ...

  3. hdu 1043 pku poj 1077 Eight (BFS + 康拓展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 http://poj.org/problem?id=1077 Eight Time Limit: 1000 ...

  4. POJ 1077 && HDU 1043 Eight A*算法,bfs,康托展开,hash 难度:3

    http://poj.org/problem?id=1077 http://acm.hdu.edu.cn/showproblem.php?pid=1043 X=a[n]*(n-1)!+a[n-1]*( ...

  5. 【HDOJ3567】【预处理bfs+映射+康拓展开hash】

    http://acm.hdu.edu.cn/showproblem.php?pid=3567 Eight II Time Limit: 4000/2000 MS (Java/Others)    Me ...

  6. bnuoj 1071 拼图++(BFS+康拓展开)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=1071 [题意]:经过四个点的顺逆时针旋转,得到最终拼图 [题解]:康拓展开+BFS,注意先预处理,得 ...

  7. 九宫重拍(bfs + 康拓展开)

    问题描述 如下面第一个图的九宫格中,放着 1~8 的数字卡片,还有一个格子空着.与空格子相邻的格子中的卡片可以移动到空格中.经过若干次移动,可以形成第二个图所示的局面. 我们把第一个图的局面记为:12 ...

  8. 8数码,欺我太甚!<bfs+康拓展开>

    不多述,直接上代码,至于康拓展开,以前的文章里有 #include<iostream> #include<cstdio> #include<queue> using ...

  9. hdu 1430(BFS+康托展开+映射+输出路径)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

随机推荐

  1. Spring Boot事务管理(中)

    在上一篇 Spring Boot事务管理(上)的基础上介绍Spring Boot事务属性和事务回滚规则 . 4 Spring Boot事务属性 什么是事务属性呢?事务属性可以理解成事务的一些基本配置, ...

  2. python练习题-day11

    1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), 要求:登录成功一次,后续的函数都无需再输入用户名和密码 flag=False def wrapper(fun): def inn ...

  3. SpringBoot-@RequestParam

    Request参数 在访问各种各样网站时,经常会发现网站的URL的最后一部分形如:?xxxx=yyyy&zzzz=wwww.这就是HTTP协议中的Request参数,它有什么用呢?先来看一个例 ...

  4. vs添加github代码库

    1.安装git for windows 2.在vs中工具->扩展和更新,安装github extension 3.在项目中右键,添加源码到git,之后配置git,然后选择同步或者commit即可

  5. [js]js设计模式-单例模式

    单例模式 不同模块之间需要同时开发, // 单例模式: 把描述同一个事物的属性和方法放在同一个内存空间下. // 优点: 分组,防止冲突 // p1 p2也叫做命名空间(模块开发) var p1 = ...

  6. shmdt() 与 shmctl() 的区别?

    操作共享内存,我们用到了下面的函数 ============================================== #include <sys/types.h> #inclu ...

  7. 微信小程序 条件渲染 wx:if

    1.在框架中,我们用wx:if="{{condition}}"来判断是否需要渲染该代码块 <view wx:if="{{condition}}"> ...

  8. Go 初体验 - 令人惊叹的语法 - defer.2 - 如何提前执行?

    上一文中讲到 defer 会在宿主函数 return 之前调用,那么我们就是想在宿主函数执行到中间调用,怎么办呢? 1. 改变宿主函数逻辑,分成多个函数,需要的那个函数里 defer . 2. 使用匿 ...

  9. 3D印表機 零件採購資訊

    3D印表機 零件採購資訊 採購資訊僅供參考,零件的品質由店家擔保! 壓克力 螺絲螺帽牙條 高來螺絲 滑套.軸承 五連軸承 掏寶-廣發軸承 光軸 掏寶-廣發軸承 彈簧 雅銅彈簧 鐵氟龍製品 馬達 電源供 ...

  10. mysql 事务锁超时时间 innodb_lock_wait_timeout

    mysql 事务锁超时时间 innodb_lock_wait_timeout: # 查询全局等待事务锁超时时间 SHOW GLOBAL VARIABLES LIKE 'innodb_lock_wait ...