Open the Lock

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
 
Author
YE, Kai
 
第一道双向BFS题、- -
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define N 10000 struct Node{
int n,t;
Node(){}
Node(int _n,int _t):n(_n),t(_t){}
};
struct Vis{
int d,t;
Vis(){}
Vis(int _d,int _t):d(_d),t(_t){}
}; int s,e;
Vis vis[N]; inline void getd(int n,int *a)
{
int k=;
while(n){
a[k++]=n%;
n/=;
}
}
inline int getn(int *a)
{
int n=;
for(int i=;i>=;i--){
n=n*+a[i];
}
return n;
}
int bfs()
{
int sp=; //层数控制
int t1[],t2[];
Node now,next;
memset(vis,,sizeof(vis));
queue<Node> p,q;
p.push(Node(s,));
q.push(Node(e,));
vis[s]=Vis(,);
vis[e]=Vis(,);
while(!p.empty() && !q.empty()){
while(p.front().t==sp){
Node now=p.front();
p.pop();
//加减1
getd(now.n,t1);
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
if(i<) t2[i]=t1[i]+>?:t1[i]+;
else t2[i-]=t1[i-]-<?:t1[i-]-;
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
p.push(next);
}
//交换相邻
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
swap(t2[i],t2[i+]);
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
p.push(next);
}
}
while(q.front().t==sp){
Node now=q.front();
q.pop();
//加减1
getd(now.n,t1);
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
if(i<) t2[i]=t1[i]+>?:t1[i]+;
else t2[i-]=t1[i-]-<?:t1[i-]-;
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
q.push(next);
}
//交换相邻
for(int i=;i<;i++){
memcpy(t2,t1,sizeof(t1));
swap(t2[i],t2[i+]);
next.t=now.t+;
next.n=getn(t2);
if(vis[next.n].d==) continue;
if(vis[next.n].d==) return next.t+vis[next.n].t;
vis[next.n].d=;
vis[next.n].t=next.t;
q.push(next);
}
}
sp++;
}
return -;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&s,&e);
printf("%d\n",bfs());
}
return ;
}

[HUD 1195] Open the Lock的更多相关文章

  1. hdu 1195 Open the Lock

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...

  2. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. hdu 1195 Open the Lock (BFS)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...

  5. hdu 1195 Open the Lock(广搜,简单)

    题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...

  6. HDU 1195 Open the Lock (双宽搜索)

    意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...

  7. HDU题解索引

    HDU 1000 A + B Problem  I/O HDU 1001 Sum Problem  数学 HDU 1002 A + B Problem II  高精度加法 HDU 1003 Maxsu ...

  8. BFS && DFS

    HDOJ 1312 Red and Black http://acm.hdu.edu.cn/showproblem.php?pid=1312 很裸的dfs,在dfs里面写上ans++,能到几个点就调了 ...

  9. CREATE A ENERGY / HEALTH BAR HUD

    Now then, let's get started. 1. Open the Play scene which you had created in the previous post. If y ...

随机推荐

  1. 【BZOJ】【1076】【SCOI2008】奖励关

    状压DP+数学期望 蒟蒻不会啊……看题跑…… Orz了一下Hzwer,发现自己现在真是太水了,难道不看题解就一道题也不会捉了吗? 题目数据范围不大……100*(2^16)很容易就跑过去了…… DP的时 ...

  2. semantic

    cgfx 里会有这个 float4X4 View : View; :后面这个 view 是一种 叫做user defined semantic provide the correct data to ...

  3. oracle sql 性能 优化

    目录[-] (1) 选择最有效率的表名顺序(只在基于规则的优化器中有效):ORACLE的解析器按照从右到左的顺序处理FROM子句中的表名,FROM子句中写在最后的表(基础表 driving table ...

  4. 【C++基础】sizeof 与 strlen的区别

    要理解两者的区别,就要分别理解他们的本质 strlen(char *) 计算字符串的长度,内部实现是用一个循环计算字符串的长度,直到‘\0’为止 1.srtlen 是一个函数,参数只能为char 或者 ...

  5. timeit统计运行时间

    import timeitt1 = timeit.timeit('sum(x*x for x in xrange(10000))',number = 10000) print t1

  6. Unity3d修改FBX文件的动画名方法

    问题描述:FBX文件导入Unity3d后的动画名字一般都是 “Take 001”并且无法修改!如何修改它呢? 解决方法:解决方法其实很简单,只要你按照Unity3d的FBX文件命名规则,压根就不会存在 ...

  7. POJ 1745

    #include <iostream> #define MAXN 10005 using namespace std; int _m[MAXN]; ]; int main() { //fr ...

  8. POJ1426Find The Multiple

    http://poj.org/problem?id=1426 题意 : 输入一个数n,找n的倍数m,这个m所满足的条件是,每一位数只能由0或1组成,在题目的旁边用红色的注明了Special Judge ...

  9. SDUT1586 计算组合数(组合数)

    这个题数据量小,不容易超时. #include<stdio.h> long long fac(int n) { ; ; i <= n ; i++) { m = i*m; } retu ...

  10. [转]Win7下Eclipse中文字体太小

    最近新装了Win7,打开eclipse3.7中文字体很小,简直难以辨认.在网上搜索发现这是由于Eclipse 3.7 用的字体是 Consolas,显示中文的时候默认太小了.   解决方式有两种:一. ...