Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3388    Accepted Submission(s): 1499

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
 
Source
 
Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:  1175 1072 1026 1180 1044 
 
 //15MS    340K    1607 B    C++
/* 题意:
给出两个四位数,有两种操作,相邻交换或每位+/-1;
求最少步数使第一串变为第二串 bfs:
细心点就可以过,先考虑每一次最多可以有多少步可以走,因为只有四位数,
而且有两种操作,一共有七步,相邻交换有三步,每位操作有四步,知道这个然后就常规的
bfs求解。 */
#include<iostream>
#include<queue>
using namespace std;
struct node{
int a,cnt;
};
int a,b;
int vis[];
int bfs()
{
memset(vis,,sizeof(vis));
queue<node>Q;
node t={a,};
vis[a]=;
Q.push(t);
while(!Q.empty()){
t=Q.front();
Q.pop();
if(t.a==b) return t.cnt;
node tt=t;
tt.cnt++;
tt.a=t.a%+(t.a/)*+(t.a%)/*;
if(!vis[tt.a]){
Q.push(tt);vis[tt.a]=;
//printf("1*%d\n",tt.a);
}
tt.a=t.a/*+(t.a%)/*+(t.a%)/*+t.a%;
if(!vis[tt.a]){
Q.push(tt);vis[tt.a]=;
//printf("2*%d\n",tt.a);
}
tt.a=t.a/*+(t.a%)*+(t.a%)/;
if(!vis[tt.a]){
Q.push(tt);vis[tt.a]=;
//printf("3*%d\n",tt.a);
} for(int i=;i<;i*=){
int temp=(t.a%(i*))/i;
if(temp==) tt.a=t.a-*i;
else tt.a=t.a+i;
if(!vis[tt.a]){
Q.push(tt);vis[tt.a]=;
//printf("4*%d\n",tt.a);
}
if(temp==) tt.a=t.a+*i;
else tt.a=t.a-i;
if(!vis[tt.a]){
Q.push(tt);vis[tt.a]=;
//printf("5*%d\n",tt.a);
}
}
if(t.a<) return ;
}
return ;
}
int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&a,&b);
printf("%d\n",bfs());
}
return ;
}

hdu 1195 Open the Lock (BFS)的更多相关文章

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

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

  2. hdu 1195 Open the Lock

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

  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(广搜,简单)

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

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

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

  6. HDU 2717 Catch That Cow --- BFS

    HDU 2717 题目大意:在x坐标上,农夫在n,牛在k.农夫每次可以移动到n-1, n+1, n*2的点.求最少到达k的步数. 思路:从起点开始,分别按x-1,x+1,2*x三个方向进行BFS,最先 ...

  7. HDU 5876 关于补图的bfs

    1.HDU 5876  Sparse Graph 2.总结:好题,把STL都过了一遍 题意:n个点组成的完全图,删去m条边,求点s到其余n-1个点的最短距离. 思路:把点分为两个集合,A为所有没有到达 ...

  8. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  9. HDU(1175),连连看,BFS

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...

随机推荐

  1. EBS并发管理器启动失败,系统暂挂,在重置计数器之前修复管理程序

    今天EBS安装补丁之后,因为停并发管理器的时候,因为关闭EBS应用时,并发管理器没有在前台停止,就直接停了应用服务,导致启动时,并发管理器直接起不来了,使用adcmctl.sh也没有办法启动. 进入系 ...

  2. Open closed principle

    #include <iostream> using namespace std; class Book { public: string getContents() { return &q ...

  3. JAVAOOP集合框架

    集合框架三大内容:对外的接口.接口的实现和对集合运算的算法 集合有三大类接口:List.Set.Map 共同点:都是集合接口,都可以用来存储很多对象 不同:Collection接口存储一组不唯一(允许 ...

  4. 22.1 高级函数【JavaScript高级程序设计第三版】

    函数是JavaScript 中最有趣的部分之一.它们本质上是十分简单和过程化的,但也可以是非常复杂和动态的.一些额外的功能可以通过使用闭包来实现.此外,由于所有的函数都是对象,所以使用函数指针非常简单 ...

  5. WPF Datagrid对鼠标单击进行响应,借助EventSetter

    在做的一个c#的项目中发现Datagrid没办法直接对鼠标单击进行响应, 调用MouseDown事件也需要点击某一行第二次才能响应. 所以借助EventSetter来简单的实现了一个. 界面部分的代码 ...

  6. python创建字典

    创建: {x:x**2 for x in (2,4,6)} dict(xjm=110,lxh=119,pzq=120) dict([('a',1),('b',2),('c',3)])

  7. mysql学习第三天练习(日期和时间函数)

    -- 日期和时间函数 -- 获取当前日期 select curdate(),current_date() from dual -- 返回服务器当前的日期和时间 select NOW(),SYSDATE ...

  8. storm集群安装部署

    安装步骤: 搭建Zookeeper集群: 安装Storm依赖库: 下载并解压Storm发布版本: 修改storm.yaml配置文件: 启动Storm各个后台进程. 1. 搭建Zookeeper集群 这 ...

  9. CentOS 7.X 防火墙简单配置

    CentOS7使用的是Linux Kernel 3.10.0的内核版本,新版的Kernel内核已经有了防火墙netfilter,并且使用效能更高,稳定性更好. 配置防火墙的两种方法: 一.使用xml配 ...

  10. 一道关于C++ 继承/虚函数 笔试题 [转]

    转自:http://www.cnblogs.com/yangyh/archive/2011/06/04/2072393.html 首先这位作者, 因为看了这篇简短的一个博文, 我相同了关于虚函数方面的 ...