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。不过需要记录每个状态是否被访问过。

代码:

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define inf 0x3fffffff
#define esp 1e-10
using namespace std;
struct node1
{
int dice[];
int val;
};
struct node
{
node1 qt;
int step;
};
node a;
node1 b;
int bfs()
{
set < int > s;
s.insert(a.qt.val);
queue < node > q;
q.push(a);
while (!q.empty())
{
node f, k;
f = q.front();
q.pop();
if (f.qt.val == b.val) return f.step;
//first
k = f;
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
k.qt.val = k.qt.dice[];
for (int y = ; y < ; ++y)
{
k.qt.val = *k.qt.val + k.qt.dice[y];
}
if (s.find(k.qt.val) == s.end())
{
k.step ++;
q.push(k);
s.insert(k.qt.val);
k.step --;
}
//second
k = f;
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
k.qt.val = k.qt.dice[];
for (int y = ; y < ; ++y)
{
k.qt.val = *k.qt.val + k.qt.dice[y];
}
if (s.find(k.qt.val) == s.end())
{
k.step ++;
q.push(k);
s.insert(k.qt.val);
k.step --;
}
//third
k = f;
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
k.qt.val = k.qt.dice[];
for (int y = ; y < ; ++y)
{
k.qt.val = *k.qt.val + k.qt.dice[y];
}
if (s.find(k.qt.val) == s.end())
{
k.step ++;
q.push(k);
s.insert(k.qt.val);
k.step --;
}
//forth
k = f;
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
k.qt.val = k.qt.dice[];
for (int y = ; y < ; ++y)
{
k.qt.val = *k.qt.val + k.qt.dice[y];
}
if (s.find(k.qt.val) == s.end())
{
k.step ++;
q.push(k);
s.insert(k.qt.val);
k.step --;
}
}
return -;
}
int main()
{
//freopen ("test.txt", "r", stdin);
while (scanf ("%d", &a.qt.dice[]) != EOF)
{
for (int i = ; i < ; ++i)
scanf ("%d", &a.qt.dice[i]);
a.step = ;
a.qt.val = a.qt.dice[];
for (int y = ; y < ; ++y)
{
a.qt.val = *a.qt.val + a.qt.dice[y];
}
for (int i = ; i < ; ++i)
scanf ("%d", &b.dice[i]);
b.val = b.dice[];
for (int y = ; y < ; ++y)
{
b.val = *b.val + b.dice[y];
}
printf ("%d\n", bfs());
}
return ;
}

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. HDU-4031-Attack(树状数组)

    Problem Description Today is the 10th Annual of "September 11 attacks", the Al Qaeda is ab ...

  2. TextView实现打印机效果 ,字符串逐字显示

    https://github.com/lygttpod/AndroidCustomView/blob/master/app/src/main/java/com/allen/androidcustomv ...

  3. 多语言中的“默认语言”设置

    最近在搞一个多语言的东西,打算如果用户是中文环境就显示中文,其他任何非中文环境就显示英文.换句话说,把默认语言设置成英文. 不过因为VS是中文的,发现即使默认资源文件是英文(AppResource.r ...

  4. oschina git服务, 如何生成并部署ssh key

    1.如何生成ssh公钥 你可以按如下命令来生成 sshkey: ssh-keygen -t rsa -C "xxxxx@xxxxx.com" # Generating public ...

  5. 使用oracle10g官方文档找到监听文件(listener.ora)的模板

    ***********************************************声明*************************************************** ...

  6. Tensorflow官方文档中文版——第二章(瞎奖杯写)

    包含如下几个部分: 1.面向机器学习初学者的 MNIST 初级教程 2.面向机器学习专家的 MNIST 高级教程 3.TensorFlow 使用指南 4.卷积神经网络 5.单词的向量表示(word e ...

  7. [转载]设计模式的UML图

    1.抽象工厂(Abstract Factory)模式 意图:为特定的客户(或情况)提供特定系列的对象. 2.类的适配器(Adapter)模式 意图:将一个类的接口转换成客户希望的另外一个接口. 3.对 ...

  8. python 学习2:生成器,迭代器,装饰器

    1.生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万  个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那 ...

  9. ubuntu vim退出时出错

    E505: "vimrc" is read-only (add ! to override) wq退出时加!强制保存退出 "vimrc" E212: Can't ...

  10. Swift协议+代理

    Swift语言开发中使用协议+代理的用法和oc中是一样的,只不过变得是语法.现在就进入swift的协议+代理. 先上个图,看看我们要实现的效果:  首先是第一个页面,然后点击到第二个页面,最后点击返回 ...