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. hdu5691 Sitting in Line(状压dp)

    1 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...

  2. CSU 1216异或最大值 (0-1 trie树)

    Description 给定一些数,求这些数中两个数的异或值最大的那个值 Input 多组数据.第一行为数字个数n,1 <= n <= 10 ^ 5.接下来n行每行一个32位有符号非负整数 ...

  3. tcp客户端socket

    import socket # 和udp的区别显而易见,udp发送和接收的是一个元祖,因为udp是不建立连接的,只有得到了对方的端口和ip才能进行沟通. # 而tcp不是,tcp发送和接受的是一个字符 ...

  4. POJ 1981 最大点覆盖问题(极角排序)

    Circle and Points Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 8346   Accepted: 2974 ...

  5. POJ 2084 Catalan

    Game of Connections Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 8772   Accepted: 43 ...

  6. Git-Git库管理

    对象和引用哪里去了? 从GitHub上克隆一个示例版本库,这个版本库在"历史穿梭"一章就已经克隆过一次了,现在要重新克隆一份.为了和原来的克隆相区别,克隆到另外的目录.执行下面的命 ...

  7. cocos2d-x 3.0环境配置(转)

    cocos2d-x 3.0发布有一段时间了,作为一个初学者,我一直觉得cocos2d-x很坑.每个比较大的版本变动,都会有不一样的项目创建方式,每次的跨度都挺大…… 但是凭心而论,3.0RC版本开始 ...

  8. centos使用--centos7.3配置LNMP

    目录 1 源的配置 2 安装软件 2.1 安装php7 2.2 安装nginx 2.3 安装mysql 2.4 安装vsftp (ftp登录配置) 3 开机启动设置 4 其它一些配置 4.1 git的 ...

  9. vim 简单命令

    (1)查找结果全部单独显示 命令: :lvimgrep /pattern/ % | lopen (2)设置文本高亮 命令: :colorscheme evening 把 ":colorsch ...

  10. SpringMVC 集成 Velocity 模板引擎

    本文通过 maven 项目中集成 1.引入 SpringMVC 与 Velocity 需要的依赖 <!-- SpringMVC --> <dependency> <gro ...