Eight II

Time Limit: 2000ms
Memory Limit: 65536KB

This problem will be judged on HDU. Original ID: 3567
64-bit integer IO format: %I64d      Java class name: Main

 
Eight-puzzle, which is also called "Nine grids", comes from an old game.

In this game, you are given a 3 by 3 board and 8 tiles. The tiles are numbered from 1 to 8 and each covers a grid. As you see, there is a blank grid which can be represented as an 'X'. Tiles in grids having a common edge with the blank grid can be moved into that blank grid. This operation leads to an exchange of 'X' with one tile.

We use the symbol 'r' to represent exchanging 'X' with the tile on its right side, and 'l' for the left side, 'u' for the one above it, 'd' for the one below it.

A state of the board can be represented by a string S using the rule showed below.

The problem is to operate an operation list of 'r', 'u', 'l', 'd' to turn the state of the board from state A to state B. You are required to find the result which meets the following constrains:
1. It is of minimum length among all possible solutions.
2. It is the lexicographically smallest one of all solutions of minimum length.

 

Input

The first line is T (T <= 200), which means the number of test cases of this problem.

The input of each test case consists of two lines with state A occupying the first line and state B on the second line.
It is guaranteed that there is an available solution from state A to B.

 

Output

For each test case two lines are expected.

The first line is in the format of "Case x: d", in which x is the case number counted from one, d is the minimum length of operation list you need to turn A to B.
S is the operation list meeting the constraints and it should be showed on the second line.

 

Sample Input

  1. 2
  2. 12X453786
  3. 12345678X
  4. 564178X23
  5. 7568X4123

Sample Output

  1. Case 1: 2
  2. dd
  3. Case 2: 8
  4. urrulldr

Source

 
解题:经典的八数码。。。。。IDA*大法好。。。。。。注意输出字典序最小的
 
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <vector>
  8. #include <queue>
  9. #include <cstdlib>
  10. #include <string>
  11. #include <set>
  12. #include <stack>
  13. #define LL long long
  14. #define pii pair<int,int>
  15. #define INF 0x3f3f3f3f
  16. using namespace std;
  17. const int maxn = ;
  18. struct sta{
  19. int x,y;
  20. sta(int a = ,int b = ){
  21. x = a;
  22. y = b;
  23. }
  24. };
  25. char mp[maxn][maxn];
  26. sta s[];
  27. int nowx,nowy;
  28. int h(){
  29. int tmp = ;
  30. for(int i = ; i < ; i++){
  31. int x = i/,y = i%;
  32. if(mp[x][y] == 'X') continue;
  33. tmp += abs(x - s[mp[x][y]-''].x) + abs(y - s[mp[x][y]-''].y);
  34. }
  35. return tmp;
  36. }
  37. int ans[],limit;
  38. const int dir[][] = {,,,-,,,-,};
  39. const char d[] = {'d','l','r','u'};
  40. bool ok;
  41. int IDAstar(int x,int y,int p,int cur){
  42. int bound = INF,tmp;
  43. int hv = h();
  44. if(cur + hv > limit) return cur + hv;
  45. if(hv == ) {ok = true;return cur;}
  46. for(int i = ; i < ; i++){
  47. if(i == p) continue;
  48. int tx = x + dir[i][];
  49. int ty = y + dir[i][];
  50. if(tx < || tx >= || ty < || ty >= ) continue;
  51. swap(mp[x][y],mp[tx][ty]);
  52. ans[cur] = i;
  53. int nbound = IDAstar(tx,ty,-i,cur+);
  54. if(ok) return nbound;
  55. bound = min(bound,nbound);
  56. swap(mp[x][y],mp[tx][ty]);
  57. }
  58. return bound;
  59. }
  60. int main() {
  61. int t,cs = ;
  62. char ch;
  63. scanf("%d",&t);
  64. getchar();
  65. while(t--){
  66. for(int i = ; i < ; i++){
  67. ch = getchar();
  68. if(ch == 'X'){
  69. nowx = i/;
  70. nowy = i%;
  71. }
  72. mp[i/][i%] = ch;
  73. }
  74. getchar();
  75. for(int i = ; i < ; i++){
  76. ch = getchar();
  77. if(ch == 'X') continue;
  78. s[ch-''] = sta(i/,i%);
  79. }
  80. getchar();
  81. limit = h();
  82. ok = false;
  83. while(!ok) limit = IDAstar(nowx,nowy,-,);
  84. printf("Case %d: %d\n",cs++,limit);
  85. for(int i = ; i < limit; i++)
  86. putchar(d[ans[i]]);
  87. putchar('\n');
  88. }
  89. return ;
  90. }

HDU 3567 Eight II的更多相关文章

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

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

  2. HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3

    http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...

  3. HDU 3567 Eight II BFS预处理

    题意:就是八数码问题,给你开始的串和结束的串,问你从开始到结束的最短且最小的变换序列是什么 分析:我们可以预处理打表,这里的这个题可以和HDU1430魔板那个题采取一样的做法 预处理打表,因为八数码问 ...

  4. HDU - 3567 Eight II (bfs预处理 + 康托) [kuangbin带你飞]专题二

    类似HDU1430,不过本题需要枚举X的九个位置,分别保存状态,因为要保证最少步数.要保证字典序最小的话,在扩展节点时,方向顺序为:down, left, right, up. 我用c++提交1500 ...

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

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

  6. HDU 2236 无题II(二分图匹配+二分)

    HDU 2236 无题II 题目链接 思路:行列仅仅能一个,想到二分图,然后二分区间长度,枚举下限.就能求出哪些边是能用的,然后建图跑二分图,假设最大匹配等于n就是符合的 代码: #include & ...

  7. Eight II HDU - 3567

    Eight II Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 130000/65536 K (Java/Others)Total S ...

  8. HDU 5919 Sequence II(主席树+逆序思想)

    Sequence II Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  9. hdu 1430+hdu 3567(预处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路:由于只是8种颜色,所以标号就无所谓了,对起始状态重新修改标号为 12345678,对目标状 ...

随机推荐

  1. [Linux]RedHat Linux 忘记rootpassword该怎样又一次设置password

    1. 开机在出现grub画面,按e键,例如以下图所看到的: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU3VubnlZb29uYQ==/font/5a6 ...

  2. luogu2441 角色属性树

    题目大意:维护一个可查询.修改的树,查询的是一个节点的:离它距离最近的.组成两个节点Key值的质因数存在交集的.祖先节点:修改是修改一个节点的key值. 如果组成两个Key值的质因数存在交集,则两个数 ...

  3. luogu2303 [SDOI2012] Longge的问题

    题目大意:给出n,求sum foreach i(1<=i<=n) (gcd(n, i)). 1~n有太多的数,但是n与m的最大公约数却有很多重复.所以我们枚举最大公约数k,然后让k乘以与n ...

  4. tiny4412开机动画、开机界面的定制 【原创】

    关键词:Android  linux 开机logo 开机动画  平台信息:内核:linux3.0.68 系统:android/android5.1平台:tiny4412 作者:庄泽彬(欢迎转载,请注明 ...

  5. POJ3349 Language: Snowflake Snow Snowflakes

    POJ3349 Language: Snowflake Snow Snowflakes 题目:传送门 题解: 链表+hash的一道水题 填个坑补个漏... 代码: #include<cstdio ...

  6. nyoj--491--幸运三角形(dfs)

    幸运三角形 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 话说有这么一个图形,只有两种符号组成('+'或者'-'),图形的最上层有n个符号,往下个数依次减一,形成倒置的 ...

  7. 【CQOI 2009】 余数之和

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1257 [算法] k mod i = k - [k / i] * i 所以 (k mo ...

  8. Codeforces Round #512 (Div. 2) D.Vasya and Triangle 数学

    题面 题意:给你n,m,k,在你在(0,0)到(n,m)的矩形内,选3个格点(x,y都是整数),使得三角形面积为n*m/k,不能找到则输出-1 题解:由毕克定理知道,格点多边形的面积必为1/2的整数倍 ...

  9. 6.Renderer Window

    渲染是实时的,所见即所得.同时还可以输出一些统计信息. Pixel Snoop:获取颜色值,同时把该值复制到剪贴板,主要用户是获取颜色值 Wireframe:开启后可以查看3D节点图形骨架 Stati ...

  10. springdatajpa使用informix数据库出现no such column 异常的问题

    本博客属原创,转载请注明出处 问题描述: 环境: spring data jpa版本4.0.3 informix驱动版本3.50.JC9 程序结构 jpa配置文件对应的jdbc配置 dao层继承jpa ...