ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)
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
这个题目可以用bfs遍历向前、向后、向左、向右转 ,这样如果用一个数组a[6]记录一种状态,那么最多也只有6!种状态,数量不是很多,可以直接暴力bfs。不过需要记录每个状态是否被访问过。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define inf 0x3fffffff
#define esp 1e-10
using namespace std;
struct node1
{
int dice[];
int val;
};
struct node
{
node1 qt;
int step;
};
node a;
node1 b;
int bfs()
{
set < int > s;
s.insert(a.qt.val);
queue < node > q;
q.push(a);
while (!q.empty())
{
node f, k;
f = q.front();
q.pop();
if (f.qt.val == b.val) return f.step;
//first
k = f;
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
k.qt.val = k.qt.dice[];
for (int y = ; y < ; ++y)
{
k.qt.val = *k.qt.val + k.qt.dice[y];
}
if (s.find(k.qt.val) == s.end())
{
k.step ++;
q.push(k);
s.insert(k.qt.val);
k.step --;
}
//second
k = f;
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
k.qt.val = k.qt.dice[];
for (int y = ; y < ; ++y)
{
k.qt.val = *k.qt.val + k.qt.dice[y];
}
if (s.find(k.qt.val) == s.end())
{
k.step ++;
q.push(k);
s.insert(k.qt.val);
k.step --;
}
//third
k = f;
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
k.qt.val = k.qt.dice[];
for (int y = ; y < ; ++y)
{
k.qt.val = *k.qt.val + k.qt.dice[y];
}
if (s.find(k.qt.val) == s.end())
{
k.step ++;
q.push(k);
s.insert(k.qt.val);
k.step --;
}
//forth
k = f;
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
swap (k.qt.dice[], k.qt.dice[]);
k.qt.val = k.qt.dice[];
for (int y = ; y < ; ++y)
{
k.qt.val = *k.qt.val + k.qt.dice[y];
}
if (s.find(k.qt.val) == s.end())
{
k.step ++;
q.push(k);
s.insert(k.qt.val);
k.step --;
}
}
return -;
}
int main()
{
//freopen ("test.txt", "r", stdin);
while (scanf ("%d", &a.qt.dice[]) != EOF)
{
for (int i = ; i < ; ++i)
scanf ("%d", &a.qt.dice[i]);
a.step = ;
a.qt.val = a.qt.dice[];
for (int y = ; y < ; ++y)
{
a.qt.val = *a.qt.val + a.qt.dice[y];
}
for (int i = ; i < ; ++i)
scanf ("%d", &b.dice[i]);
b.val = b.dice[];
for (int y = ; y < ; ++y)
{
b.val = *b.val + b.dice[y];
}
printf ("%d\n", bfs());
}
return ;
}
ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)的更多相关文章
- ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)
Description There is a special number sequence which has n+1 integers. For each number in sequence, ...
- ACM学习历程—HDU 5512 Pagodas(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...
- ACM学习历程—HDU 3915 Game(Nim博弈 && xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3915 题目大意是给了n个堆,然后去掉一些堆,使得先手变成必败局势. 首先这是个Nim博弈,必败局势是所 ...
- ACM学习历程—HDU 5536 Chip Factory(xor && 字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5536 题目大意是给了一个序列,求(si+sj)^sk的最大值. 首先n有1000,暴力理论上是不行的. ...
- ACM学习历程—HDU 5534 Partial Tree(动态规划)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x ...
- ACM学习历程—HDU 3949 XOR(xor高斯消元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3949 题目大意是给n个数,然后随便取几个数求xor和,求第k小的.(重复不计算) 首先想把所有xor的 ...
- ACM学习历程—HDU 5317 RGCDQ (数论)
Problem Description Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more an ...
- ACM学习历程—HDU 2112 HDU Today(map && spfa && 优先队列)
Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候,XHD夫妇也退居了二线 ...
- ACM学习历程—HDU 5073 Galaxy(数学)
Description Good news for us: to release the financial pressure, the government started selling gala ...
随机推荐
- python 上传文件下载图片
python 2.7 poster-0.8.1 requests-2.7.0 #coding:utf-8import urllibimport urllib2import sysimport time ...
- Myecplise Tomcat 启动很慢
今天突然遇到一个问题,tomcat在Myecplse启动非常慢,直接用tomcat自带的start.bat启动很快,如果通过Myeclipse启动会发现项目一直在实例化,最后发现是因为加了断点调试,断 ...
- MFC添加菜单资源与菜单执行函数的两种命令形式
添加资源->新建一个菜单资源->选择相应的对话框 菜单的执行函数命令形式: COMMAD 是指点击菜单后的执行命令 UPDATE_COMMAND_UI 是指点击菜单后菜单状态的函数
- 小程序获取openId
1.小程序获取微信openId wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId / ...
- data standardization
import random import numpy as np l, num, gen_min_, gen_max_ = [], 100, 1, 200 l = [random.randint(ge ...
- CXF生成client注意事项
1. 在使用wsdl2java命令生成client文件时在Service的Java文件中面出现super构造错误,这是因为jax-ws2.2规约与java6冲突 故须要减少jax-ws规约版本号. ...
- 解决oracle锁表
1.查看被锁住的表select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects ...
- 3.08课·········switch case及if else嵌套(日期格式)
switch case switch (n) { : break; : break; . . . case n: break; } 1.switch case必须与break一同使用,每一个case后 ...
- 深入浅出聊聊企业级API网关
http://architect.dataguru.cn/article-11431-1.html API Gateway(API GW / API 网关),顾名思义,是出现在系统边界上的一个面向 A ...
- c的详细学习(7)指针学习(一)
指针是c语言的一个重要概念,指针类型是c语言最有特色的数据类型: *利用指针编写的程序可使调用函数共享变量或数据结构,实现双向数据通信: *可以实现内存空间的动态存储分配:可以提高程序的编译效率和执行 ...