HDU 1195 Open the Lock(双向BFS)
2015-07-14 23:02
399 查看
Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5108 Accepted Submission(s): 2251
Problem Description
Now an emergent task for you is to open a password lock. The password is consisted of four digits. Each digit is numbered from 1 to 9.
Each time, you can add or minus 1 to any digit. When add 1 to '9', the digit will change to be '1' and when minus 1 to '1', the digit will change to be '9'. You can also exchange the digit with its neighbor. Each action will take one step.
Now your task is to use minimal steps to open the lock.
Note: The leftmost digit is not the neighbor of the rightmost digit.
Input
The input file begins with an integer T, indicating the number of test cases.
Each test case begins with a four digit N, indicating the initial state of the password lock. Then followed a line with anotther four dight M, indicating the password which can open the lock. There is one blank line after each test case.
Output
For each test case, print the minimal steps in one line.
Sample Input
2
1234
2144
1111
9999
Sample Output
2
4
思路:双向BFS
分别从开始项和结束项出发BFS遍历,相遇时输出答案并跳出搜索。
代码:
//0MS 1804K #include<iostream> #include<cstring> #include<queue> using namespace std; int dis[10005];//改变需要的步数 int color[10005];//正向,反向搜索标记 queue<int> que1,que2; int num[5],num1[5]; void tochar(int x) { num[0]=x/1000; num[1]=x%1000/100; num[2]=x%100/10; num[3]=x%10; } int toint(int *num) { return num[0]*1000+num[1]*100+num[2]*10+num[3]; } int bfs(int sta,int en) { while(!que1.empty()) que1.pop(); while(!que2.empty()) que2.pop(); memset(color,0,sizeof(color)); memset(dis,0,sizeof(dis)); que1.push(sta); que2.push(en); color[sta] = 1; color[en] = 2; while(!que1.empty() || !que2.empty()) { if(!que1.empty()) { int t = que1.front(); que1.pop(); tochar(t); for(int i=0;i<4;i++) { num1[0]=num[0]; num1[1]=num[1]; num1[2]=num[2]; num1[3]=num[3]; num1[i]=num[i]+1; if(num1[i]==10) num1[i]=1; int ans=toint(num1); if(color[ans]==0) { color[ans]=1; dis[ans]=dis[t]+1; que1.push(ans); } else if(color[ans]==2) return (dis[ans]+dis[t]+1); num1[i]=num[i]-1; if(num1[i]==0) num1[i]=9; ans=toint(num1); if(color[ans]==0) { color[ans]=1; dis[ans]=dis[t]+1; que1.push(ans); } else if(color[ans]==2) return (dis[ans]+dis[t]+1); } for(int i=0;i<3;i++) { num1[0]=num[0]; num1[1]=num[1]; num1[2]=num[2]; num1[3]=num[3]; int k = num1[i]; num1[i] = num1[i+1]; num1[i+1] = k; int ans = toint(num1); if(color[ans]==0) { color[ans]=1; dis[ans]=dis[t]+1; que1.push(ans); } else if(color[ans]==2) return (dis[ans]+dis[t]+1); } } if(!que2.empty()) { int t = que2.front(); que2.pop(); tochar(t); for(int i=0;i<4;i++) { num1[0]=num[0]; num1[1]=num[1]; num1[2]=num[2]; num1[3]=num[3]; num1[i]=num[i]+1; if(num1[i]==10) num1[i]=1; int ans=toint(num1); if(color[ans]==0) { color[ans]=2; dis[ans]=dis[t]+1; que2.push(ans); } else if(color[ans]==1) return (dis[ans]+dis[t]+1); num1[i]=num[i]-1; if(num1[i]==0) num1[i]=9; ans=toint(num1); if(color[ans]==0) { color[ans]=2; dis[ans]=dis[t]+1; que2.push(ans); } else if(color[ans]==1) return (dis[ans]+dis[t]+1); } for(int i=0;i<3;i++) { num1[0]=num[0]; num1[1]=num[1]; num1[2]=num[2]; num1[3]=num[3]; int k = num1[i]; num1[i] = num1[i+1]; num1[i+1] = k; int ans = toint(num1); if(color[ans]==0) { color[ans]=2; dis[ans]=dis[t]+1; que2.push(ans); } else if(color[ans]==1) return (dis[ans]+dis[t]+1); } } } } int main() { int t,ss,ee; cin>>t; while(t--) { cin>>ss>>ee; if(ss==ee) { cout<<0<<endl; continue; } cout<<bfs(ss,ee)<<endl; } return 0; }
相关文章推荐
- shell脚本中$的作用
- 【架构设计】MVP与MVC,实现Android应用层开发
- Linux的SOCKET编程详解
- 用JAX-WS在Tomcat中发布WebService
- 用Hadoop构建电影推荐系统
- 大型网站系统架构的演化
- 每天一个linux命令(2):cd命令
- linux镜像源设置
- 设置Linux内核参数 /etc/sysctl.conf
- Linux内核参数 /etc/sysctl.conf 功能说明
- Linux:安装双系统(Win7+Ubuntu)后,Ubuntu正常,Win7无法启动,有需要的朋友可以参考下
- Docker exec与Docker attach
- 关于centos中文件权限的设置
- linux0.12之内核代码之『深入追踪变参函数的实现』
- CentOS 6.5实现LAMP+NFS双web服务负载均衡
- adb shell:fail to startdeamon
- centos下的压缩解压缩
- 每天一个Linux命令-3( ls和 ll )
- 第四章、bash特性详解
- Window下Eclips用Maven构建Hadoop项目