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. 石子合并DP

    DP Time Limit:3000MS     Memory Limit:131072KB     64bit IO Format:%lld & %llu Submit Status Pra ...

  2. jQuery.callbacks 注释

    (function( jQuery ) { // String to Object flags format cache var flagsCache = {}; // Convert String- ...

  3. org.apache.poi3.1.7 Excle并发批量导入导出

    org.apache.poi3.1.7 升级,需要修改设置方式: 1.org.apache.poi3.1.4 的设置单元格: XSSFCellStyle cellStyle = wb.createCe ...

  4. FlashFXP上传下载

    正常情况我们的生产环境我们本地是不能直接访问的,因为网段不通,且生产环境不允许我们随便访问,但是对于我们自运维的集群我们有时候需要上去做一些操作,通过堡垒机跳转到生产机器上即可,但是我们不能通过xsh ...

  5. 使用JSTL在页面前的空行怎么去除?

    解决的方法是:在每个JSP的头上加上一段代码   <%@ page trimDirectiveWhitespaces="true" %>

  6. JSP页面EL表达式不解析

    问题是这样:在搭建springMVC环境的时候,笔者写了一个简单的Controller如下: @Controller public class HelloController { @RequestMa ...

  7. 《程序员代码面试指南》第二章 链表问题 将单链表每K个节点之间逆序

    样例 链表1-2-3-4-5-6-7-8-9-10 K=3 ,结果 3-2-1-6-5-4-9-8-7-10 java代码 /** * @Description:将单链表每K个节点之间逆序 * @Au ...

  8. web前端框架之自定义form表单验证

    自定义form验证初试 .在后端创建一个类MainForm,并且在类中自定义host ip port phone等,然后写入方法,在post方法中创建MainForm对象,并且把post方法中的sel ...

  9. maven pom filter 导致的问题记录

    Maven提供了一个很不错的功能 Resource Filter, 可以将按不同环境的进行变量赋值, 比如数据库链接, redis, 日志输出位置等等.. 具体的filter如何使用我这里不做介绍, ...

  10. Linux课程---1、VMWare安装CentOS虚拟机(安装重要注意)

    Linux课程---1.VMWare安装CentOS虚拟机(安装重要注意) 一.总结 一句话总结: 可以先去百度搜一篇对应系统的安装教程:比如 CentOS 7 安装 1.安装VMWare之后,打开w ...