codevs 1733 聪明的打字员 (Bfs)
2016-06-11 20:38
405 查看
/* Bfs+Hash 跑的有点慢 但是codevs上时间限制10s 也ok */ #include<iostream> #include<cstdio> #include<cstring> #include<queue> #define maxn 10000010 using namespace std; int len; bool f[maxn]; string ls,rs; struct node { int step,place; string s; }; queue<node>q; int Hash(node x) { int re=0; len=x.s.length(); for(int i=1;i<=len-1;i++) re=re*10+x.s[i]-'0'; re=re*10+x.place; return re; } int main() { cin>>ls>>rs; ls=' '+ls;rs=' '+rs; node st;st.s=ls; st.step=0;st.place=1; q.push(st);f[Hash(st)]=1; while(!q.empty()) { node k=q.front();q.pop(); string si=k.s; int p=k.place,t=k.step; if(si==rs) { cout<<t; return 0; } for(int i=1;i<=6;i++) { int pi,ki;node x; string ss=si; if(i==1&&p<6) { pi=p;pi++; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } else if(i==2&&ss[p]<'9') { ss[p]++;pi=p; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } else if(i==3&&ss[p]>'0') { ss[p]--;pi=p; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } else if(i==4) { char tmps=ss[p];ss[p]=ss[1];ss[1]=tmps;pi=p; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } else if(i==5) { char tmps=ss[p];ss[p]=ss[6];ss[6]=tmps;pi=p; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } else if(i==6&&p>1) { pi=p;pi--; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } } } return 0; }
/* 加上剪枝的话就ok了 200ms 对于2 3 4 5这几个点 左移右移对答案是没有贡献的 只有1 6 左移右移再加上swap0 1才有贡献 所以2 3 4 5这几个只有已经和目标相同了才左右移 */ #include<iostream> #include<cstdio> #include<cstring> #include<queue> #define maxn 10000010 using namespace std; int len; bool f[maxn]; string ls,rs; struct node { int step,place; string s; }; queue<node>q; int Hash(node x) { int re=0; len=x.s.length(); for(int i=1;i<=len-1;i++) re=re*10+x.s[i]-'0'; re=re*10+x.place; return re; } int main() { //freopen("clever.in","r",stdin); //freopen("clever.out","w",stdout); cin>>ls>>rs; ls=' '+ls;rs=' '+rs; node st;st.s=ls; st.step=0;st.place=1; q.push(st);f[Hash(st)]=1; while(!q.empty()) { node k=q.front();q.pop(); string si=k.s; int p=k.place,t=k.step; if(si==rs) { cout<<t; return 0; } for(int i=1;i<=6;i++) { int pi,ki;node x; string ss=si; if(i==1&&p<6) { if((p==2||p==3||p==4)&&ss[p]!=rs[p])continue; pi=p;pi++; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } else if(i==2&&ss[p]<'9') { ss[p]++;pi=p; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } else if(i==3&&ss[p]>'0') { ss[p]--;pi=p; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } else if(i==4) { char tmps=ss[p];ss[p]=ss[1];ss[1]=tmps;pi=p; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } else if(i==5) { char tmps=ss[p];ss[p]=ss[6];ss[6]=tmps;pi=p; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } else if(i==6&&p>1) { if((p==2||p==3||p==4)&&ss[p]!=rs[p])continue; pi=p;pi--; x.place=pi;x.step=t+1;x.s=ss; int hash=Hash(x); if(f[hash]==0) { f[hash]=1; q.push(x); } } } } return 0; }
相关文章推荐
- Android使用Rxjava异步递归读取内存卡中所有的视频文件
- 人月神话阅读笔记03
- AVAudioPlayer本地音乐播放、后台播放、歌词同步,都告诉你
- 获取手机软件缓存的方法(反射)getPackageSizeInfo
- 关于指针的一点思考
- HTML5游戏开发/微信游戏开发——Cocos2dx-js开发环境搭建(win32平台)
- Poj487-3279
- SVN使用教程
- MyEclipse xml文件不能自动补全解决
- java多线程共享变量
- 文章标题
- swustoj---1091
- 【Get深一度】7种常用的排序算法
- 获取本机IP地址
- 多台服务器部署zookeeper集群
- iOS 自定义和导出代码块模板
- 乐学成语——第三部分
- 用python做个弹球游戏(一)
- Acdream 1210 Chinese Girls' Amusement(大数模板运算 + 找规律)
- 学习进度条