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
#include<stdio.h>
#include<string.h>
int vist[10],flog;
char str[5],ch[5];
void DFS(int j,int x,int step,char ch1[])
{
int i,st,e;
char tem[5];
//printf("%s %d\n",ch1,ste);
if(str[0]==ch1[0]&&str[1]==ch1[1]&&str[2]==ch1[2]&&str[3]==ch1[3])//一样了
{
if(flog>step) //记录小的
flog=ste;
//printf("%d ",ste);
}
vist[x]=1; //表示在ch1中第x位置方问过
for(i=x+1;i<4;i++)
if(vist[i]==0)
{
if(str[j]-ch1[i]>=5||str[j]-ch1[i]<=-5)//第一步,当成立时,把ch1[i]加或减变成str[j]的所用时间加起来,会用tem装
{
if(str[j]>ch[i])
st=(ch1[i]-'0'+'9'-str[j])+step;
else
st=(str[j]-'0'+'9'-ch1[i])+step;
}
else
{
if(str[j]>ch1[i])
st=(str[j]-ch1[i])+step;
else
st=(ch1[i]-str[j])+step;
}
for(e=0;e<j;e++)//把ch1的第j个以前的数和str第j个数以前相同的数先装起来
tem[e]=ch1[e];
tem[j]=str[j];//当前第j个位置改变成str和第j个
if(i!=j)//不相等说明是回朔了,i比j大
{
for(e=j+1;e<=i;e++)//把第i个要放到第j个前面,那么ch1中从第j个开始位置开始到第i个位置都往后存放一位,
tem[e]=ch1[e-1];
for(;e<4;e++) //把第i个位置以后的数装入
tem[e]=ch1[e];
}
else//相等说明还没有回朔,那么就直接存放
for(e=j+1;e<4;e++)
tem[e]=ch1[e];
//printf("%d %d %d %s %s\n",j,i,st+i-j,tem,ch1); DFS(j+1,j,st+i-j,tem);//i-j的意思是把ch1里的第i个移到第j个位置,那么tem中从0->i(包括第i个)都是与str相同
}
vist[x]=0;
}
int main()
{
int i,t,j,e,st;
char ch1[5];
scanf("%d",&t);
while(t--)
{
getchar();
scanf("%s",str);
getchar();
scanf("%s",ch);
flog=13322;
for(i=0;i<4;i++)//以ch中的第i个数为开头,并把头一个变成str[0]相同
{
ch1[0]=ch[i];
for(j=0,e=1;j<4;j++)
if(j!=i)
ch1[e++]=ch[j]; if(str[0]-ch1[0]>=5||str[0]-ch1[0]<=-5)
{
if(str[0]>ch1[0])
st=(ch1[0]-'0'+'9'-str[0]);
else
st=(str[0]-'0'+'9'-ch1[0]);
}
else
{
if(str[0]>ch1[0])
st=(str[0]-ch1[0]);
else
st=(ch1[0]-str[0]);
}
ch1[0]=str[0];
//printf("%s\n",ch1);
//printf("%d\n",st+i);
DFS(1,0,st+i,ch1);//i是从第i个到第一个须要多少步,st是把ch[i]变成str[0]须要多少步
//printf("\n");
}
printf("%d\n",flog);
}
}

 

hdu1195 Open the Lock (DFS)的更多相关文章

  1. Valid Pattern Lock(dfs + 暴力)

    Valid Pattern Lock Time Limit: 2 Seconds      Memory Limit: 65536 KB Pattern lock security is genera ...

  2. [HDU1195]Open the Lock

    题目大意:给你一个4位数的初始状态(只包含1~9),要求你变化成另一个4位数. 变化规则为:每次可给任意一位加1或减1(1减1变为9,9加1变为1),或交换相邻两个数位上的数字(第一位和最后一位不相邻 ...

  3. Hadoop学习总结之五:Hadoop的运行痕迹

    Hadoop学习总结之五:Hadoop的运行痕迹   Hadoop 学习总结之一:HDFS简介 Hadoop学习总结之二:HDFS读写过程解析 Hadoop学习总结之三:Map-Reduce入门 Ha ...

  4. hadoop源码调试

    原文地址:http://www.cnblogs.com/end/archive/2011/04/26/2029497.html 在使用hadoop的时候,可能遇到各种各样的问题,然而由于hadoop的 ...

  5. Hadoop源码分析(3): Hadoop的运行痕迹

    在使用hadoop的时候,可能遇到各种各样的问题,然而由于hadoop的运行机制比较复杂,因而出现了问题的时候比较难于发现问题. 本文欲通过某种方式跟踪Hadoop的运行痕迹,方便出现问题的时候可以通 ...

  6. RAC环境下ORACLE序列缓存导致序列混乱

    目前项目中发现了这样一个问题,在数据库部署了RAC环境之后,偶尔会出现从Oracle Sequence所取出来的数是混乱的,比如第二次比第一次所取的数要小.这样当程序的逻辑依赖于ID的大小来排序时,就 ...

  7. 【等待事件】等待事件系列(5.1)--Enqueue(队列等待)

    [等待事件]等待事件系列(5.1)--Enqueue(队列等待)   1  BLOG文档结构图   2  前言部分   2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可 ...

  8. NameNode和SecondaryNameNode(面试开发重点)

    NameNode和SecondaryNameNode(面试开发重点) 1 NN和2NN工作机制 思考:NameNode中的元数据是存储在哪里的? 首先,我们做个假设,如果存储在NameNode节点的磁 ...

  9. 【等待事件】序列等待事件总结(enq: SQ - contention、row cache lock、DFS lock handle和enq: SV - contention)

    [等待事件]序列等待事件总结(enq: SQ - contention.row cache lock.DFS lock handle和enq: SV -  contention) 1  BLOG文档结 ...

随机推荐

  1. 使用for循环嵌套实现乘法口诀表

    九九乘法表的实现: package com.liaojianya.chapter1; /** * This program demonstrates the way of using * for-lo ...

  2. 使用ArrayList对大小写字母的随机打印

    从a~z以及A~Z随机生成一个字母并打印:打印全部的字母 package com.liaojianya.chapter1; import java.util.ArrayList; /** * This ...

  3. 打印十进制数n 递归

    #include<stdio.h> //printd函数: 打印十进制数n void printd(int n){ ){ putchar('-'); n=-n; } ) printd(n/ ...

  4. div section article aside的理解

    div 是一个大的容器 内部可以包含header main nav aside footer等标签 没有语义,多用于为脚本添加样式 section的语义比div语义强些,用于主题性比较强的内容,比如一 ...

  5. 或许有一两点你不知的C语言特性

    关键字篇 volatile关键字 鲜为人知的关键字之一volatile,表示变量是'易变的',之所以会有这个关键字,主要是消除编译优化带来的一些问题,看下面的代码 ; int b = a; int c ...

  6. 全连接的BP神经网络

    <全连接的BP神经网络> 本文主要描述全连接的BP神经网络的前向传播和误差反向传播,所有的符号都用Ng的Machine learning的习惯.下图给出了某个全连接的神经网络图. 1前向传 ...

  7. tyvj 1934 高精度

    「Poetize3」Heaven Cow与God Bull From wwwwodddd     背景 Background __int64 ago,there's a heaven cow call ...

  8. hduoj 1077 Catching Fish 求单位圆最多覆盖点个数

    Catching Fish Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  9. PHP 如何安全的使用 MySQL ?

    大多数 PHP 程序员对 MySQL 肯定不陌生,至于各种 MySQL 函数的用法在开发手册和 w3school 这类网站上也有很多介绍.但是,你所用的写法真的安全吗?面对越来越猖獗的黑客攻击,SQL ...

  10. 在非UI线程中更改UI(Delphi使用隐藏窗口来处理,QT使用信号槽)

    在Delphi里我记得是使用TThread.Synchronize(TThreadMethod),原理是利用了一个隐藏窗口来处理. 在QT Debug模式一下,碰到了同样的问题,显示错误: canno ...