http://acm.hdu.edu.cn/showproblem.php?pid=5012

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
题目意思:
一个正方形,每个面有一个数字而且不相同(最多为6最小为1),正方形可以向左、右、前、后转,给出正方形两个状态,问最少转多少次可以从前面的状态转换成后面的状态,若不能输出-1。
思路:
正方形最多有6!种状态,由前面状态推到最后的状态,很明显bfs。
题目解析:因为是网络赛没看题就觉得是几何题,觉得做不了,之后看交的多了,就看了一下题,没想到是bfs模板水题。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
using namespace std;
int v[][][][][][];
struct node
{
int ff[];
int ans;
};
struct node t,f;
int a[],b[];
int bfs()
{
queue<node>q;
for(int i=; i<=; i++)
t.ff[i]=a[i];
t.ans=;
q.push(t);
v[t.ff[]][t.ff[]][t.ff[]][t.ff[]][t.ff[]][t.ff[]]=;
while(!q.empty())
{
t=q.front();
q.pop();
if(t.ff[]==b[]&&t.ff[]==b[]&&t.ff[]==b[]&&t.ff[]==b[]&&t.ff[]==b[]&&t.ff[]==b[])
{
printf("%d\n",t.ans);
return ;
}
for(int i=; i<=; i++)
{
if(i==)
{
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
if(v[f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]]==)
{
f.ans=t.ans+;
v[f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]]=;
q.push(f);
}
}
else if(i==)
{
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
if(v[f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]]==)
{
f.ans=t.ans+;
v[f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]]=;
q.push(f);
}
}
else if(i==)
{
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
if(v[f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]]==)
{
f.ans=t.ans+;
v[f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]]=;
q.push(f);
}
}
else if(i==)
{
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
f.ff[]=t.ff[];
if(v[f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]]==)
{
f.ans=t.ans+;
v[f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]][f.ff[]]=;
q.push(f);
}
}
}
}
return ;
}
int main()
{
int F;
while(scanf("%d%d%d%d%d%d",&a[],&a[],&a[],&a[],&a[],&a[])!=EOF)
{
for(int i=; i<=; i++)
scanf("%d",&b[i]);
memset(v,,sizeof(v));
F=bfs();
if(F==) printf("-1\n");
}
return ;
}
 

HDU5012:Dice(bfs模板)的更多相关文章

  1. 2014 网选 5012 Dice(bfs模板)

    /* 题意:就是给定两个筛子,每个筛子上6个面,每个面的数字属于[1,6], 且互不相同! 问a筛子最少经过按照题目规定的要求转动,达到和b筛子上下左右前后的数字相同! 思路:很直白的bfs,将每一种 ...

  2. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  3. BFS (1)算法模板 看是否需要分层 (2)拓扑排序——检测编译时的循环依赖 制定有依赖关系的任务的执行顺序 djkstra无非是将bfs模板中的deque修改为heapq

    BFS模板,记住这5个: (1)针对树的BFS 1.1 无需分层遍历 from collections import deque def levelOrderTree(root): if not ro ...

  4. PAT1076. Forwards on Weibo(标准bfs模板)

    //标准的层次遍历模板 //居然因为一个j写成了i,debug半天.....解题前一定要把结构和逻辑想清楚,不能着急动手,理解清楚题意,把处理流程理清楚再动手,恍恍惚惚的写出来自己慢慢debug吧 # ...

  5. BFS 模板

    转自:欣哥 下面是bfs一般的形式,谈不上模板但一般都这么来做有点乱有什么多交流 bfs一般用于求最短时间 #include<stdio.h>#include<queue>us ...

  6. 图的遍历——DFS和BFS模板(一般的图)

    关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...

  7. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  8. hdu1242 又又又是逃离迷宫(bfs模板题)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...

  9. HRBUST 1181 移动 bfs模板

    #include<bits/stdc++.h>///该头文件为万能头文件,有些学校oj不能使用,读者可根据需要自行修改 using namespace std; ; int vis[MAX ...

随机推荐

  1. FastCGI在nginx中的参数

    FastCGI参数 fastcgi主要用于http调用外部解释器的接口,采用c/s结构,可以将http服务器和脚本解析器分开,同时在脚本解析服务器上启动一个或者多个脚本解析守护进程.当HTTP服务器每 ...

  2. PowerDesigner 同步Name到Comment 及 同步 Comment 到Name

    PowerDesigner中使用方法为:     PowerDesigner->Tools->Execute Commands->Edit/Run Scripts 代码一:将Name ...

  3. java(2) 面向对象

    1.类的封装 *在定义一个类时,将类中的属性私有化,即使用prviate关键字来修饰,私有属性只能在它所在的类中被访问.为了能让外界访问私有属性,需要提供一些使用public修饰的公有方法,其中包括用 ...

  4. VMware ESXI5.5 Memories limits resolved soluation.

    在使用VMware ESXI5.5 的时候提示内存限制了,在网上找的了解决方案: 如下文: 1. Boot from VMware ESXi 5.5; 2. wait "Welcome to ...

  5. Shell语法整理,持续维护

    Shell shell条件表达式: CONDITIONAL EXPRESSIONSConditional expressions are used by the [[ compound command ...

  6. 2015.7.8js-05(简单日历)

    今天做一个简单的小日历,12个月份,鼠标移动其中一个月份时添加高亮并显示本月的活动.其实同理与选项卡致.不过是内容存在js里 window.onload = function(){ var oMain ...

  7. MacOS 安装 nginx

    brew install nginx 开机启动 $ sudo cp `brew --prefix nginx`/homebrew.mxcl.nginx.plist /Library/LaunchDae ...

  8. 23种设计模式之建造者模式(Builder)

    建造者模式强调将一个复杂对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示.建造者模式是一步一步地创建一个复杂的对象,它允许用户只通过制定复杂对象的类型和内容就可以构建它们,用户不需要知道 ...

  9. web自动化时,sendkeys输入长文本时浏览器响应慢或错误时处理

    在做某个测试时,要在文本框中输入大量的文本,文件内容如下: "-----BEGIN CERTIFICATE-----\nMIIBozCCAQwCAQEwDQYJKoZIhvcNAQEFBQA ...

  10. 在eclipse中编辑linux上的项目

    以前在linux的上接口自动化项目都是使用notepad++或SVN下载到本地后再上传来完成功做,但在调试时非常麻烦. 查看了下在eclipse中有一个非常好用的插件Remote Systems,可以 ...