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. [转载]点评阿里云、盛大云等国内IaaS产业

    免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:刘黎明      原文地址:http://www.chinacloud.org ...

  2. VIM配置(转载)

    注: 转载于http://www.cnblogs.com/ma6174/ 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主要有以下优点: 1.按F5可以直接编译并执行C.C++.ja ...

  3. c库函数之scanf

    scanf()函数的原理 想象输入设备(键盘)连接着一个叫“缓冲”的东西,把缓冲认为是一个字符数组. 当你的程序执行到scanf时,会从你的缓冲区读东西,如果缓冲区是空的,就阻塞住,等待你从键盘输入. ...

  4. 获取c++ edit控件内容

    CString str1,str2; ((CEdit*)GetDlgItem(IDC_EDIT1))->GetWindowText(str1); ((CEdit*)GetDlgItem(IDC_ ...

  5. pureftpd安装配置-pureftp参数详解(一)

    1. 下载 #cd /usr/local/src/ #wget ftp://ftp.pureftpd.org/pub/pure-ftpd/releases/pure-ftpd-1.0.30.tar.g ...

  6. Linq查询Count、Sum、Min、Max、Average

    原文地址:Linq——Count.Sum.Min.Max.Average作者:mousekitty Linq查询之Count.Sum.Min.Max.Average using System; usi ...

  7. [转载]Spring Java Based Configuration

    @Configuration & @Bean Annotations Annotating a class with the @Configuration indicates that the ...

  8. Java 理论和实践: 了解泛型

    转载自 : http://www.ibm.com/developerworks/cn/java/j-jtp01255.html 表面上看起来,无论语法还是应用的环境(比如容器类),泛型类型(或者泛型) ...

  9. POJ1416Shredding Company

    http://poj.org/problem?id=1416 题意 : 要为碎纸机公司开发一种新的碎纸机,这种碎纸机要具有3个特性 :一是粉碎机以一个目标数 t 作为输入,并且粉碎的纸上写有一个数字n ...

  10. 【mysql的设计与优化专题(6)】mysql索引攻略

    所谓索引就是为特定的mysql字段进行一些特定的算法排序,比如二叉树的算法和哈希算法,哈希算法是通过建立特征值,然后根据特征值来快速查找,而用的最多,并且是mysql默认的就是二叉树算法 BTREE, ...