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 ;
}

最新文章

  1. 动画系统(Mecanim补充)
  2. T-SQL 语句的理解
  3. Python 包管理工具解惑
  4. c++ basic 整理1
  5. csu oj 1811: Tree Intersection (启发式合并)
  6. jQuery中开发插件的两种方式
  7. 【疑问】css
  8. CopyOnWriteArrayList简介
  9. 29 内置方法 eval | exec 元类 单例
  10. Buzzsumo大型教程(内容营销+外链outreach必备)营销神器
  11. MySQL中临时表的基本创建与使用教程(create temporary table )
  12. shell脚本把一些请求量非常高的ip给拒绝掉
  13. C++矩阵库 Eigen 快速入门
  14. php功能模块学习笔记
  15. soj1011. Lenny&#39;s Lucky Lotto
  16. 初识CSS样式表
  17. 玩转Javascript 给JS写测试
  18. HDU 4764:Stone(巴什博弈)
  19. 16.1 eclipse设置
  20. Unity3D手游开发日记(6) - 适合移动平台的水深处理

热门文章

  1. windows下配置kafka
  2. 使用泛型实现对int数组或者String数组进行排序
  3. poj_1730_Perfect Pth Powers
  4. matlab2018a安装后帮助文档打不开解决方法
  5. glibc2.12升级至2.15
  6. 在win10上同时安装Python2/Python3
  7. Python学习笔记:logging(日志处理)
  8. Makefile (3) 基本语法和使用
  9. Spark机器学习之推荐引擎
  10. [学习笔记]CSS选择器