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. Python的Django框架中的Context使用

    Python的Django框架中的Context使用 近期整理些Python方面的知识,一旦你创建一个 Template 对象,你能够用 context 来传递数据给它. 一个context是一系列变 ...

  2. 如何更改CSDN博客高亮代码皮肤的样式,使博客看起来更有范(推荐)

    由于本人写博客的时候,也没有配置博客的相关属性,因此贴出来的代码块都是CSDN默认的,因此代码背景色都是白色的,如下所示: 但是本人在浏览他人博客的时候,发现有些博客的代码块看起来比较有范,整个代码库 ...

  3. 8.Django模型类例子

    这里定义4个模型 作者:一个作者有姓名 作者详情:包括性别,email,出生日期, 出版商:名称,地址,城市,省,国家,网站 书籍:名称,日期 分析: 作者详情和作者一对一的关系 一本书可以有多个作者 ...

  4. Django安装debug tool bar

    1.安装Django Debug Toolbarpip install django-debug-toolbar 2.设置项目的DEBUG属性DEBUG = True 3.INSTALLED_APPS ...

  5. python cookbook第三版学习笔记十八:可由用户修改的装饰器

    定义一个属性可由用户修改的装饰器: 在前面的介绍中使用装饰器来包装函数,这一章来介绍下如何让用户调整装饰器的属性. 首先来看下代码: from functools import wraps,parti ...

  6. SSAS(SQL Server 分析服务)、***S(SQL Server报表服务)、SSIS(SQL Server集成服务)

    一.数据仓库入门 实验手册 1. 创建数据源  http://jimshu.blog.51cto.com/3171847/13366622. 创建数据源视图 http://jimshu.blog.51 ...

  7. oracle decode 用法

    含义解释: decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译值1)ELSIF 条件=值2 THEN ...

  8. UVALive - 7427 the math 【二分匹配】

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  9. 使用MAVEN手动创建web项目

    问题:如下图,使用maven创建webapp项目时,默认使用maven-archetype-webapp这个archetype,由于这个archetype比较古老,有如下缺点: 1. 默认生成的项目会 ...

  10. finally return 执行关系 异常处理 c#

    Return.finally执行关系简述 除了函数出现system.exit(0)终止虚拟机,finally中的代码一定执行,return语句会等待finally的执行:如果是值传递,finally中 ...