HDU 5012 Dice (BFS)
2015-12-29 09:46
274 查看
事实上是非常水的一道bfs,用字符串表示每一个状态,map判重就ok了。
题目链接:http://acm.hdu.edu.cn/showproblem.php?
pid=5012
题目链接:http://acm.hdu.edu.cn/showproblem.php?
pid=5012
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<cctype> #include<algorithm> #include<string> #include<queue> #include<map> #define N 7 using namespace std; string a,b; map<string,int> Hash; struct node { int t; string v; }; int rot[4][7]={ //Rotation operation {0,4,3,1,2,5,6}, //left {0,3,4,2,1,5,6}, //right {0,6,5,3,4,1,2}, //front {0,5,6,3,4,2,1}, //back }; int bfs() { queue<node> q; node u,v; u.t=0; u.v=b; q.push(u); Hash[b]=1; while(!q.empty()) { u=q.front(); q.pop(); if(u.v==a) return u.t; for(int i=0;i<4;i++) { v.v=""; for(int j=1;j<7;j++) v.v+=u.v[rot[i][j]-1]; //~~ if(!Hash[v.v]) { v.t=u.t+1; q.push(v); Hash[v.v]=1; } } } return -1; } int main() { string t; while(cin>>t) { a=b=""; a+=t; for(int i=2;i<=6;i++) { cin>>t; a+=t; } for(int j=1;j<=6;j++) { cin>>t; b+=t; } Hash.clear(); cout<<bfs()<<endl; } return 0; }
相关文章推荐
- Netty缓冲区动态扩容
- 在二元树中找出和为某一值的所有路径-数据结构
- 自定义Android Studio方法注释模板
- Java基础学习总结(13)——流IO
- 梯度下降来龙去脉附matlab代码
- 程序功能:布尔表达式翻译成四元式
- dwr和spring的整合
- Transaction、Datasource、Connection关系
- excel实用操作整理
- RxAndroid与RxJava 变换的原理值lift()
- LINUX GREP命令详解
- Eclipse——浏览功能
- 从大数据菜鸟走上大师的历程
- concurrenthashmap和hashmap
- Java基础学习总结(12)——一哈希编码HashCode
- Java基础学习总结(12)——一哈希编码HashCode
- Linq To SQL 的问题点滴
- java 访问 usb
- I2C上拉电阻
- 圣诞新年送豪礼《远征》双蛋运动开启--国民网游戏