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. Ubuntu Mysql 安装

    下载 http://dev.mysql.com/downloads/mysql/ 选择 Linux- Generic 选择版本 wget http://cdn.mysql.com/Downloads/ ...

  2. 指针*pbuffer和getchar 读取字符串

    在C语言入门教材里看到这一段代码,没看懂是什么意思.char buffer[10];char *pbuffer = buffer;while( (*pbuffer++ = getchar() )!= ...

  3. C++引用具体解释

    引用是C++中新出现的.有别于C语言的语法元素之中的一个. 关于引用的说明,网络上也有不少.可是总感觉云遮雾绕,让人印象不深刻. 今天我就来深入解释一下引用.并就一些常见的观点进行说明,最后附带代码演 ...

  4. java基础---->hashSet的简单分析(一)

    对于HashSet而言,它是基于HashMap实现的,底层采用HashMap来保存元素的.今天我们就简单的分析一下它的实现.人生,总会有不期而遇的温暖,和生生不息的希望. HashSet的简单分析 一 ...

  5. 【BZOJ4636】蒟蒻的数列 STL

    [BZOJ4636]蒟蒻的数列 Description 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个数列,初始值均为0,他进行N次操作,每次将数列[a,b)这个 ...

  6. js 连等赋值 分析

    JavaScript权威指南-第6版 4.11 赋值表达式 提到了连等赋值的情况,但是解释的不够详细,所以在此总结下: 首先看书上最重要的一句话: 这句话总结下就是: A = B ; // 整个表达式 ...

  7. Maven 构建Spring-Boot工程报错

    Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:repackage ...

  8. java面向对象、构造方法 之内部类

    UML类图 UML  Unified Modeling Language (UML)又称统一建模语言或标准建模语言 在一个类中      属性表示这类事物的性质(描述它的数据)      方法表示这类 ...

  9. MySQL备份1356错误提示修复办法

    mysqldump备份出现错误提示 mysqldump: Couldn't execute 'SHOW FIELDS FROM `view_videos`': View 'hekegame_video ...

  10. If a cache file exists, it is sent directly to the browser, bypassing the normal system execution.

    w开启缓存,缓存视图,用于后续请求. https://www.codeigniter.com/userguide3/overview/appflow.html http://codeigniter.o ...