HDU 5012 Dice (搜索)

HDU5012 求最小步数,暴力bfs就可以了。 范围小,直接map进行映射。 随便搞啊

Dice

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 444 Accepted Submission(s): 263

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

Source

2014 ACM/ICPC Asia Regional Xi’an Online

/* ***
Author :kuangbin
Created Time :2014/9/15 21:27:02
File Name :E:\2014ACM\2014网络赛\2014西安\HDU5012.cpp
************************************************ */

#include <stdio.h>

#include <string.h>

#include

#include

#include

#include

#include

#include

#include

#include <math.h>

#include <stdlib.h>

#include <time.h>
using namespace std;

string left(string ss){
string ret;
ret.resize(6);
int a[] = {3,2,0,1,4,5};
for(int i = 0;i < 6;i++)
ret[i] = ss[a[i]];
return ret;
}
string right(string ss){
string ret;
ret.resize(6);
int a[] = {2,3,1,0,4,5};
for(int i = 0;i < 6;i++)
ret[i] = ss[a[i]];
return ret;
}
string front(string ss){
string ret;
ret.resize(6);
int a[] = {5,4,2,3,0,1};
for(int i = 0;i < 6;i++)
ret[i] = ss[a[i]];
return ret;
}
string back(string ss){
string ret;
ret.resize(6);
int a[] = {4,5,2,3,1,0};
for(int i = 0;i < 6;i++)
ret[i] = ss[a[i]];
return ret;
}
int solve(string start,string end){
map<string,int>mp;
mp[start] = 0;
queueq;
q.push(start);
while(!q.empty()){
string tmp = q.front();
q.pop();
if(tmp == end)return mp[tmp];
string nn;
nn = left(tmp);
if(mp.find(nn) == mp.end()){
mp[nn] = mp[tmp] + 1;
q.push(nn);
}
nn = right(tmp);
if(mp.find(nn) == mp.end()){
mp[nn] = mp[tmp] + 1;
q.push(nn);
}
nn = front(tmp);
if(mp.find(nn) == mp.end()){
mp[nn] = mp[tmp] + 1;
q.push(nn);
}
nn = back(tmp);
if(mp.find(nn) == mp.end()){
mp[nn] = mp[tmp] + 1;
q.push(nn);
}
}
return -1;
}

int main()
{
//freopen(“in.txt”,”r”,stdin);
//freopen(“out.txt”,”w”,stdout);
int a;
while(scanf(“%d”,&a) == 1){
string str1 = “”;
str1 += (char)(a+’0’);
for(int i = 1;i < 6;i++)
{
scanf(“%d”,&a);
str1 += (char)(a+’0’);
}
string str2 = “”;
for(int i = 0;i < 6;i++){
scanf(“%d”,&a);
str2 += (char)(a+’0’);
}
printf(“%d\n”,solve(str1,str2));
}
return 0;
}

------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 393.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%