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. asp.net 动态添加多个用户控件

    动态添加多个相同用户控件,并使每个用户控件获取不同的内容. 用户控件代码: 代码WebControls using System; using System.Collections.Generic;  ...

  2. 1-1、superset开发环境搭建

    在对superset进行二次开发的过程中,往往需要搭建本地开发环境,修改后立即看到效果,下面我们就讲下开发环境的搭建. 1.打开PyCharm,在菜单栏上执行VCS-->Checkout fro ...

  3. linux命令之rpm

    1.查询一个包是否被安装的命令rpm -q  < rpm package name> 2.列出所有被安装的rpm package 命令rpm -qa

  4. 第一个内核模块hello world

    1.源码树的下载和编译(只是研究内核模块的话,应该不需要源码树的) 下载很简单,压缩包解压 编译:make menuconfig make bzImage -j4 参考 2. cd  /usr/src ...

  5. uva 110 Meta-Loopless Sorts 用程序写程序 有点复杂的回溯水题

    题目要求写一个直接用比较排序的pascal程序,挺有趣的一题. 我看题目数据范围就到8,本来以为贪个小便宜,用switch输出. 然后发现比较次数是阶乘级别的,8的阶乘也是挺大的,恐怕会交不上去. 于 ...

  6. ios 如何改变UISegmentedControl文本的字体大小?

    UIFont *Boldfont = [UIFont boldSystemFontOfSize:16.0f]; NSDictionary *attributes = [NSDictionary dic ...

  7. android studio升级时提示 Connection failed. Please check your network connection and try again

    原文地址 http://www.eyeapk.com/android-studio-update.html Mac OSX中修改文件路径为 bin/idea.vmoptions ,添加如下内容,如果无 ...

  8. 利用jsPerf优化Web应用的性能

    在前端开发的过程中,掌握好浏览器的特性进行有针对性的性能调优是一项基本工作,jsperf.com是一个用来发布基于HTML的针对性能比较的测试用例的网站,你可以在jsPerf上在线填写和运行测试用例, ...

  9. http请求及json发送与解析 post string

    golang http请求及json流解析 - 长风v持成的博客 - CSDN博客 https://blog.csdn.net/u011677067/article/details/80852158 ...

  10. HDFS基本工作机制