题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879

思路:题目意思很简单,就是通过一些位置的交换,最后变成有序数列,对于一组序列,我们可以用康托展开然后hash判重。

然后就是普通的bfs,稍微留意一下细节即可。

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<queue>
  6. using namespace std;
  7. int is_prime[]={,,,,,,,,,,,,,,,,,};
  8. int fac[]={,,,,,,,,};
  9.  
  10. struct Point{
  11. int num,flag;
  12. };
  13.  
  14. struct Node{
  15. Point state[];
  16. int step;
  17. }st;
  18.  
  19. bool mark[];
  20.  
  21. int Get_Hash(Node &node)
  22. {
  23. int a[],val=,cnt;
  24. for(int i=;i<;i++)a[i]=abs(node.state[i].num);
  25. for(int i=;i<;i++){
  26. cnt=;
  27. for(int j=;j<i;j++){
  28. if(a[j]>a[i])cnt++;
  29. }
  30. val+=cnt*fac[i];
  31. }
  32. return val;
  33. }
  34.  
  35. bool Judge(Node &node)
  36. {
  37. for(int i=;i<;i++){
  38. if(abs(node.state[i].num)>abs(node.state[i+].num))
  39. return false;
  40. }
  41. return true;
  42. }
  43.  
  44. Node Get_Node(Node &p,int pos1,int pos2,int dir)
  45. {
  46. //pos1->pos2 left
  47. if(dir==){
  48. if(pos1<pos2){
  49. int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
  50. for(int i=pos1;i<=pos2-;i++){
  51. p.state[i].num=p.state[i+].num;
  52. p.state[i].flag=p.state[i+].flag;
  53. }
  54. p.state[pos2-].num=tmp;
  55. p.state[pos2-].flag=flag;
  56. }else {
  57. int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
  58. for(int i=pos1;i>pos2;i--){
  59. p.state[i].num=p.state[i-].num;
  60. p.state[i].flag=p.state[i-].flag;
  61. }
  62. p.state[pos2].num=tmp,p.state[pos2].flag=flag;
  63. }
  64. }else if(dir==){ //pos1->pos2 right
  65. if(pos1<pos2){
  66. int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
  67. for(int i=pos1;i<=pos2-;i++){
  68. p.state[i].num=p.state[i+].num;
  69. p.state[i].flag=p.state[i+].flag;
  70. }
  71. p.state[pos2].num=tmp,p.state[pos2].flag=flag;
  72. }else {
  73. int tmp=p.state[pos1].num,flag=p.state[pos1].flag;
  74. for(int i=pos1;i>=pos2+;i--){
  75. p.state[i].num=p.state[i-].num;
  76. p.state[i].flag=p.state[i-].flag;
  77. }
  78. p.state[pos2+].num=tmp,p.state[pos2+].flag=flag;
  79. }
  80. }
  81. return p;
  82. }
  83.  
  84. void bfs()
  85. {
  86. memset(mark,false,sizeof(mark));
  87. queue<Node>que;
  88. que.push(st);
  89. mark[Get_Hash(st)]=true;
  90. while(!que.empty()){
  91. Node q,pp,p=que.front();
  92. que.pop();
  93. if(Judge(p)){
  94. printf("%d\n",p.step);
  95. return ;
  96. }
  97. for(int i=;i<;i++){
  98. for(int j=;j<;j++)if(i!=j){
  99. if(p.state[i].flag!=p.state[j].flag&&is_prime[abs(p.state[i].num)+abs(p.state[j].num)]){
  100. for(int k=;k<;k++){
  101. pp=p;
  102. q=Get_Node(pp,i,j,k);
  103. int val=Get_Hash(q);
  104. q.step=p.step+;
  105. if(!mark[val]){
  106. mark[val]=true;
  107. que.push(q);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. puts("-1");
  115. }
  116.  
  117. int main()
  118. {
  119. int _case,t=;
  120. scanf("%d",&_case);
  121. while(_case--){
  122. for(int i=;i<;i++){
  123. scanf("%d",&st.state[i].num);
  124. st.state[i].flag=(st.state[i].num>?:-);
  125. }
  126. st.step=;
  127. printf("Case %d: ",t++);
  128. bfs();
  129. }
  130. return ;
  131. }

loj 1165(bfs+康托展开)的更多相关文章

  1. HDU_1043 Eight 【逆向BFS + 康托展开 】【A* + 康托展开 】

    一.题目 http://acm.hdu.edu.cn/showproblem.php?pid=1043 二.两种方法 该题很明显,是一个八数码的问题,就是9宫格,里面有一个空格,外加1~8的数字,任意 ...

  2. hdu1430魔板(BFS+康托展开)

    做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...

  3. Aizu0121 Seven Puzzle(bfs+康托展开)

    https://vjudge.net/problem/Aizu-0121 比八数码要水的多,bfs. 但是做的时候我把康托展开记错了,wa了好几次. 附上康托展开博客详解:https://blog.c ...

  4. 蓝桥杯 历届试题 九宫重排 (bfs+康托展开去重优化)

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

  5. HDU 1043 Eight(双向BFS+康托展开)

    http://acm.hdu.edu.cn/showproblem.php?pid=1043 题意:给出一个八数码,求出到达指定状态的路径. 思路:路径寻找问题.在这道题里用到的知识点挺多的.第一次用 ...

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

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

  7. HDU_1430 魔板 【BFS+康托展开+置换】

    一.题面 POJ1430 二.分析 该题与之前做的八数码不同,它是一个2*4的棋盘,并且没有空的区域.这样考虑的情况是很少的,依然结合康托展开,这时康托展开最多也只乘7的阶乘,完全可以BFS先预处理一 ...

  8. HDU - 1430 魔板 【BFS + 康托展开 + 哈希】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...

  9. hdu 5012 bfs 康托展开

    Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

随机推荐

  1. QueryHelp

    //辅助查询 Author:高兵兵 public class QueryHelp { #region IList<T> ToList<T>(string cmdText,str ...

  2. iOS多线程介绍

    一.线程概述 有些程序是一条直线,起点到终点:有些程序是一个圆,不断循环,直到将它切断.直线的如简单的Hello World,运行打印完,它的生命周期便结束了,像昙花一现那样:圆如操作系统,一直运行直 ...

  3. mingw32-g++.exe: *: No such file or directory错误解决方法

    初次使用CodeBlocks,好不容易把环境配好, 编译没有错误了,但是程序并不生成exe,提示以下问题: mingw32-g++.exe: /W3: No such file or director ...

  4. ldconfig deferred processing now taking place

    在ubuntu下面安装软件,安装结束后,提示:ldconfig deferred processing now taking place 到网上查询了一下,大概意思是说:软件安装完了,是否要重启电脑.

  5. jQuery1.11源码分析(9)-----初始化jQuery对象的函数和关联节点获取函数

    这篇也没什么好说的,初始化jQuery对象的函数要处理多种情况,已经被寒冬吐槽烂了.关联节点获取函数主要基于两个工具函数dir和sibling,前者基于指定的方向遍历,后者则遍历兄弟节点(真的不能合并 ...

  6. Run UliPad 4.1 Under Windows 7 64bit and wxPython 3.0.2

    Abstract: UliPad that is developed by limodou is an excellent code editor. It works well with wxPyth ...

  7. Windows主机里利用VMware安装Linux(CentOS)虚拟机,Host-only连接上网方式详解

    关于Host-only指的是主机与虚拟机之间的互联,因此虚拟机是不能连网的,若需要连网则需要使用NAT模式: Host-only模式实现联网得考虑如下配置过程: 附:VMware虚拟机三种网络模式(B ...

  8. HTML中的IE条件注释

    IE条件注释是一种特殊的HTML注释,这种注释只有IE5.0及以上版本才能理解.比如普通的HTML注释是: <!--This is a comment--> 而只有IE可读的IE条件注释是 ...

  9. unity3d iPhone文件目录介绍

    原地址:http://cl314413.blog.163.com/blog/static/190507976201210259126559/ 如何查看iPhone文件存放目录?首先需要越狱,越狱后打开 ...

  10. HDU 4435 charge-station bfs图论问题

    E - charge-station Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...