HDU--Dice--隐式图搜索

Dice

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


 

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

 

 

Recommend

hujie

 

求a翻转到b所用的最小步数。

广度优先搜索,以前是四个方向走,现在是四个位置反转,找好对应的位置标号,最上面的是a0.....。

把a数组每次映射为一个数X,判断X是否走过这一步,然后把X转化为数组判断是否和b数组相同。

#include <algorithm>    //STL通用算法
#include <bitset>     //STL位集容器
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>     //复数类
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>      //STL双端队列容器
#include <exception>    //异常处理类
#include <fstream>
#include <functional>   //STL定义运算函数(代替运算符)
#include <limits>
#include <list>      //STL线性列表容器
#include <map>       //STL 映射容器
#include <iomanip>
#include <ios>      //基本输入/输出支持
#include<iosfwd>     //输入/输出系统使用的前置声明
#include <iostream>
#include <istream>     //基本输入流
#include <ostream>     //基本输出流
#include <queue>      //STL队列容器
#include <set>       //STL 集合容器
#include <sstream>    //基于字符串的流
#include <stack>      //STL堆栈容器    
#include <stdexcept>    //标准异常类
#include <streambuf>   //底层输入/输出支持
#include <string>     //字符串类
#include <utility>     //STL通用模板类
#include <vector>     //STL动态数组容器
#include <cwchar>
#include <cwctype>
#define ll long long
using namespace std;
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
//priority_queue<int,vector<int>,less<int> >q;
int dx[]= {-1,1,0,0,-1,-1,1,1};
int dy[]= {0,0,-1,1,-1,1,1,-1};
const int maxn = 50000+6;
const ll mod=1e9+7;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
const int INF=99999999;
int a[10];
int b[10];
int c[10];
int mmp[][6]=
{
    {2,3,1,0,4,5},//向右反转
    {3,2,0,1,4,5},//向左反转
    {5,4,2,3,0,1},//向前反转
    {4,5,2,3,1,0}//向后反转
};
int getIndex(int *a)
{
    int ans=0;
    dep(i,5,0)
    {
        ans=ans*6+a[i];
    }
    return ans;
}
void getArr(int x,int *a)
{
    rep(i,0,5)
    {
        a[i]=x%6;
        x/=6;
    }
}
bool check()
{
    rep(i,0,5)
    {
        if(a[i]!=b[i])
            return false;
    }
    return true;
}
int dis[maxn];
int vis[maxn];
int solve()
{
    queue<int>q;
    while(q.size())
        q.pop();
    memset(vis,0,sizeof(vis));
    int id=getIndex(a);
    q.push(id);
    dis[id]=0;
    vis[id]=1;
    while(q.size())
    {
        int x=q.front();
        q.pop();
        getArr(x,a);
        if(check())
        {
            return dis[x];
        }
        rep(i,0,3)
        {
            rep(j,0,5)
            {
                c[j]=a[mmp[i][j]];
            }
            int y=getIndex(c);
            if(vis[y])
                continue;
            vis[y]=1;
            dis[y]=dis[x]+1;
            q.push(y);
        }
    }
    return -1;
}
int main()
{
    while(scanf("%d",&a[0])!=EOF)
    {
        rep(i,1,5)
        {
            scanf("%d",&a[i]);
        }
        rep(i,0,5)
        {
            scanf("%d",&b[i]);
            a[i]--;
            b[i]--;
        }
        printf("%d\n",solve());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值