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. sklearn算法库的顶层设计

    sklearn监督学习的各个模块 neighbors近邻算法,svm支持向量机,kernal_ridge核岭回归,discriminant_analysis判别分析,linear_model广义线性模 ...

  2. numpy常用举例

    转自https://morvanzhou.github.io/tutorials/data-manipulation/np-pd/2-1-np-attributes/ numpy 的属性: ndim: ...

  3. Elasticsearch学习之多种查询方式

    1. query string search 搜索全部商品:GET /ecommerce/product/_search took:耗费了几毫秒 timed_out:是否超时,这里是没有 _shard ...

  4. Apache Server Status详解

    Apache的日志如果靠分析日志或者查看服务器进程来监视Apache运行状态的话,比较繁冗.不过在Apache 1.3.2及以后的版本中就自带一个查看Apache状态的功能模块server-statu ...

  5. 为Docker容器中运行的gitlab添加ssh的一些问题记录

    最近做的一个东西,是将gitlab10.x的汉化版本,从源码编译(在源码中自己定制一些东西),然后制作成Docker镜像,作为Docker容器来运行 在启用容器中的gitlab的ssh的时候,遇到了一 ...

  6. 【BZOJ4361】isn 动态规划+树状数组+容斥

    [BZOJ4361]isn Description 给出一个长度为n的序列A(A1,A2...AN).如果序列A不是非降的,你必须从中删去一个数, 这一操作,直到A非降为止.求有多少种不同的操作方案, ...

  7. 美团开源 SQL 优化工具 SQLAdvisor

    https://www.oschina.net/news/82725/sqladvisor-opensource https://github.com/Meituan-Dianping/SQLAdvi ...

  8. RabbitMQ 安装和说明

    一.安装 1. 下载源码,RabbitMQ是使用Erlang开发,所以安装RabbitMQ前需要先安装Erlang.官方推荐从源码安装Erlang,因此下面开始从源码安装OTP 17.0.下载OTP ...

  9. ELKStack生产案例

    需求分析: 访问日志:apache访问日志,nginx访问日志,tomcat file 错误日志: error log,java日志 直接收 java异常需要处理 系统日志:/var/log/*   ...

  10. ASP.NET MVC中ViewBag和ViewData的区别

    在MVC3.0以上我们会用到ViewBag或者ViewData进行页面传值,对比一下二者的差距: ViewData ViewBag 基于key/value的字典集合 dynamic类型对象 从ASP. ...