题意  输出八数码问题从给定状态到12345678x的路径

用康托展开将排列相应为整数  即这个排列在全部排列中的字典序  然后就是基础的BFS了

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 5e5, M = 9;
  4. int x[4] = { -1, 1, 0, 0};
  5. int y[4] = {0, 0, -1, 1};
  6. int fac[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320};
  7. int puz[N][M], nex[N], dir[N], vis[N], q[N];
  8.  
  9. int getCantor(int a[]) //康托展开 将排列转化为整数
  10. {
  11. int ret = 0;
  12. for(int i = 0; i < M; ++i)
  13. {
  14. for(int j = i + 1; j < M; ++j)
  15. if(a[j] < a[i]) ret += fac[M - i - 1];
  16. }
  17. return ret;
  18. }
  19.  
  20. void bfs()
  21. {
  22. int t[M] = {1, 2, 3, 4, 5, 6, 7, 8, 0};
  23. int id = getCantor(t);
  24. dir[id] = -1;
  25.  
  26. memcpy(puz[id], t, sizeof(t));
  27. memset(vis, 0, sizeof(vis));
  28.  
  29. int r, c, k, nr, nc, nk, nid;
  30. int front = 0, rear = 0;
  31. q[rear++] = id;
  32. vis[id] = 1;
  33.  
  34. while(front < rear)
  35. {
  36. int id = q[front++];
  37. memcpy(t, puz[id], sizeof(t));
  38. for(k = 0; t[k]; ++k); //找0的位置
  39. r = k / 3, c = k % 3; //一维转二维
  40.  
  41. for(int i = 0; i < 4; ++i)
  42. {
  43. nr = r + x[i], nc = c + y[i], nk = nr * 3 + nc;
  44.  
  45. if(nr < 0 || nr > 2 || nc < 0 || nc > 2) continue;
  46. swap(t[k], t[nk]);
  47. nid = getCantor(t);
  48. memcpy(puz[nid], t, sizeof(t));
  49. swap(t[k], t[nk]);
  50.  
  51. if(vis[nid]) continue;
  52. vis[nid] = 1;
  53. q[rear++] = nid;
  54. nex[nid] = id;
  55. dir[nid] = i;
  56. }
  57. }
  58. }
  59.  
  60. int main()
  61. {
  62. char t[5], sdir[] = "durl";
  63. int s[M], id;
  64. bfs();
  65.  
  66. while(~scanf("%s", t))
  67. {
  68. s[0] = t[0] == 'x' ? 0 : t[0] - '0';
  69. for(int i = 1; i < M; ++i)
  70. {
  71. scanf("%s", t);
  72. s[i] = t[0] == 'x' ? 0 : t[0] - '0';
  73. }
  74.  
  75. id = getCantor(s);
  76. if(!vis[id]) puts("unsolvable");
  77. else
  78. {
  79. while(dir[id] >= 0)
  80. {
  81. printf("%c", sdir[dir[id]]);
  82. id = nex[id];
  83. }
  84. puts("");
  85. }
  86. }
  87. return 0;
  88. }
  89. //Last modified : 2015-07-05 11:15

  1.  

Eight

Problem Description
The 15-puzzle has been around for over 100 years; even if you don't know it by that name, you've seen it. It is constructed with 15 sliding tiles, each with a number from 1 to 15 on it, and all packed into a 4 by 4 frame with one tile missing. Let's call the
missing tile 'x'; the object of the puzzle is to arrange the tiles so that they are ordered as:

  1. 1 2 3 4
  2. 5 6 7 8
  3. 9 10 11 12
  4. 13 14 15 x

where the only legal operation is to exchange 'x' with one of the tiles with which it shares an edge. As an example, the following sequence of moves solves a slightly scrambled puzzle:

  1. 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4
  2. 5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8
  3. 9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12
  4. 13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x
  5. r-> d-> r->

The letters in the previous row indicate which neighbor of the 'x' tile is swapped with the 'x' tile at each step; legal values are 'r','l','u' and 'd', for right, left, up, and down, respectively. 



Not all puzzles can be solved; in 1870, a man named Sam Loyd was famous for distributing an unsolvable version of the puzzle, and 

frustrating many people. In fact, all you have to do to make a regular puzzle into an unsolvable one is to swap two tiles (not counting the missing 'x' tile, of course). 



In this problem, you will write a program for solving the less well-known 8-puzzle, composed of tiles on a three by three 

arrangement.

 
Input
You will receive, several descriptions of configuration of the 8 puzzle. One description is just a list of the tiles in their initial positions, with the rows listed from top to bottom, and the tiles listed from left to right within a row, where the tiles are
represented by numbers 1 to 8, plus 'x'. For example, this puzzle 



1 2 3 

x 4 6 

7 5 8 



is described by this list: 



1 2 3 x 4 6 7 5 8
 
Output
You will print to standard output either the word ``unsolvable'', if the puzzle has no solution, or a string consisting entirely of the letters 'r', 'l', 'u' and 'd' that describes a series of moves that produce a solution. The string should include no spaces
and start at the beginning of the line. Do not print a blank line between cases.
 
Sample Input
  1. 2 3 4 1 5 x 7 6 8
 
Sample Output
  1. ullddrurdllurdruldr
 

HDU 1043 Eight (BFS&#183;八数码&#183;康托展开)的更多相关文章

  1. HDU 1043 Eight 【经典八数码输出路径/BFS/A*/康托展开】

    本题有写法好几个写法,但主要思路是BFS: No.1 采用双向宽搜,分别从起始态和结束态进行宽搜,暴力判重.如果只进行单向会超时. No.2 采用hash进行判重,宽搜采用单向就可以AC. No.3 ...

  2. hdu1043Eight (经典的八数码)(康托展开+BFS)

    建议先学会用康托展开:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description The 15-puzzle ...

  3. HDU 3567 Eight II(八数码 II)

    HDU 3567 Eight II(八数码 II) /65536 K (Java/Others)   Problem Description - 题目描述 Eight-puzzle, which is ...

  4. Eight HDU - 1043 (双向BFS)

    记得上人工智能课的时候老师讲过一个A*算法,计算估价函数(f[n]=h[n]+g[n])什么的,感觉不是很好理解,百度上好多都是用逆向BFS写的,我理解的逆向BFS应该是从终点状态出发,然后把每一种状 ...

  5. HDU 1043 Eight BFS

    题意:就是恢复成1,2,3,4,5,6,7,8,0; 分析:暴力BFS预处理,所有路径,用康拓展开判重,O(1)打印 93ms 还是很快的 #include <iostream> #inc ...

  6. BFS:八数码问题

    #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> ...

  7. POJ 1077 Eight (BFS+康托展开)详解

    本题知识点和基本代码来自<算法竞赛 入门到进阶>(作者:罗勇军 郭卫斌) 如有问题欢迎巨巨们提出 题意:八数码问题是在一个3*3的棋盘上放置编号为1~8的方块,其中有一块为控制,与空格相邻 ...

  8. hdu 1043 Eight (八数码问题)【BFS】+【康拓展开】

    <题目链接> 题目大意:给出一个3×3的矩阵(包含1-8数字和一个字母x),经过一些移动格子上的数后得到连续的1-8,最后一格是x,要求最小移动步数. 解题分析:本题用BFS来寻找路径,为 ...

  9. hdu 1043 Eight 经典八数码问题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 The 15-puzzle has been around for over 100 years ...

随机推荐

  1. js图片延迟加载如何实现

      这里延迟加载的意思是,拖动滚动条时,在图片出现在浏览器显示区域后才加载显示. 大概的实现方式是: 在页面的load没有触发之前,把所有的指定id的元素内的img放入到imgs中,将所有的图片的sr ...

  2. [eclipse相关] eclipse 安装svn插件

    最近看到别人带主题的eclipse,非常羡慕,所以也换了一个eclipse,版本是java ee luna 4.4.2,然后得偿所愿有了花花绿绿的代码界面:) 但是差点被svn搞死,~~~~(> ...

  3. linux使用yum的方式安装mysql实践

    1.先检测是否已安装mysql ps -ef|grep mysql root : pts/ :: /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mys ...

  4. Deploy .Net project automatically with MsBuild and MsDeploy (0)

    I will use a example of my project to show how to use MS Build and MS Deploy in a real project and s ...

  5. AutoMapper IIS回收引发的 未将对象引用设置到对象实例

    一.前言 最近使用AutoMapper的时候,一段时间久会产生System.NullReferenceException:未将对象引用设置到对象的实例.这个问题.后来通过测试,发现部署在IIS上的站点 ...

  6. android wear开发之:创建可穿戴设备应用 - Creating Wearable Apps

    注:本文内容来自:https://developer.android.com/training/wearables/apps/index.html 翻译水平有限,如有疏漏,欢迎批评指教. 译:山人 创 ...

  7. c#中将IP地址转换成无符号整形数的方法与逆变换方法

    我们知道 IP地址就是给每个连接在Internet上的主机分配的一个32bit地址. 按照TCP/IP协议规定,IP地址用二进制来表示,每个IP地址长32bit,比特换算成字节,就是4个字节.而c#中 ...

  8. [转载] Linux五种IO模型

      转载:http://blog.csdn.net/jay900323/article/details/18141217     Linux五种IO模型性能分析   目录(?)[-] 概念理解 Lin ...

  9. javaweb-2-Tomcat初步学习与使用

    一.Tomcat服务器简介(此点网上官方有详尽的解释,故此不赘述,以学习使用为主) Apache Jakarta的开源项目 JSP/Servlet容器 二.Tomcat的目录结构 三.启动和停止Tom ...

  10. Unity3D高性能战争迷雾实现

    效果图 先上效果图吧,这是为了吸引到你们的ヽ(。◕‿◕。)ノ゚ 战争迷雾效果演示图 战争调试界面演示图 由于是gif录制,为了压缩图片,帧率有点低,实际运行时,参数调整好是不会像这样一卡一顿的. 战争 ...