HDU1195Open the Lock(BFS)
来源:互联网 发布:淘宝被套 编辑:程序博客网 时间:2024/06/08 17:49
Open the Lock
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.
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.
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
21234214411119999
Sample Output
24
#include<stdio.h>#include<queue>#include<iostream>using namespace std;struct node{ int a; int t;};int s,e;int toDigit(int a[]){ int ans=0; for(int i=0;i<4;i++) ans=ans*10+a[i]; return ans;}void toSprit(int a[],int ans){ int i=4; while(i>0) { a[--i]=ans%10; ans/=10; }}int bfs(){ queue<node>q; node p,tp; int vist[10000]={0},a[6]; if(s==e) return 0; p.a=s; p.t=0; q.push(p); vist[s]=1; while(!q.empty()) { p=q.front(); q.pop(); toSprit(a,p.a); tp.t=p.t+1; for(int i=0;i<4;i++) { if(a[i]==9) a[i]=1;//+1 else a[i]+=1; tp.a=toDigit(a); if(tp.a==e) return tp.t; if(vist[tp.a]==0) q.push(tp),vist[tp.a]=1; if(a[i]==1)a[i]=9; else a[i]--; if(a[i]==1) //-1 a[i]=9; else a[i]-=1; tp.a=toDigit(a); if(tp.a==e) return tp.t; if(vist[tp.a]==0) q.push(tp),vist[tp.a]=1; if(a[i]==9)a[i]=1;else a[i]++; if(i<3) //与前一个交换 { int tem=a[i]; a[i]=a[i+1]; a[i+1]=tem; tp.a=toDigit(a); if(tp.a==e) return tp.t; if(vist[tp.a]==0) q.push(tp),vist[tp.a]=1; tem=a[i]; a[i]=a[i+1]; a[i+1]=tem; } } } return -1;}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d%d",&s,&e); int time=bfs(); printf("%d\n",time); }}
0 0
- HDU1195Open the Lock(BFS)
- HDU1195Open the Lock( BFS )
- hdu1195Open the Lock(BFS)
- hdu1195Open the Lock(双向bfs+循环队列)
- HDU1195Open the Lock(AC)
- hdu1195Open the Lock
- hdu1195Open the Lock
- hdu1195Open the Lock(隐式图的广搜)
- Bfs++ open the lock
- Open the Lock(bfs)
- hdu1195 Open the Lock(BFS)
- HDU1195:Open the Lock(BFS)
- HDU1195:Open the Lock(BFS)
- hdu1195 Open the Lock (BFS)
- Open the Lock (BFS)
- zoj 2416 Open the Lock(BFS~)
- hdu 1195 Open the Lock bfs
- HDU 1195 Open the Lock BFS
- Json文件 使用rapidjson获取Json数据
- springMVC 注解实现实例 springMVC+ajax
- 【POJ1509】Glass Beads 最小表示法
- 学习Unity3D之列的基本操作、约束和ArrayList类。
- 浅析错排问题
- HDU1195Open the Lock(BFS)
- UVA 156 Ananagrams (字符串模拟)
- Python Socket 网络编程
- python 函数设计概念
- BZOJ 3798 特殊的质数 分块打表
- uva11520 Fill the Square
- 从B 树、B+ 树、B* 树谈到R 树
- Hibernate中配置ehcache缓存
- BAPI 异步更新问题