Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 1123 Accepted Submission(s): 595

Problem Description

Let’s play a card game called Gap.

You have 28 cards labeled with two-digit numbers. The first digit (from 1 to 4) represents the suit of the card, and the second digit (from 1 to 7) represents the value of the card.

First, you shu2e the cards and lay them face up on the table in four rows of seven cards, leaving a space of one card at the extreme left of each row. The following shows an example of initial layout.

Next, you remove all cards of value 1, and put them in the open space at the left end of the rows: “11” to the top row, “21” to the next, and so on.

Now you have 28 cards and four spaces, called gaps, in four rows and eight columns. You start moving cards from this layout.

At each move, you choose one of the four gaps and fill it with the successor of the left neighbor of the gap. The successor of a card is the next card in the same suit, when it exists. For instance the successor of “42” is “43”, and “27” has no successor.

In the above layout, you can move “43” to the gap at the right of “42”, or “36” to the gap at the right of “35”. If you move “43”, a new gap is generated to the right of “16”. You cannot move any card to the right of a card of value 7, nor to the right of a gap.

The goal of the game is, by choosing clever moves, to make four ascending sequences of the same suit, as follows.

Your task is to find the minimum number of moves to reach the goal layout.

Input

The input starts with a line containing the number of initial layouts that follow.

Each layout consists of five lines - a blank line and four lines which represent initial layouts of four rows. Each row has seven two-digit numbers which correspond to the cards.

Output

For each initial layout, produce a line with the minimum number of moves to reach the goal layout. Note that this number should not include the initial four moves of the cards of value 1. If there is no move sequence from the initial layout to the goal layout, produce “-1”.

Sample Input

4

12 13 14 15 16 17 21

22 23 24 25 26 27 31

32 33 34 35 36 37 41

42 43 44 45 46 47 11

26 31 13 44 21 24 42

17 45 23 25 41 36 11

46 34 14 12 37 32 47

16 43 27 35 22 33 15

17 12 16 13 15 14 11

27 22 26 23 25 24 21

37 32 36 33 35 34 31

47 42 46 43 45 44 41

27 14 22 35 32 46 33

13 17 36 24 44 21 15

43 16 45 47 23 11 26

25 37 41 34 42 12 31

Sample Output

0

33

60

-1

【题目链接】:http://acm.hdu.edu.cn/showproblem.php?pid=1067

【题解】



搜索题。

这题的判重方法和八数码类似;

把整张4*8的图降成一维的图;

判重的时候转换成一个字符串判重就好;

在bfs的队列里面

记录

{

———-4*7个数码在哪一个位置;

———-4个空格的位置;

———-当前的步数;

———-这张图用一维字符串的表示;

}

在扩展的时候

枚举4个空格.

看看4个空格左边是什么;

如果是0或者尾数为7就跳过;

否则找到比它大1的数码(我们有记录)的位置;

(二维的坐标和一维的坐标可以通过(x-1)*8+y来转换)

然后调换字符串中两个数目;

更改那个被调换的数目的位置(不要忘了!)

更改空格的位置;

修改当前步数;

入队。

如此循环一下就OK了.

判重用map



【完整代码】

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define lson l,m,rt<<1
  4. #define rson m+1,r,rt<<1|1
  5. #define LL long long
  6. #define rep1(i,a,b) for (int i = a;i <= b;i++)
  7. #define rep2(i,a,b) for (int i = a;i >= b;i--)
  8. #define mp make_pair
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define rei(x) scanf("%d",&x)
  13. #define rel(x) scanf("%I64d",&x)
  14. typedef pair<int,int> pii;
  15. typedef pair<LL,LL> pll;
  16. const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
  17. const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
  18. const int goal[32] = {11,12,13,14,15,16,17,0,21,22,23,24,25,26,27,0,31,32,33,34,35,36,37,0,41,42,43,44,45,46,47,0};
  19. const double pi = acos(-1.0);
  20. const int MAXN = 110;
  21. struct abc
  22. {
  23. pii po[50],kong[5];
  24. int dis;
  25. string s;
  26. };
  27. char chushi[5][10];
  28. string sgoal,ts;
  29. map <string,int> dic;
  30. queue <abc> dl;
  31. bool bfs()
  32. {
  33. dic.clear();
  34. while (!dl.empty()) dl.pop();
  35. abc pre;
  36. pre.dis = 0;pre.s = " ";
  37. int num = 0;
  38. rep1(i,1,4)
  39. {
  40. rep1(j,1,8)
  41. {
  42. pre.s+=chushi[i][j];
  43. if (chushi[i][j]==0)
  44. pre.kong[++num] = {i,j};
  45. else
  46. pre.po[int(chushi[i][j])] = {i,j};
  47. }
  48. }
  49. dic[pre.s] = 1;
  50. if (dic[sgoal])
  51. {
  52. puts("0");
  53. return true;
  54. }
  55. dl.push(pre);
  56. while (!dl.empty())
  57. {
  58. abc temp = dl.front();dl.pop();
  59. for (int i = 1;i <= 4;i++)
  60. {
  61. abc t = temp;
  62. int tx = t.kong[i].fi,ty = t.kong[i].se;
  63. int sz = (tx-1)*8+ty;
  64. int judge = t.s[sz-1];
  65. if (judge==0 || judge%10==7) continue;
  66. int tx1 = t.po[judge+1].fi,ty1 = t.po[judge+1].se;
  67. int sz1 = (tx1-1)*8+ty1;
  68. swap(t.s[sz],t.s[sz1]);
  69. if (!dic[t.s])
  70. {
  71. dic[t.s] = 1;
  72. t.kong[i] = {tx1,ty1};
  73. t.po[judge+1] = {tx,ty};
  74. t.dis++;
  75. if (dic[sgoal])
  76. {
  77. printf("%d\n",t.dis);
  78. return true;
  79. }
  80. dl.push(t);
  81. }
  82. }
  83. }
  84. return false;
  85. }
  86. int main()
  87. {
  88. //freopen("F:\\rush.txt","r",stdin);
  89. sgoal = " ";
  90. for (int i = 0;i <= 31;i++)
  91. sgoal+=goal[i];
  92. int T;
  93. rei(T);
  94. while (T--)
  95. {
  96. memset(chushi,0,sizeof chushi);
  97. rep1(i,1,4)
  98. rep1(j,2,8)
  99. {
  100. int x;
  101. rei(x);
  102. chushi[i][j] = x;
  103. if (x%10==1)
  104. swap(chushi[x/10][1],chushi[i][j]);
  105. }
  106. if (!bfs())
  107. puts("-1");
  108. }
  109. return 0;
  110. }

【hdu 1067】Gap的更多相关文章

  1. 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题

    [HDU  3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...

  2. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  3. -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】

    [把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...

  4. 【HDU 2196】 Computer(树的直径)

    [HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...

  5. 【HDU 2196】 Computer (树形DP)

    [HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...

  6. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

  7. 【hdu 1043】Eight

    [题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=1043 [题意] 会给你很多组数据; 让你输出这组数据到目标状态的具体步骤; [题解] 从12345 ...

  8. 【HDU 3068】 最长回文

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3068 [算法] Manacher算法求最长回文子串 [代码] #include<bits/s ...

  9. 【HDU 4699】 Editor

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4699 [算法] 维护两个栈,一个栈放光标之前的数,另外一个放光标之后的数 在维护栈的同时求最大前缀 ...

随机推荐

  1. java 钩子方法

    Runtime.getRuntime().addShutdownHook(shutdownHook);    这个方法的含义说明:        这个方法的意思就是在jvm中增加一个关闭的钩子,当jv ...

  2. Android 小米盒子游戏手柄按键捕获 - 能获取到的 home 键依旧是个痛

    Android 小米盒子游戏手柄按键捕获 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 ...

  3. 3. Spring Boot Servlet

    转自:https://blog.csdn.net/catoop/article/details/50501686

  4. 洛谷—— P1434 滑雪

    https://www.luogu.org/problem/show?pid=1434#sub 题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜 ...

  5. UVA 488 - Triangle Wave 水~

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. [Angular] Setup automated deployment with Angular, Travis and Firebase

    Automate all the things!! Automation is crucial for increasing the quality and productivity. In this ...

  7. 基于深度学习的人脸识别系统(Caffe+OpenCV+Dlib)【二】人脸预处理

    前言 基于深度学习的人脸识别系统,一共用到了5个开源库:OpenCV(计算机视觉库).Caffe(深度学习库).Dlib(机器学习库).libfacedetection(人脸检测库).cudnn(gp ...

  8. Springboot+shiro配置笔记+错误小结(转)

    软件152 尹以操 springboot不像springmvc,它没有xml配置文件,那该如何配置shiro呢,其实也不难,用java代码+注解来解决这个问题.仅以此篇记录我对shiro的学习,如有对 ...

  9. .dmp文件导出使用示例

    exp导出的几种用例,先睹为快: 1 将数据库SampleDB完全导出,用户名system 密码manager 导出到E:/SampleDB.dmp中 exp system/manager@TestD ...

  10. 25、写一个USB摄像头驱动程序(有ioctrl分析)

    videobuf2-core.h中的vb2_buffer,记录了v4l2_buffer ,驱动可以对vb2_buffer的v4l2_buffer进行操控, vb2_buffer是v4l2框架层的代码, ...