Eight

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 14380    Accepted Submission(s): 4044 Special Judge

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 5 6 7 8 9 10 11 12 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 5 6 7 8 5 6 7 8 5 6 7 8 5 6 7 8 9 x 10 12 9 10 x 12 9 10 11 12 9 10 11 12 13 14 11 15 13 14 11 15 13 14 x 15 13 14 15 x 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
2 3 4 1 5 x 7 6 8
 
Sample Output
ullddrurdllurdruldr
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<ctype.h>
  5. typedef long long ll ;
  6. int map[][] ;
  7. char mp[] ;
  8. int l , r , l1 , r1 ;
  9. int flag ;
  10. int anti ;
  11. int move[][] = {{,} , {-,} , {,} , {,-}} ;
  12. const int mod = ;
  13. struct node
  14. {
  15. int x , y , nxt ;
  16. int map[][] ;
  17. } b[mod];
  18. struct hash
  19. {
  20. ll w , id ;
  21. int nxt ;
  22. }e[mod * ];
  23. int H[mod * ] , E ;
  24.  
  25. void insert (ll x , int id)
  26. {
  27. int y = x % mod ;
  28. if (y < ) y += mod ;
  29. e[++ E].w = x ;
  30. e[E].id = id ;
  31. e[E].nxt = H[y] ;
  32. H[y] = E ;
  33. }
  34.  
  35. int find (ll x)
  36. {
  37. int y = x % mod ;
  38. if (y < ) y += mod ;
  39. for (int i = H[y] ; ~ i ; i = e[i].nxt ) {
  40. if (e[i].w == x)
  41. return e[i].id ;
  42. }
  43. return - ;
  44. }
  45.  
  46. void table ()
  47. {
  48. node ans , tmp ;
  49. E = - ; memset (H , - , sizeof (H) ) ;
  50. l1 = , r1= ;
  51. b[l1].x = , b[l1].y = , b[l1].nxt = - ;
  52. ll sum = ;
  53. for (int i = ; i < ; i ++) for (int j = ; j < ; j ++) b[l1].map[i][j] = i * + j + ;
  54. insert ( , ) ;
  55. while (l1 != r1) {
  56. ans = b[l1] ;
  57. for (int i = ; i < ; i ++) {
  58. tmp.x = ans.x + move[i][] ; tmp.y = ans.y + move[i][] ;
  59. if (tmp.x < || tmp.y < || tmp.x >= || tmp.y >= ) continue ;
  60. for (int i = ; i < ; i ++) for (int j = ; j < ; j ++) tmp.map[i][j] = ans.map[i][j] ;
  61. std::swap (tmp.map[ans.x][ans.y] , tmp.map[tmp.x][tmp.y]) ;
  62. sum = ;
  63. for (int i = ; i < ; i ++) for (int j = ; j < ; j ++) sum = sum * + tmp.map[i][j] ;
  64. if (find (sum) != - ) continue ;
  65. insert (sum , r1 ) ;
  66. tmp.nxt = l1 ;
  67. b[r1 ++] = tmp ;
  68. }
  69. l1 ++ ;
  70. }
  71. }
  72.  
  73. void solve (int u)
  74. {
  75. if (u == -) return ;
  76. int t = b[u].nxt ;
  77. if (t != -) {
  78. int x = b[t].x - b[u].x , y = b[t].y - b[u].y ;
  79. if (x == ) printf ("d") ;
  80. else if (x == -) printf ("u") ;
  81. else if (y == ) printf ("r") ;
  82. else if (y == - ) printf ("l") ;
  83. }
  84. solve (t) ;
  85. }
  86.  
  87. int main ()
  88. {
  89. freopen ("a.txt" , "r" , stdin ) ;
  90. table () ;
  91. while (gets (mp) != NULL) {
  92. int tot = ;
  93. for (int i = ; mp[i] != '\0' ; i ++) {
  94. if (mp[i] != ' ') {
  95. if (isdigit (mp[i])) {
  96. map[tot / ][tot % ] = mp[i] - '' ;
  97. }
  98. else map[tot / ][tot % ] = ;
  99. tot ++ ;
  100. }
  101. }
  102. ll sum = ;
  103. for (int i = ; i < ; i ++) for (int j = ; j < ; j ++) sum = sum * + map[i][j] ;
  104. anti = find (sum) ;
  105. if (anti != -) solve (anti) ;
  106. else printf ("unsolvable") ;
  107. puts ("") ;
  108. }
  109. return ;
  110. }

打表思路很简单,也是为了对抗unsolve这种情况,从123456789最终状态产生所有状态,即可.

奇偶逆序:

逆序数:也就是说,对于n个不同的元素,先规定各元素之间有一个标准次序(例如n个 不同的自然数,可规定从小到大为标准次序),于是在这n个元素的任一排列中,当某两个元素的先后次序与标准次序不同时,就说有1个逆序。一个排列中所有逆序总数叫做这个排列的逆序数。

结论:(是除去移动元素,(这里是9))

对源状态A与目标状态B进行规范化,使得两矩阵的元素0的位置相同;记为新的源状态A'与目标状态B';
若A'与B'的逆序对的奇偶性相同(即A'与B1的逆序对的奇偶性相同),则A'必定可能转化为B',即A可以转化到B; 
若A'与B'的逆序对的奇偶性不同(即A'与B2的逆序对的奇偶性相同),则A'必定不可能转化为B',即A不可以转化到B;

也就是为了对抗unsolve.

然后用双广就OK了.

hdu.1043.Eight (打表 || 双广 + 奇偶逆序)的更多相关文章

  1. Eight hdu 1043 八数码问题 双搜

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. POJ-1077 HDU 1043 HDU 3567 Eight (BFS预处理+康拓展开)

    思路: 这三个题是一个比一个令人纠结呀. POJ-1077 爆搜可以过,94ms,注意不能用map就是了. #include<iostream> #include<stack> ...

  3. HDU 1403 Eight&POJ 1077(康拖,A* ,BFS,双广)

    Eight Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  4. hdu 1401(单广各种卡的搜索题||双广秒速)

    Solitaire Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  5. HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  6. HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)

    Eight Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30176   Accepted: 13119   Special ...

  7. HDU 1043 Eight 八数码问题 A*算法(经典问题)

    HDU 1043 Eight 八数码问题(经典问题) 题意 经典问题,就不再进行解释了. 这里主要是给你一个状态,然后要你求其到达\(1,2,3,4,5,6,7,8,x\)的转移路径. 解题思路 这里 ...

  8. HDU 1043 八数码(八境界)

    看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...

  9. Eight POJ - 1077 HDU - 1043 八数码

    Eight POJ - 1077 HDU - 1043 八数码问题.用hash(康托展开)判重 bfs(TLE) #include<cstdio> #include<iostream ...

随机推荐

  1. codevs 1013 求先序排列(二叉树遍历)

    传送门 Description 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=8). Input 两个字符串,分别是中序和后序(每行一个) Outp ...

  2. HDU 1022 Train Problem I(栈模拟)

    传送门 Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of st ...

  3. 总结jQuery选择器

    基本选择器 1. id选择器(指定id元素) 2. class选择器(遍历css类元素) 3. element选择器(遍历html元素) 4. * 选择器(遍历所有元素) 5. 并列选择器$('p,d ...

  4. 淘淘商城基于maven和svn的理解

    首先了解下maven和svn是什么: Maven是一个项目的管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目的生命周期(Project Life ...

  5. 手写控件,frame,center和bounds属性

    一.手写控件 1.手写控件的步骤 (1)使用相应的控件类创建控件对象 (2)设置该控件的各种属性 (3)添加控件到视图中 (4)如果是button等控件,还需考虑控件的单击事件等 (5)注意:View ...

  6. PHP 数组(遍历)

    数组定义$attr = array(); //定义一个空的数组$attr = array(1,2,3,4); //定义一个有值的数组$attr[0]="aa";$attr[1]=& ...

  7. Jquery 学习之基础一

    1.添加一个CSS类 $("button").click(function(){  $("#div1").addClass("important bl ...

  8. DllImport dll中有些啥函数 及 dll中是否用到了别的dll

    在加载dll的时候不知道dll中有哪些接口怎么办,或者使用别人封装的东西时报出类似于“无法在 DLL“XXX.dll”中找到名为“XXX函数”的入口点.”     1.通过LordPE这个软件来看dl ...

  9. JavaWeb学习笔记——开发动态WEB资源(五)servlet身份验证

    本工程的功能是实现Javaweb的servlet身份验证 一下是login.html文件中的代码 <!DOCTYPE html> <html> <head> < ...

  10. JS数组类型检测

    在强类型语言,数组类型检测是非常容易的事情(typeof就可以解决),而在弱语言JS数据类型就很容易混淆了. JS中常见的数据类型有:number.string.boolean.undefined.f ...