Dice

Time Limit:1000MS     Memory Limit:65536KB

Description

There are 2 special dices on the table. On each face of the dice, a distinct number was written. Consider a 1.a 2,a 3,a 4,a 5,a 6 to be numbers written on top face, bottom face, left face, right face, front face and back face of dice A. Similarly, consider b 1.b 2,b 3,b 4,b 5,b 6 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 a i ≠ a j and b i ≠ b j 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, a i ≠ b i). Ddy wants to make the two dices look the same from all directions(which means for all i, a i = b i) 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 a 1,a 2,a 3,a 4,a 5,a 6, representing the numbers on dice A.

The second line consists of six integers b 1,b 2,b 3,b 4,b 5,b 6, 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

题解:

  1. 正确表示正方体,用a1-a6表示。注意a1-a6分别表示的是上下左右前后六个面,顺序不能换。

  2. 使用map记录步数,以及查看该状态是否已经尝试过。

  3. 广度优先搜索,求得的结果即是最少的步数。

  4. ​注意翻转后的数组不要写错。

以下是代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <sstream>
#include <queue>
#include <stack>
#include <stack>
#include <map>
using namespace std; #define F(i,s,e) for(int i = s;i<e;i++)
#define ss(x) scanf("%d",&x)
#define write() freopen("1.in","r",stdin)
#define W(x) while(x) string s1,s2;
string change(string str,int k){//1 l 2 r 3 f 4 b
string s="";
int a[6]={0,1,2,3,4,5};
switch(k){//实现四种翻转
case 1:a[0]=2;a[1]=3;a[2]=1;a[3]=0;break;
case 2:a[0]=3;a[1]=2;a[2]=0;a[3]=1;break;
case 3:a[0]=4;a[1]=5;a[4]=1;a[5]=0;break;
case 4:a[0]=5;a[1]=4;a[4]=0;a[5]=1;break;
}
F(i,0,6)s+=str[a[i]];
return s;
}
int bfs(){
map<string ,int > ma;//记录步数
queue<string>q;
string t1,t2;
ma[s1]=0;
q.push(s1);
W(!q.empty()){
t1 = q.front();
q.pop();
if(t1 == s2)return ma[t1];
F(i,1,5){
t2 = change(t1,i);
if(ma.find(t2)==ma.end()){//广度优先
q.push(t2);
ma[t2]=ma[t1]+1;
}
}
}
return -1;
} int main(){
//write();
int t;
W(ss(t)!=EOF){
s1=s2="";
s1+=t+'0';
F(i,0,5){
ss(t);
s1+=t+'0';
}
F(i,0,6){
ss(t);
s2+=t+'0';
}
cout << bfs()<<endl;
}
}

  

 
 

Spring-1-F Dice(HDU 5012)解题报告及测试数据的更多相关文章

  1. BestCoder18 1002.Math Problem(hdu 5105) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105 题目意思:给出一个6个实数:a, b, c, d, l, r.通过在[l, r]中取数 x,使得 ...

  2. BestCoder17 1002.Select(hdu 5101) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有 ...

  3. BestCoder12 1002.Help him(hdu 5059) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5059 题目意思:就是输入一行不多于 100 的字符串(除了'\n' 和 '\r' 的任意字符),问是否 ...

  4. BestCoder10 1001 Revenge of Fibonacci(hdu 5018) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A ...

  5. BestCoder7 1002 Little Pony and Alohomora Part I(hdu 4986) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4986 题目意思:有 n 个box(从左到右编号依次为1~n),每个box里面有一个随机的钥匙,有可能这 ...

  6. BestCoder8 1001.Summary(hdu 4989) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4989 题目意思:给出 n 个数,然后将这些数两两相加,得到 n*(n-1) /2 对和,把重复的和去掉 ...

  7. BestCoder24 1001.Sum Sum Sum(hdu 5150) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5150 题目意思:就是直接求素数. 不过 n = 1,也属于答案范围!!只能说,一失足成千古恨啊---- ...

  8. BestCoder22 1002.NPY and arithmetic progression(hdu 5143) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 题目意思:给出 1, 2, 3, 4 的数量,分别为a1, a2, a3, a4,问是否在每个数 ...

  9. BestCoder20 1002.lines (hdu 5124) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5124 题目意思:给出 n 条线段,每条线段用两个整数描述,对于第 i 条线段:xi,yi 表示该条线段 ...

随机推荐

  1. ios 从URL中截取所包含的参数,并且以字典的形式返回和参数字典转URL

    //字典转链接(参数) - (NSString *)keyValueStringWithDict:(NSDictionary *)dict { if (dict == nil) { return ni ...

  2. tinker

    Ios前一段时间因为热更新被强制下架也算是最大闻了,但Android没关系,继续玩 首先tinker比Andfix好多了,版本现在都到1.7.11了,Andfix不支持yunos, 现在项目中没有用到 ...

  3. 当苹果因为UIDevice、udid、uniqueIdentifier而把我们的应用拒之门外invalid binary的时候,呕心沥血解决方法啊

    本文转载至 http://blog.csdn.net/macmini/article/details/16341669 当我们辛辛苦苦把应用或者游戏做好的时候,满怀激动地把应用提交上去给苹果大大,谁知 ...

  4. SQL ALTER TABLE 命令

    SQL ALTER TABLE 命令 SQL ALTER TABLE 命令用于添加.删除或者更改现有数据表中的列. 你还可以用 ALTER TABLE 命令来添加或者删除现有数据表上的约束. 语法: ...

  5. LA5059 Playing With Stones

    题意:nim游戏.加上限制每次不得取走超过当前堆一半的石子 1 ≤ N ≤ 100,1 ≤ ai ≤ 2 ∗ 1018 分析:由于ai过大.所以我们采用SG函数递推找规律. (详见代码) #inclu ...

  6. LeetCode 笔记系列16.2 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  7. 制作item和category的mvc视图总结

    View层index.phg 代码: <?php use yii\helpers\Html; use yii\grid\GridView; use yii\widgets\Pjax; use f ...

  8. Zabbix使用SMTP发送邮件报警并且制定报警内容

    接上篇Zabbix监控介绍及安装配置 选择报警项 创建一个报警项 选择到刚刚自定义的80端口 定义报警方法 定义告警级别 一些报警方法 diff 比较是否有修改 last 最低值 nodata 没有数 ...

  9. CH5105 Cookies【贪心】【线性dp】

    5105 Cookies 0x50「动态规划」例题 描述 圣诞老人共有M个饼干,准备全部分给N个孩子.每个孩子有一个贪婪度,第 i 个孩子的贪婪度为 g[i].如果有 a[i] 个孩子拿到的饼干数比第 ...

  10. 剑指Offer——数组中出现次数超过一半的数字

    题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2 ...