Open the Lock[HDU1195]
Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2914 Accepted Submission(s): 1271
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
Zhejiang University Local Contest 2005
Recommend
Ignatius.L
- #include<stdio.h>
- #include<string.h>
- #include<queue>
- #include<iostream>
- using namespace std;
- class node
- {
- public:
- int x[4];
- int time;
- int val()
- {
- return x[0]*1000+x[1]*100+x[2]*10+x[3];
- }
- };
- int S,P;
- bool s[10000];
- int bfs()
- {
- memset(s,false,sizeof(s));
- s[S]=true;
- queue<node> q;
- while (!q.empty()) q.pop();
- node tmp;
- tmp.x[0]=S/1000;
- tmp.x[1]=(S-tmp.x[0]*1000)/100;
- tmp.x[2]=(S-tmp.x[0]*1000-tmp.x[1]*100)/10;
- tmp.x[3]=S%10;
- tmp.time=0;
- q.push(tmp);
- while (!q.empty())
- {
- node x=q.front();
- q.pop();
- int state;
- if (x.val()==P) return x.time;
- int i;
- for (i=0;i<4;i++)
- {
- tmp=x;
- if (tmp.x[i]==9) tmp.x[i]=1;
- else tmp.x[i]++;
- tmp.time=x.time+1;
- state=tmp.val();
- if (!s[state])
- {
- s[state]=true;
- q.push(tmp);
- }
- tmp=x;
- if (tmp.x[i]==1) tmp.x[i]=9;
- else tmp.x[i]--;
- tmp.time=x.time+1;
- state=tmp.val();
- if (!s[state])
- {
- s[state]=true;
- q.push(tmp);
- }
- }
- for (i=0;i<3;i++)
- {
- tmp=x;
- int t=tmp.x[i];
- tmp.x[i]=tmp.x[i+1];
- tmp.x[i+1]=t;
- tmp.time=x.time+1;
- state=tmp.val();
- if (!s[state])
- {
- s[state]=true;
- q.push(tmp);
- }
- }
- }
- }
- int main()
- {
- int T;
- scanf("%d",&T);
- while (T--)
- {
- scanf("%d%d",&S,&P);
- printf("%d\n",bfs());
- }
- return 0;
- }
Open the Lock[HDU1195]的更多相关文章
- ACM-BFS之Open the Lock——hdu1195(双向BFS)
这道题的0基础版本号,暴力BFS及题目详情请戳:http://blog.csdn.net/lttree/article/details/24658031 上回书说道,要用双向BFS来尝试一下. 最终A ...
- hdu1195 Open the Lock (DFS)
Problem Description Now an emergent task for you is to open a password lock. The password is consist ...
- [HDU1195]Open the Lock
题目大意:给你一个4位数的初始状态(只包含1~9),要求你变化成另一个4位数. 变化规则为:每次可给任意一位加1或减1(1减1变为9,9加1变为1),或交换相邻两个数位上的数字(第一位和最后一位不相邻 ...
- C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent
看下组织结构: System.Object System.MarshalByRefObject System.Threading.WaitHandle System.Threading.Mutex S ...
- 多线程同步工具——Lock
本文原创,转载请注明出处. 参考文章: <"JUC锁"03之 公平锁(一)> <"JUC锁"03之 公平锁(二)> 锁分独占锁与共享锁, ...
- java 线程 Lock 锁使用Condition实现线程的等待(await)与通知(signal)
一.Condition 类 在前面我们学习与synchronized锁配合的线程等待(Object.wait)与线程通知(Object.notify),那么对于JDK1.5 的 java.util.c ...
- InnoDB:Lock & Transaction
InnoDB 是一个支持事务的Engine,要保证事务ACID,必然会用到Lock.就像在Java编程一下,要保证数据的线程安全性,必然会用到Lock.了解Lock,Transaction可以帮助sq ...
- 使用四元数解决万向节锁(Gimbal Lock)问题
问题 使用四元数可以解决万向节锁的问题,但是我在实际使用中出现问题:我设计了一个程序,显示一个三维物体,用户可以输入绕zyx三个轴进行旋转的指令,物体进行相应的转动. 由于用户输入的是绕三个轴旋转的角 ...
- 万向节锁(Gimbal Lock)的理解
[TOC] 结论 我直接抛出结论: Gimbal Lock 产生的原因不是欧拉角也不是旋转顺序,而是我們的思维方式和程序的执行逻辑没有对应,也就是说是我们的观念导致这个情况的发生. 他人解释 首先我们 ...
随机推荐
- FOJ 2161 Jason and Number
暴力模拟找规律: 552287 2014-04-23 21:08:48 Accepted 2161 Visual C++ 0 ms 192KB 347B Watermelon #include< ...
- Metasploit是一款开源的安全漏洞检测工具,
Metasploit是一款开源的安全漏洞检测工具,可以帮助安全和IT专业人士识别安全性问题,验证漏洞的缓解措施,并管理专家驱动的安全性进行评估,适合于需要核实漏洞的安全专家,同时也适合于强大进攻能力的 ...
- shell 生成指定范围随机数与随机字符串 .
shell 生成指定范围随机数与随机字符串 分类: shell 2014-04-22 22:17 20902人阅读 评 ...
- PHP 冒泡原理
header('Content-Type: text/html; charset=utf-8'); // 简单冒泡算法 $a = array(5,43,3,2,1); function mp($a){ ...
- Smarty s01
复习面向过程中,如何输出显示变量的内容 01.php 第一个版本,使用三个文件来输出html 1.访问文件 2.类MyTpl.class.php 3.一个html模板文件 课堂练习第一个版本 第二个版 ...
- 面向侧面的程序设计AOP-------《一》概述
Aspect-Oriented Programming(面向方面编程,AOP)正好可以解决这一问题.它允许开发者动态地修改静态的OO模型,构造出一个能够不断增长以满足新增需求的系统,就象现实世界中的对 ...
- iOS UIBezierPath知识介绍
UIBezierPath是在画图,定制动画轨迹中都有应用. UIBezierPath有许多类方法,能够创建基本的曲线,比如利用一个rect创建一个椭圆path的方法:bezierPathWithOva ...
- codeforces 471B. MUH and Important Things 解题报告
题目链接:http://codeforces.com/problemset/problem/471/B 题目意思:有 n 个 tasks,编号依次为 1 - n,每个 task 都有一定的难度值来评估 ...
- jquery给height拼接动态变量
var sizeLength = "${list.size()}"; if(sizeLength==''){ sizeLength=0; } sizeLength=400*size ...
- HDU 5754 Life Winner Bo (各种博弈) 2016杭电多校联合第三场
题目:传送门 题意:一个国际象棋棋盘,有四种棋子,从(n,m)走到(1,1),走到(1,1)的人赢,先手赢输出B,后手赢输出G,平局输出D. 题解:先把从(n,m)走到(1,1)看做是从(1,1)走到 ...