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

模拟出骰子四种反转方式,bfs,最多不会走超过6步

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x)) typedef long long LL;
int a[6],b[6];
bool ard(int x)
{
int c[8],d[4];
c[0] = a[4],c[1] = a[3],c[2] = a[5],c[3] = a[2];
for(int j = 0;j < 4;++j)
c[j+4] = c[j];
if(x == 0){
d[0] = b[4],d[1] = b[3],d[2] = b[5],d[3] = b[2];
}
else if(x == 1){
d[0] = b[4],d[1] = b[2],d[2] = b[5],d[3] = b[3];
}
else if(x == 2){
d[0] = b[5],d[1] = b[1],d[2] = b[4],d[3] = b[0];
}
else if(x == 3){
d[0] = b[5],d[1] = b[0],d[2] = b[4],d[3] = b[1];
}
else if(x == 4){
d[0] = b[1],d[1] = b[3],d[2] = b[0],d[3] = b[2];
}
else if(x == 5){
d[0] = b[1],d[1] = b[2],d[2] = b[0],d[3] = b[3];
}
for(int i = 0;i < 8;++i)
if(d[0] == c[i] && i + 3 < 8 && d[1] == c[i+1] && d[2] == c[i+2] && d[3] == c[i+3])
return true;
return false;
}
bool check()
{
int top = a[0],bot = a[1];
for(int i = 0;i < 6;++i){
if(top == b[i] && bot == b[i+1] && ard(i))
return true;
if(top == b[i+1] && bot == b[i] && ard(i+1))
return true;
}
return false;
}
bool same(int d[6])
{
for(int i = 0;i < 6;++i){
if(a[i] != d[i])
return false;
}
return true;
}
int ans;
bool vis[700000];
void dfs(int r,int da[6])
{
//cout<<r<<endl;
if(r > 9)
return ;
if(same(da)){
ans = min(ans,r);
return ;
}
int f = da[0]*100000+da[1]*10000+da[2]*1000+da[3]*100+da[4]*10+da[5];
if(vis[f])
return ;
vis[f] = 1;
int c[6];
c[0] = da[3],c[1] = da[2],c[2] = da[0],c[3] = da[1],c[4] = da[4],c[5] = da[5];
dfs(r+1,c);
c[0] = da[2],c[1] = da[3],c[2] = da[1],c[3] = da[0],c[4] = da[4],c[5] = da[5];
dfs(r+1,c);
c[0] = da[4],c[1] = da[5],c[2] = da[2],c[3] = da[3],c[4] = da[0],c[5] = da[1];
dfs(r+1,c);
c[0] = da[4],c[1] = da[5],c[2] = da[2],c[3] = da[3],c[4] = da[1],c[5] = da[0];
dfs(r+1,c);
}
void bfs()
{
queue< pair <int,int> > q;
int f = b[0]*100000+b[1]*10000+b[2]*1000+b[3]*100+b[4]*10+b[5];
//int da[6] = {b[0],b[1],b[2],b[3],b[4],b[5]};
q.push(make_pair(0,f));
while(!q.empty()){
int r = q.front().first;
f = q.front().second;
vis[f] = 1;
q.pop();
int da[6] = {f/100000,f/10000%10,f/1000%10,f/100%10,f/10%10,f%10};
if(same(da)){
ans = min(ans,r);
return ;
}
f = da[3]*100000+da[2]*10000+da[0]*1000+da[1]*100+da[4]*10+da[5];
if(!vis[f]){
q.push(make_pair(r+1,f));
}
f = da[2]*100000+da[3]*10000+da[1]*1000+da[0]*100+da[4]*10+da[5];
if(!vis[f]){
q.push(make_pair(r+1,f));
}
f = da[5]*100000+da[4]*10000+da[2]*1000+da[3]*100+da[0]*10+da[1];
if(!vis[f]){
q.push(make_pair(r+1,f));
}
f = da[4]*100000+da[5]*10000+da[2]*1000+da[3]*100+da[1]*10+da[0];
if(!vis[f]){
q.push(make_pair(r+1,f));
}
//int c[6] = {da[3],da[2],da[0],da[1],da[4],da[5]};
//c[0] = da[3],c[1] = da[2],c[2] = da[0],c[3] = da[1],c[4] = da[4],c[5] = da[5];
//q.push(make_pair(r+1,c));
// int cc[6] = {da[2],da[3],da[1],da[0],da[4],da[5]};
// //c[0] = da[2],c[1] = da[3],c[2] = da[1],c[3] = da[0],c[4] = da[4],c[5] = da[5];
// q.push(make_pair(r+1,cc));
// int ccc[6] = {da[4],da[5],da[2], da[3], da[0],da[1]};
// //c[0] = da[4],c[1] = da[5],c[2] = da[2],c[3] = da[3],c[4] = da[0],c[5] = da[1];
// q.push(make_pair(r+1,ccc));
// int cccc[6] = {da[4],da[5],da[2],da[3],da[0],da[1]};
// //c[0] = da[4],c[1] = da[5],c[2] = da[2],c[3] = da[3],c[4] = da[0],c[5] = da[1];
// q.push(make_pair(r+1,cccc));
}
}
//top face, bottom face, left face, right face, front face and back face
int main () {
while(~RD(a[0])){
for(int i = 1;i < 6;++i)
RD(a[i]);
for(int i = 0;i < 6;++i)
RD(b[i]);
if(!check()){
puts("-1");
continue;
}
clr0(vis);
ans = 10005;
bfs();
if(ans >= 10005)
puts("-1");
else
printf("%d\n",ans);
}
return 0;
}

hdu 5012 模拟+bfs的更多相关文章

  1. hdu 5012(bfs)

    题意:给你2个 骰子,让你通过翻转使第一个变成第二个,求最少翻转数 思路:bfs #include<cstdio> #include<iostream> #include< ...

  2. HDU 5012 Dice (BFS)

    事实上是非常水的一道bfs,用字符串表示每一个状态,map判重就ok了. 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5012 #include&l ...

  3. hdu 5012 bfs --- 慎用STL 比方MAP判重

    http://acm.hdu.edu.cn/showproblem.php?pid=5012 发现一个问题 假设Sting s = '1'+'2'+'3'; s!="123"!!! ...

  4. ACM学习历程—HDU 5012 Dice(ACM西安网赛)(bfs)

    Problem Description There are 2 special dices on the table. On each face of the dice, a distinct num ...

  5. hdu 5012 bfs 康托展开

    Dice Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  6. HDU 1495 非常可乐 BFS 搜索

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目就不说了, 说说思路! 倒可乐 无非有6种情况: 1. S 向 M 倒 2. S 向 N 倒 3. N ...

  7. HDU 5012 骰子旋转(DFS)

    http://acm.hdu.edu.cn/showproblem.php?pid=5012 保存骰子的状态,然后用dfs或者bfs搜索 还是再讲一下dfs 我们的目标是找一个与b相同,且转次数最少的 ...

  8. HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4528 小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/O ...

  9. hdu 1072 Nightmare (bfs+优先队列)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...

随机推荐

  1. one by one 项目 part 2

    在网上百度了一下mySQL常用语句 ,整理如下: 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3. ...

  2. 经典动态规划python实现

    1.最长上升子序列 对于一个数字序列,请设计一个复杂度为O(nlogn)的算法,返回该序列的最长上升子序列的长度,这里的子序列定义为这样一个序列U1,U2...,其中Ui < Ui+1,且A[U ...

  3. java基础四 [构造器和垃圾回收](阅读Head First Java记录)

    本章讲解了对象的创建到被回收的过程,讲述了对象的生命周期   堆(heap)与栈(stack) 实例变量:实例变量是只声明在类下,方法外的变量(实例变量默认值为0/0.0/false,引用的默认值为n ...

  4. mysql中树形结构表的操作

    一种是:邻接表模型(局限性:对于层次结构中的每个级别,您需要一个自联接,并且随着连接的复杂性增加,每个级别的性能自然会降低.在纯SQL中使用邻接列表模型充其量是困难的.在能够看到类别的完整路径之前,我 ...

  5. hdoj2859(矩阵DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2859 思路: 第一次碰到这种矩阵上的DP题,想了半天也没想明白.本来想用子矩阵的左上角坐标和右下角坐标 ...

  6. 配置Maven从私服下载构件

    --------------------siwuxie095                                     配置 Maven 从私服下载构件         从 Nexus ...

  7. 理解数据结构Priority Queue

    我们知道Queue是遵循先进先出(First-In-First-Out)模式的,但有些时候需要在Queue中基于优先级处理对象.举个例子,比方说我们有一个每日交易时段生成股票报告的应用程序,需要处理大 ...

  8. 9-最短路径(dijkstra)

    参考博客:http://www.wutianqi.com/?p=1890 #include <iostream>using namespace std;#define  max 1< ...

  9. spring-boot基础概念与简单应用

    1.spring家族 2.应用开发模式 2.1单体式应用 2.2微服务架构 微服务架构中每个服务都可以有自己的数据库  3.微服务架构应当注意的细节 3.1关于"持续集成,持续交付,持续部署 ...

  10. php 中的信号处理

    首先我们需要了解几个函数 pcntl_signal   安装信号处理器,也就是当指定信号发生时,调用函数. pcntl_alarm   指定秒数后向进程发送SIGALRM信号. posix_getpi ...