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. mysql 刘道成视频教程1、2课----------大致结构

    第1课 第2课 一.整体结构 1.mysql -h localhost -u root -p *** 如果-h不写,则默认连接localhost. 2.连接服务器成功,-------> 显示数据 ...

  2. 设置服务器远程连接mysql

    一直单人开发所以没有考虑过这方面,到新公司要做合作开发,所以要进行设置,然后开始自己搞 下面把过程罗列一下: 1)由于使用的云服务器 ,所以上面都配置好了,直接配置了mysql的命令行输入密码就可以进 ...

  3. OnClose()和 OnDestroy()

    OnClose()和 OnDestroy() 基于对话框的MFC程序,发现每次程序退出时,托盘的小图标不能自动消失,鼠标移上去之后才能消失,比较不爽. 后来发现我删除这个图标的代码是在自己重写的OnC ...

  4. uitableview的空白处不能响应 touchesbegan 事件

    现在的uitableview 的上面  响应不了     touchesbegan 事件   可能算是苹果的一个bug吧,不知道以后会不会改变 今天试了好久  都不行  最后  写了个字类  继承自 ...

  5. 转载:如何避免代码中的if嵌套

    http://top.jobbole.com/4960/ http://stackoverflow.com/questions/24430504/how-to-avoid-if-chains 在Sta ...

  6. Codeforces Round #205 (Div. 2) : D

    思维题,感叹自己的智商不够啊. 思路大概是这样的: 1.排在队伍前面的女生是不用换位置的: 2.女生在队伍中的顺序是不会变的: 3.最后一个女生稳定了则程序结束: 4.每个女生都有个初始位置和最终位置 ...

  7. mongodb 查看数据库和表大小

    1.查看数据库 > db.stats(); { "db" : "test", //当前数据库 "collections" : 3, / ...

  8. LeetCode 面试:Add Binary

    1 题目 Given two binary strings, return their sum (also a binary string). For example,a = "11&quo ...

  9. C/C++中程序在使用堆内存时的内存复用问题

    在一个C/C++程序中,如果使用了堆内存的管理机制,那么内存究竟是怎么分配与回收的呢? 先看一个程序: #include <iostream> using namespace std; i ...

  10. .config-20150410

    ## Automatically generated file; DO NOT EDIT.# OpenWrt Configuration#CONFIG_MODULES=yCONFIG_HAVE_DOT ...