Problem Description

There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a1.a2,a3,a4,a5,a6
to be numbers written on top face, bottom face, left face, right face,
front face and back face of dice A. Similarly, consider b1.b2,b3,b4,b5,b6
to be numbers on specific faces of dice B. It’s guaranteed that all
numbers written on dices are integers no smaller than 1 and no more than
6 while ai ≠ aj and bi ≠ bj for all i ≠ j. Specially, sum of numbers on opposite faces may not be 7.

At the beginning, the two dices may face different(which means there exist some i, ai ≠ bi). Ddy wants to make the two dices look the same from all directions(which means for all i, ai = bi) only by the following four rotation operations.(Please read the picture for more information)

Now Ddy wants to calculate the minimal steps that he has to take to achieve his goal.

Input

There are multiple test cases. Please process till EOF.

For each case, the first line consists of six integers a1,a2,a3,a4,a5,a6, representing the numbers on dice A.

The second line consists of six integers b1,b2,b3,b4,b5,b6, representing the numbers on dice B.

Output

For
each test case, print a line with a number representing the answer. If
there’s no way to make two dices exactly the same, output -1.

Sample Input

1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 5 6 4 3
1 2 3 4 5 6
1 4 2 5 3 6

Sample Output

0
3
-1

这个题目可以用bfs遍历向前、向后、向左、向右转 ,这样如果用一个数组a[6]记录一种状态,那么最多也只有6!种状态,数量不是很多,可以直接暴力bfs。不过需要记录每个状态是否被访问过。

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <queue>
  10. #include <string>
  11. #include <vector>
  12. #define inf 0x3fffffff
  13. #define esp 1e-10
  14. using namespace std;
  15. struct node1
  16. {
  17. int dice[];
  18. int val;
  19. };
  20. struct node
  21. {
  22. node1 qt;
  23. int step;
  24. };
  25. node a;
  26. node1 b;
  27. int bfs()
  28. {
  29. set < int > s;
  30. s.insert(a.qt.val);
  31. queue < node > q;
  32. q.push(a);
  33. while (!q.empty())
  34. {
  35. node f, k;
  36. f = q.front();
  37. q.pop();
  38. if (f.qt.val == b.val) return f.step;
  39. //first
  40. k = f;
  41. swap (k.qt.dice[], k.qt.dice[]);
  42. swap (k.qt.dice[], k.qt.dice[]);
  43. swap (k.qt.dice[], k.qt.dice[]);
  44. k.qt.val = k.qt.dice[];
  45. for (int y = ; y < ; ++y)
  46. {
  47. k.qt.val = *k.qt.val + k.qt.dice[y];
  48. }
  49. if (s.find(k.qt.val) == s.end())
  50. {
  51. k.step ++;
  52. q.push(k);
  53. s.insert(k.qt.val);
  54. k.step --;
  55. }
  56. //second
  57. k = f;
  58. swap (k.qt.dice[], k.qt.dice[]);
  59. swap (k.qt.dice[], k.qt.dice[]);
  60. swap (k.qt.dice[], k.qt.dice[]);
  61. k.qt.val = k.qt.dice[];
  62. for (int y = ; y < ; ++y)
  63. {
  64. k.qt.val = *k.qt.val + k.qt.dice[y];
  65. }
  66. if (s.find(k.qt.val) == s.end())
  67. {
  68. k.step ++;
  69. q.push(k);
  70. s.insert(k.qt.val);
  71. k.step --;
  72. }
  73. //third
  74. k = f;
  75. swap (k.qt.dice[], k.qt.dice[]);
  76. swap (k.qt.dice[], k.qt.dice[]);
  77. swap (k.qt.dice[], k.qt.dice[]);
  78. k.qt.val = k.qt.dice[];
  79. for (int y = ; y < ; ++y)
  80. {
  81. k.qt.val = *k.qt.val + k.qt.dice[y];
  82. }
  83. if (s.find(k.qt.val) == s.end())
  84. {
  85. k.step ++;
  86. q.push(k);
  87. s.insert(k.qt.val);
  88. k.step --;
  89. }
  90. //forth
  91. k = f;
  92. swap (k.qt.dice[], k.qt.dice[]);
  93. swap (k.qt.dice[], k.qt.dice[]);
  94. swap (k.qt.dice[], k.qt.dice[]);
  95. k.qt.val = k.qt.dice[];
  96. for (int y = ; y < ; ++y)
  97. {
  98. k.qt.val = *k.qt.val + k.qt.dice[y];
  99. }
  100. if (s.find(k.qt.val) == s.end())
  101. {
  102. k.step ++;
  103. q.push(k);
  104. s.insert(k.qt.val);
  105. k.step --;
  106. }
  107. }
  108. return -;
  109. }
  110. int main()
  111. {
  112. //freopen ("test.txt", "r", stdin);
  113. while (scanf ("%d", &a.qt.dice[]) != EOF)
  114. {
  115. for (int i = ; i < ; ++i)
  116. scanf ("%d", &a.qt.dice[i]);
  117. a.step = ;
  118. a.qt.val = a.qt.dice[];
  119. for (int y = ; y < ; ++y)
  120. {
  121. a.qt.val = *a.qt.val + a.qt.dice[y];
  122. }
  123. for (int i = ; i < ; ++i)
  124. scanf ("%d", &b.dice[i]);
  125. b.val = b.dice[];
  126. for (int y = ; y < ; ++y)
  127. {
  128. b.val = *b.val + b.dice[y];
  129. }
  130. printf ("%d\n", bfs());
  131. }
  132. return ;
  133. }

ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)的更多相关文章

  1. ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

    Description There is a special number sequence which has n+1 integers. For each number in sequence, ...

  2. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  3. ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...

  4. ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...

  5. ACM学习历程—HDU 5534 Partial Tree(动态规划)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...

  6. ACM学习历程—HDU 3949 XOR(xor高斯消元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...

  7. ACM学习历程—HDU 5317 RGCDQ (数论)

    Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...

  8. ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)

    Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...

  9. ACM学习历程—HDU 5073 Galaxy(数学)

    Description Good news for us: to release the financial pressure, the government started selling gala ...

随机推荐

  1. SpringBoot启动流程分析(六):IoC容器依赖注入

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  2. WebApi 中使用 Token

    1.登陆的时候根据用户信息生成Token var token = FormsAuthentication.Encrypt( new FormsAuthenticationTicket( , " ...

  3. 洛谷 P3216 [HNOI2011]数学作业

    最近学了矩阵,kzj大佬推荐了我这一道题目. 乍一眼看上去,没看出是矩阵,就随便打了一个暴力,30分. 然后仔细分析了一波,发现蛮简单的. 结果全wa了,先看看下面的错误分析吧! 首先,设f[n]为最 ...

  4. setTimeout解决循环值的几种方法

    for(var i=0;i<5;i++){ setTimeout(function(){ console.log(`错误 ${i}`); },0) } for(var i=0;i<5;i+ ...

  5. ADT和Android SDK的安装

    本文主要涉及Android开发环境搭建时的Eclipse.ADT及Android SDK的安装方法,还有遇到的两个问题及其解决办法.其中,ADT的安装介绍了在线和离线安装两种方式.  1.安装ecli ...

  6. [转]eclipse中的常用快捷键

    1.选中你要加注释的区域,用ctrl+shift+C 会加上//注释2.先把你要注释的东西选中,用shit+ctrl+/ 会加上注释3.要修改在eclispe中的命令的快捷键方式我们只需进入windo ...

  7. 深入理解ES6之迭代器与生成器

    迭代器 迭代器 iterator,在 Javascript 中,迭代器是一个对象(也可称作为迭代器对象),它提供了一个 next() 方法,用来返回迭代序列中的下一项. next 方法的定义,next ...

  8. java-从这里开始认识

    <java是什么:>Programming language 程序语言Development environment 开发环境Application environment 应用环境Dep ...

  9. 矩阵内积和Schur补

    > Many problems in the field of signal processing have been expended into matrix problems.So it's ...

  10. spring boot项目启动报(No session repository could be auto-configured, check your configuration (session store type is 'null'))

    找到项目的application配置文件,增加 spring.session.store-type=none,重新启动问题解决 注:因为项目未使用redis管理session,可以如上设置,如果想使用 ...