Partychen invents a new game named “Eleven Puzzle” .Just like the classic game “Eight Puzzle”,but there some difference between them:The shape of the board is different and there are two empty tiles. 

The tile in black means it’s empty

Each step you can move only one tile. 
Here comes the problem.How many steps at least it required to done the game. 

 

Input

The first line of input contains one integer specifying the number of test cases to follow. 
Every case contains five lines to describe the initial status of the board. 0 means empty.

It’s confirmed that the board is legal.

 

Output

Output one line for each testcase.Contain an integer denotes the minimum step(s) it need to complete the game.Or “No solution!” if it’s impossible to complete the game within 20 steps.
 

Sample Input

3
2
1 0 3
4 5 6 7 8
9 0 11
10
0
1 2 3
4 5 6 7 8
9 10 11
0
0
11 10 9
8 7 6 5 4
3 2 1
0
 

Sample Output

2
0
No solution!
 
题意:给出上图的11数码,有2个空格,每次可以移一步,问能否在20步以内达到目标状态,如果能输出步数,否则输出No solution!
 
解析:题目给了20步的限制,而且前后搜效果是一样的,所以很容易想到双向bfs,但是状态怎么保存呢,有11个数外加2个空格,可以考虑用康拓展开,但不能用数组保存值,因为值太大了,所以可以考虑用map,我用的方法是哈希,给每个数乘上一个权值,然后加起来哈希成一个值,用数组模拟链表保存状态,但可能出现两种不同的状态哈希成同一个值,所以在相同的情况下还要整个进行对比。
 
源代码
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<string>
  4. #include<iostream>
  5. #include<sstream>
  6. #include<algorithm>
  7. #include<utility>
  8. #include<vector>
  9. #include<set>
  10. #include<map>
  11. #include<queue>
  12. #include<cmath>
  13. #include<iterator>
  14. #include<stack>
  15. using namespace std;
  16. const int INF=1e9+;
  17. const double eps=1e-;
  18. const int mod=;
  19. const int maxn=;
  20. int f[],r[]; //双向bfs的两个队列的队首队尾指针
  21. int dx[]={-,,,},dy[]={,-,,}; //方向数组
  22. bool in(int x,int y){ return x>=&&x<&&y>=&&y<; } //是否越界
  23. struct node
  24. {
  25. int px[],py[]; //保存2个空格的位置和整个数组
  26. int A[][];
  27. }nod[][maxn]; //0是前面搜,1是后面搜
  28. int B[][]={ //最终状态
  29. -,-,,-,-,
  30. -,,,,-,
  31. ,,,,,
  32. -,,,,-,
  33. -,-,,-,-
  34. };
  35. bool Same(int A[][]) //判断该状态是否与最终的状态相等
  36. {
  37. for(int i=;i<;i++)
  38. for(int j=;j<;j++)
  39. if(A[i][j]!=B[i][j]) return false;
  40. return true;
  41. }
  42. int F[]; //保存2^i
  43. void GetF()
  44. {
  45. F[]=;
  46. for(int i=;i<;i++) F[i]=F[i-]*;
  47. }
  48. int Get(int A[][]) //得到哈希值
  49. {
  50. int ret=,k=;
  51. for(int i=;i<;i++)
  52. for(int j=;j<;j++) if(A[i][j]>) ret+=F[k++]*A[i][j]; //乘上一个权值
  53. return ret;
  54. }
  55. struct Hash
  56. {
  57. int v,next,nid,k; //是哈希值,next指向下一个节点,nid和k分别保存是下标和0或1
  58. }ha[mod+maxn];
  59. int hash_id;
  60. bool check(int a,int k1,int b,int k2) //判断是否完全相等
  61. {
  62. for(int i=;i<;i++)
  63. for(int j=;j<;j++)
  64. if(nod[k1][a].A[i][j]!=nod[k2][b].A[i][j]) return false;
  65. return true;
  66. }
  67. int Insert_Hash(int v,int nid,int k) //插入
  68. {
  69. int a=v%mod;
  70. int p=ha[a].next;
  71. while(p!=-)
  72. {
  73. if(ha[p].v==v&&check(ha[p].nid,ha[p].k,nid,k)) return ha[p].k;//有相同的状态
  74. p=ha[p].next;
  75. }
  76. p=++hash_id; //没有则增加新节点,前插法
  77. ha[p].v=v; ha[p].nid=nid; ha[p].k=k;
  78. ha[p].next=ha[a].next; ha[a].next=p;
  79. return -; //-1代表插入了新节点
  80. }
  81. bool AddNode(node& t,int i,int j,int k)
  82. {
  83. int x=t.px[i],y=t.py[i];
  84. int nx=x+dx[j],ny=y+dy[j];
  85. if(!in(nx,ny)||t.A[nx][ny]<=) return false;
  86. node& tt=nod[k][r[k]];
  87. tt=t;
  88. swap(tt.A[x][y],tt.A[nx][ny]); //交换
  89. tt.px[i]=nx; tt.py[i]=ny;
  90. int a=Insert_Hash(Get(tt.A),r[k],k);
  91. if(a==-){ r[k]++; return false; } //队尾指加1
  92. else if(a==k) return false; //自己原来访问过的状态
  93. else return true; //相遇了,找到了解
  94. }
  95. void Print(node& t)
  96. {
  97. for(int i=;i<;i++)
  98. {
  99. for(int j=;j<;j++) printf("%d ",t.A[i][j]);
  100. puts("");
  101. }
  102. puts("=========");
  103. getchar();
  104. }
  105. bool bfs(int k)
  106. {
  107. int& be=f[k];
  108. int en=r[k];
  109. while(be<en)
  110. {
  111. node& t=nod[k][be++];
  112. //Print(t);
  113. for(int i=;i<;i++)
  114. for(int j=;j<;j++) if(AddNode(t,i,j,k)) return true;
  115. }
  116. return false;
  117. }
  118. int solve()
  119. {
  120. if(Same(nod[][].A)) return ;
  121. for(int i=;i<;i++)
  122. for(int j=;j<;j++) nod[][].A[i][j]=B[i][j]; //最终状态
  123. nod[][].px[]=; nod[][].py[]=;
  124. nod[][].px[]=; nod[][].py[]=;
  125. int step=;
  126. f[]=f[]=,r[]=r[]=;
  127. for(int i=;i<mod;i++) ha[i].next=-;
  128. hash_id=mod-;
  129. while(f[]<r[]||f[]<r[]) //双向搜
  130. {
  131. step++;
  132. if(bfs()) return step;
  133. step++;
  134. if(bfs()) return step;
  135. if(step>=) return -;
  136. }
  137. return -;
  138. }
  139. int main()
  140. {
  141. int T;
  142. GetF();
  143. scanf("%d",&T);
  144. while(T--)
  145. {
  146. int k=;
  147. for(int i=;i<;i++)
  148. for(int j=;j<;j++)//得到初始状态
  149. {
  150. if(B[i][j]==-) { nod[][].A[i][j]=-; continue; }
  151. scanf("%d",&nod[][].A[i][j]);
  152. if(nod[][].A[i][j]==)
  153. { nod[][].px[k]=i; nod[][].py[k++]=j; }
  154. }
  155. int ans=solve();
  156. if(ans==-) printf("No solution!\n");
  157. else printf("%d\n",ans);
  158. }
  159. return ;
  160. }

hdu3095-Eleven puzzle(双向搜索+哈希)的更多相关文章

  1. Eleven puzzle_hdu_3095(双向广搜).java

    Eleven puzzle Time Limit: 20000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. Eight(bfs+全排列的哈希函数)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22207   Accepted: 9846   Special Judge ...

  3. poj1200Crazy Search (哈希)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Crazy Search Time Limit: 1000MS   Memory ...

  4. POJ 1200:Crazy Search(哈希)

    Crazy Search Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32483   Accepted: 8947 Des ...

  5. POJ-3131-Cubic Eight-Puzzle(双向BFS+哈希)

    Description Let's play a puzzle using eight cubes placed on a 3 × 3 board leaving one empty square. ...

  6. Crazy Search POJ - 1200 (字符串哈希hash)

    Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could ...

  7. [PHP内核探索]PHP中的哈希表

    在PHP内核中,其中一个很重要的数据结构就是HashTable.我们常用的数组,在内核中就是用HashTable来实现.那么,PHP的HashTable是怎么实现的呢?最近在看HashTable的数据 ...

  8. java单向加密算法小结(2)--MD5哈希算法

    上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...

  9. Java 哈希表运用-LeetCode 1 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

随机推荐

  1. hdu4641-K-string(后缀自动机)

    Problem Description Given a string S. K-string is the sub-string of S and it appear in the S at leas ...

  2. 使用kthread内核线程的内核模块

    这里使用了msleep(50); printk 开启其实挺大的,当我使用msleep(10);机器直接卡死了: 另外ISERR不能判断结构体的,只能判断 空指针 #cat hello.c #inclu ...

  3. Spring的工作原理核心组件和应用

    Spring框架 Spring 是管理多个java类的容器框架,注意是类不管理接口. Spring 的主要功能 Ioc 反转控制和 DI 依赖注入. 注入的方式可以是构造函数赋值也可以是 set方法赋 ...

  4. Head First Html 与 Css 截图

    1.认识HTML web语言 2.深入了解超文本 HTML中的"HT" 3. 构建模块 web页面建设 4. 连接起来 5. 认识媒体,为页面添加图像 6. 严肃的HTML标准及其 ...

  5. 字典树-百度之星-Xor Sum

    Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...

  6. Laravel 4 Blade模板引擎

    http://my.oschina.net/5say/blog/201290 模板输出 基本输出 1 <!-- app/views/example.blade.php --> 2 < ...

  7. web工程调用hadoop集群1.2

    本实例代码在lz的资源中有上传,有需要的可以参考(下载后的文件解压后有两个,一个直接导入myeclipse工程,另外的jar放在hadoop的lib下面,只需修改Utils中的ip即可运行该程序): ...

  8. LoadRuner性能测试之内存分析方法及步骤(Windows)

    1.首先观察Available  Mbytes(可用内存),至少要>=1/2的内存空间 2.然后观察Pages/sec值是不是很大 3.再观察Page  Faules/sec是不是很大,其值表示 ...

  9. java web实现读取指定盘符下的图像(二)

    之前写了一篇文章是关于如何读取指定盘符下的图片,虽然功能可以实现,但是使用的是I/O流的方式,效率不高.现在发现还有一个更好的办法,使用也更加的方便. 我们知道,当我们的图片是放在tomcat下web ...

  10. vim搜索后跳到下(上)一个

    搜索高亮后, 跳到下一个:小写n 上一个:大写N