hdu 5012 模拟+bfs
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的更多相关文章
- hdu 5012(bfs)
题意:给你2个 骰子,让你通过翻转使第一个变成第二个,求最少翻转数 思路:bfs #include<cstdio> #include<iostream> #include< ...
- HDU 5012 Dice (BFS)
事实上是非常水的一道bfs,用字符串表示每一个状态,map判重就ok了. 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5012 #include&l ...
- hdu 5012 bfs --- 慎用STL 比方MAP判重
http://acm.hdu.edu.cn/showproblem.php?pid=5012 发现一个问题 假设Sting s = '1'+'2'+'3'; s!="123"!!! ...
- 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 ...
- hdu 5012 bfs 康托展开
Dice Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- HDU 1495 非常可乐 BFS 搜索
http://acm.hdu.edu.cn/showproblem.php?pid=1495 题目就不说了, 说说思路! 倒可乐 无非有6种情况: 1. S 向 M 倒 2. S 向 N 倒 3. N ...
- HDU 5012 骰子旋转(DFS)
http://acm.hdu.edu.cn/showproblem.php?pid=5012 保存骰子的状态,然后用dfs或者bfs搜索 还是再讲一下dfs 我们的目标是找一个与b相同,且转次数最少的 ...
- HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4528 小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/O ...
- hdu 1072 Nightmare (bfs+优先队列)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 Description Ignatius had a nightmare last night. H ...
随机推荐
- 软件工程导论九月26号Homework
习题3 (1)数据流图 (2)实体关系图ER 习题6
- redmine邮件配置
网上找了半天,有很多答案,最后自己测试找出一个解决办法. 1.找到安装位置 D:\Bitnami\redmine-2.5.2-2\apps\redmine\htdocs\config下的文件confi ...
- C#四舍五入说明
string.Format("{0:N2}", d) 与 Math.Round(d, 2).ToString() 不总是相等 string.Format("{0:N2}& ...
- Windows自带的端口转发工具netsh使用方法_DOS/BAT
Windows自带的端口转发工具netsh使用方法_DOS/BAT 作者:用户 来源:互联网 时间:2017-02-22 17:24:30 netsh 端口转发 摘要: 下面的代码在windows ...
- topGO
前面我们讲过GO.db这个包,现在接着延伸topGO包,该包是用来协助GO富集分析 1)安装 if("topGO" %in% rownames(installed.packages ...
- Spring Boot中使用Websocket搭建即时聊天系统
1.首先在pom文件中引入Webscoekt的依赖 <!-- websocket依赖 --> <dependency> <groupId>org.springfra ...
- 4sum, 4sum closest
4sum [抄题]: [思维问题]: 以为很复杂,其实是“排序+双指针”的最高阶模板 [一句话思路]: [输入量特别大怎么办]: [画图]: [一刷]: 先排序! if (i > 0 & ...
- [leetcode]125. Valid Palindrome判断回文串
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...
- spring中配置监听队列的MQ
一.spring中配置监听队列的MQ相关信息注:${}是读取propertites文件的常量,这里忽略.绿色部分配置在接收和发送端都要配置. <bean id="axx" ...
- Spring框架的IOC核心功能快速入门
2. 步骤一:下载Spring框架的开发包 * 官网:http://spring.io/ * 下载地址:http://repo.springsource.org/libs-release-local/ ...