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. 编译dubbo2.5.4时遇到的问题及解决

    dubbo的官方git地址为:https://github.com/alibaba/dubbo 按照其流程进行下载及编译,遇到的问题为: 1. 执行 mvn clean install -Dmaven ...

  2. js遍历集合

    首先,描述一下问题.由于需要后台取出数据,遍历展示在table中,大体如下: 后台取出的是 Map<String , List<Object>>,下面是我写得比较朴素一点的写法 ...

  3. dbutils报错:com.microsoft.sqlserver.jdbc.SQLServerException: 无法识别元数据的表

    今天用dbutils操作数据库,莫名地报错:com.microsoft.sqlserver.jdbc.SQLServerException: 无法识别元数据的表 检查了sql语句没有问题.经过仔细排查 ...

  4. mac 搭建git服务器

      一.简单搭建,不提供复杂的权限管理: 远程建立git用户,并打开ssh服务:见http://www.cnblogs.com/whj198579/archive/2013/04/09/3009350 ...

  5. Linux安装python 2.7.9

    1.下载python wget https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz 2.解压.编译安装 tar -zxvf Python- ...

  6. 【锋利的JQuery-学习笔记】添加提示图片

    效果图: hot图片: (注意:这个图标本身就有抖动效果的,并不是由于标签<del>而具有抖动效果) 周期性抖动,起到提示的作用 html: <div class="jnC ...

  7. Silverlight弹出层(转载)

    ChildWindow为Silverlight中的弹出子窗口 可以在项目新建子窗口文件: 相互传值: //父窗体向子窗体传值,需要在ChildWindow中构造函数进行传值ChildWindowTes ...

  8. 黑马程序员-- .net基础加强8之委托,事件,程序集

    ---------------------- ASP.Net+Android+IOS开发..Net培训.期待与您交流! ---------------------- 一.委托 ============ ...

  9. Java中转UTC时间字符串(含有T Z)为local时间

    在Java中我们需要转换相应格式的字符串,很多时候我们想到用SimpleDateFormat类来解析.但是最近我在调用一个第三方的接口时返回的 JSON字符串中有个expires字段的值是2014-0 ...

  10. A. Sorting Railway Cars

    A. Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input stan ...