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. 美秒快报 移动端API接口后台制作总结

    1.创建方法时,不要用index这类的不易显示该方法功能的单词,尽量使用功能的缩写 例如: public function xssc(){} 2.尽量少用Request方法,多用input助手方法获取 ...

  2. 【.netcore基础】MVC制器Controller依赖注入

    废话少说,直接上代码 首先我们得有一个接口类和一个实现类,方便后面注入MVC里 接口类 public interface IWeatherProvider { List<WeatherForec ...

  3. 【MD5加密】MD5加密编码的坑

    MD5 MD5即Message-Digest Algorithm (信息-摘要算法5),用于确保信息传输完整一致. 是计算机广泛使用的杂凑算法之一(又译摘要算法.哈希算法),主流编程语言普遍已有MD5 ...

  4. JS插件---->SyntaxHighlighter的使用

    SyntaxHighlighter是一款用于web页面的代码着色工具,可以用来着色多种语言.今天我们通过实例来学习一下它的用法.旧同桌不是老情人,但与你分享过的青春不比初恋少半分. SyntaxHig ...

  5. eagle学习汇总

    一.原理图编辑器 1. 编辑->全局属性->可以设置全局变量,选择“文本框”,以‘>’开头代表引用全局属性的值. 2. 绘制->Frame->可绘制原理图边框,一般选择“ ...

  6. 在winform上内嵌入其它的程序

    这段代码很有意义,用于把一个程序的界面嵌入到我们自己程序的某个指定窗体上. 比如在某个项目里,我需要把基恩士的激光扫描轮廓显示给客户看,但是激光的DLL中并没有这种功能提供. 于是我想先启动激光的官方 ...

  7. 基于Python的跨平台端口转发工具

    背景 使用lcx也好,nc也好,总是会被安全防护软件查杀,所以想着自己写一个.顺面学习一下,端口转发的原理. 端口转发的逻辑 端口转发的逻辑很简单开启两个scoket,一个绑定IP端口进行listen ...

  8. oracle dblink 查询 tns:无法解析指定的连接标识符

    问题情景是这样的:我在数据库服务器(windows server 2008r2 ,64bit)oracle(11gr2,64bit)中通过dblink连接到另外一台服务器(hp-ux)的oracle( ...

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

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

  10. html如何引用另一个html的内容

    第一种:使用jquery: https://api.jquery.com/load/ 这可以加载html中某个id的元素 $( "#result" ).load( "aj ...