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. linux 常用的17个性能指标

    1.Average load:Average number of processes simultaneously in Ready state during the last minute.   上 ...

  2. Windows环境下搭建SVN服务器

    使用 VisualSVN Server来实现主要的 SVN功能则要比使用原始的 SVN和Apache相配合来实现源代码的 SVN管理简单的多,下面就看看详细的说明. VisualSVN Server的 ...

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

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

  4. 高次同余方程模板BabyStep-GiantStep

    /************************************* ---高次同余方程模板BabyStep-GiantStep--- 输入:对于方程A^x=B(mod C),调用BabySt ...

  5. 爬虫入门【1】urllib.request库用法简介

    urlopen方法 打开指定的URL urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, ca ...

  6. 学习Sharding JDBC 从入门到出门-1

    感觉大神已经写好了,自己膜拜下下, 送上大神地址:http://www.cnblogs.com/zhongxinWang/p/4262650.html 这篇博客主要是理论的说明了什么是分库分表,路由等 ...

  7. 【基于rssi室内定位报告】rssi分布情况标识位置

    import matplotlib matplotlib.use('Agg') import numpy as np from numpy import array from matplotlib i ...

  8. phpstorm+xdebug, 实现断点调试: xdebug如何配置

    [XDebug] xdebug.profiler_output_dir="D:\phpStudy\tmp\xdebug" xdebug.trace_output_dir=" ...

  9. 保护眼睛,win7家庭版如何修改窗口的背景颜色

    win7的窗口背景色为白色,长时间使用电脑对眼睛的刺激比较大,为了保护眼睛建议改成浅灰色或者淡绿.淡黄色等,可是win7的家庭版里没有[个性化]菜单,那么我们如何修改呢? 首先在[开始]处找到[控制面 ...

  10. 【python】将excel转成json

    excel格式如下: 转换后如下 {"BD": 1375.0, "BE": 829.0, "BF": 3.0, "BG" ...