hdu 1195 双向bfs
来源:互联网 发布:windows to go可以 编辑:程序博客网 时间:2024/06/03 12:30
http://acm.hdu.edu.cn/showproblem.php?pid=1195
这代码好长,不过好多重复的内容。
#include <iostream>#include <queue>#include <cstring>using namespace std;struct node{ int x, step;};int vis[10000];node q1[10000];node q2[10000];int c[5],d[5];int ss[2]={1,-1};void hash1(int x){ for(int i = 3; i>=0; i--) { c[i] = x%10; x/=10; }}int rehash(){ int a = 0; for(int i = 0; i<4; i++) a = a*10+d[i]; return a;}int front1,front2,tail1,tail2,tmp;int bfs(int a, int b){ front1=front2=tail1=tail2=0; node temp; temp.x=a; temp.step=0; q1[tail1++] = temp; temp.x=b; temp.step=0; q2[tail2++]=temp; vis[a]=1; vis[b]=2; while(front1<tail1||front2<tail2) { tmp = tail1; for(; front1<tmp; front1++) { temp = q1[front1]; hash1(temp.x); for(int k = 0; k<2; k++) { for(int i = 0; i<4; i++) { for(int j = 0; j<4; j++) d[j]=c[j]; d[i]+=ss[k]; if(k==0&&d[i]>9) d[i] = 1; else if(k==1&&d[i]<1) d[i] = 9; int ans = rehash(); if(vis[ans]!=1) { if(vis[ans]==2) { int cnt; for(cnt=1; cnt<tail2; cnt++) if(q2[cnt].x==ans) break; return q2[cnt].step+q1[front1].step+1; } vis[ans] = 1; temp.x = ans; temp.step = q1[front1].step+1; q1[tail1++] = temp; } } } for(int i = 0; i<3; i++) { for(int j = 0; j<4; j++) d[j] = c[j]; int o = d[i]; d[i] = d[i+1]; d[i+1] = o; int ans = rehash(); if(vis[ans]!=1) { if(vis[ans]==2) { int cnt; for(cnt=1; cnt<tail2; cnt++) if(q2[cnt].x==ans) break; return q2[cnt].step+q1[front1].step+1; } vis[ans] = 1; temp.x = ans; temp.step = q1[front1].step+1; q1[tail1++] = temp; } } } tmp = tail2; for(; front2<tmp; front2++) { temp = q2[front2]; hash1(temp.x); for(int k = 0; k<2; k++) { for(int i = 0; i<4; i++) { for(int j = 0; j<4; j++) d[j]=c[j]; d[i]+=ss[k]; if(k==0&&d[i]>9) d[i] = 1; else if(k==1&&d[i]<1) d[i] = 9; int ans = rehash(); if(vis[ans]!=2) { if(vis[ans]==1) { int cnt; for(cnt = 1; cnt<tail1; cnt++) if(q1[cnt].x==ans) break; return q1[cnt].step+q2[front2].step+1; } vis[ans] = 2; temp.x = ans; temp.step = q2[front2].step+1; q2[tail2++] = temp; } } } for(int i = 0; i<3; i++) { for(int j = 0; j<4; j++) d[j] = c[j]; int o = d[i]; d[i] = d[i+1]; d[i+1] = o; int ans = rehash(); if(vis[ans]!=2) { if(vis[ans]==1) { int cnt; for(cnt = 1; cnt<tail1; cnt++) if(q1[cnt].x==ans) break; return q1[cnt].step+q2[front2].step+1; } vis[ans] = 2; temp.x = ans; temp.step = q2[front2].step+1; q2[tail2++] = temp; } } } } return 0;}int main(){ int t,a,b; cin>>t; while(t--) { memset(vis,0,sizeof(vis)); cin>>a>>b; int ans = bfs(a,b); cout<<ans<<endl; }}
0 0
- hdu 1195 双向bfs
- HDU 1195 Open the Lock (双向BFS)
- HDU 1195 Open the Lock(双向BFS)
- 双向BFS-->hdu 1195 Open the Lock
- hdu 1401 双向bfs
- hdu 3085 双向BFS
- hdu 3085(双向bfs)
- HDU 1560 双向BFS
- HDU 3085 双向BFS
- HDU 1401 双向BFS !!!
- HDU 1195 BFS,双向BFS两种写法
- HDU 1195 Open the Lock (双向BFS与单向BFS)
- HDU 1195 Open the Lock(bfs or 双向bfs)
- HDU 1401 Solitaire 双向BFS
- hdu 1401 Solitaire 双向bfs
- HDU 1401 Solitaire 双向BFS
- HDU 3085 Nightmare 双向bfs
- hdu-3085(双向bfs)
- UVALive 4329--Ping pong+树状数组
- Centos下源码包安装lamp常见的几个小问题
- MT7621A 硬件调试总结---(3)
- QTreeView的使用总结1
- AndroidUI设计之 布局管理器 - 详细解析布局实现
- hdu 1195 双向bfs
- 黑马程序员——Java基础——Collection集合的总结
- vs2012编译caffe
- linux常用命令手册
- lucene的基础知识
- 响应式布局的一些要点
- [JQ权威指南]第五天:导航条在项目中运用
- JS问题汇总
- vss问题:客户端提示"Sourcesafe cannot find a default database.Would you like to select one?"