hdu1195 Open the Lock (DFS)
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.
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.
1234
2144
1111
9999
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)的更多相关文章
- Valid Pattern Lock(dfs + 暴力)
Valid Pattern Lock Time Limit: 2 Seconds Memory Limit: 65536 KB Pattern lock security is genera ...
- [HDU1195]Open the Lock
题目大意:给你一个4位数的初始状态(只包含1~9),要求你变化成另一个4位数. 变化规则为:每次可给任意一位加1或减1(1减1变为9,9加1变为1),或交换相邻两个数位上的数字(第一位和最后一位不相邻 ...
- Hadoop学习总结之五:Hadoop的运行痕迹
Hadoop学习总结之五:Hadoop的运行痕迹 Hadoop 学习总结之一:HDFS简介 Hadoop学习总结之二:HDFS读写过程解析 Hadoop学习总结之三:Map-Reduce入门 Ha ...
- hadoop源码调试
原文地址:http://www.cnblogs.com/end/archive/2011/04/26/2029497.html 在使用hadoop的时候,可能遇到各种各样的问题,然而由于hadoop的 ...
- Hadoop源码分析(3): Hadoop的运行痕迹
在使用hadoop的时候,可能遇到各种各样的问题,然而由于hadoop的运行机制比较复杂,因而出现了问题的时候比较难于发现问题. 本文欲通过某种方式跟踪Hadoop的运行痕迹,方便出现问题的时候可以通 ...
- RAC环境下ORACLE序列缓存导致序列混乱
目前项目中发现了这样一个问题,在数据库部署了RAC环境之后,偶尔会出现从Oracle Sequence所取出来的数是混乱的,比如第二次比第一次所取的数要小.这样当程序的逻辑依赖于ID的大小来排序时,就 ...
- 【等待事件】等待事件系列(5.1)--Enqueue(队列等待)
[等待事件]等待事件系列(5.1)--Enqueue(队列等待) 1 BLOG文档结构图 2 前言部分 2.1 导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可 ...
- NameNode和SecondaryNameNode(面试开发重点)
NameNode和SecondaryNameNode(面试开发重点) 1 NN和2NN工作机制 思考:NameNode中的元数据是存储在哪里的? 首先,我们做个假设,如果存储在NameNode节点的磁 ...
- 【等待事件】序列等待事件总结(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文档结 ...
随机推荐
- MVVM模式应用 之xml文件的读取
XML如下所示: <?xml version="1.0" encoding="utf-8" ?> <schools> <schoo ...
- Zend Studio 11.0.2 破解和汉化
本方法适用于Zend Studio 11.0.2,亲测,其他版本未知. 破解方法:覆盖安装目录 plugins 里同名文件,启动任意输入即可注册. Windows版下载地址:http://downlo ...
- 开发中遇到的angularJs的小问题
1.在使用自定义指令后如果需要将指令内部的数据传给外层controller需要设置scope属性,此时注意,如果指令内传出的数据在某种情况下需要清空,如传出一个选中元素的数组,点击清空按钮则清空选中这 ...
- windows下配置wnmp
最近尝试windows下配置nginx+php+mysql,在这里总结一下. 1.下载windows版本的nginx,官网下载地址:http://nginx.org/en/download.htm, ...
- MYSQL 错误 :Out of resources when opening file './datagather/mx_domain#P#p178.MYD' (Errcode: 24) 解决办法
出现Out of resources when opening file './xxx.MYD' (Errcode: 24)错误是因为打开的文件数超过了my.cnf的--open-files-limi ...
- 【python】运算优先级
来源:小甲鱼 课件
- 关于全局变量和函数,在其他类中调用问题,extern关键字
1个工程下有几个类文件,和1个全局的文件comm.h, comm.h中存放了这几个类同时需要的变量和同时调用的函数. 当时,我只在comm.h中定义: int commData1: vector&l ...
- C#让程序自动在管理员权限下运行
windows 7和vista提高的系统的安全性,同时需要明确指定“以管理员身份运行”才可赋予被运行软件比较高级的权限,比如访问注册表等.否则,当以普通身份运行的程序需要访问较高级的系统资源时,将会抛 ...
- CoreProfiler/NanoProfiler
使用CoreProfiler/NanoProfiler实现跨平台&应用的整合性能调试 摘要 NanoProfiler是一个开源.NET性能调试类库,CoreProfiler是其.NET Cor ...
- 转:etcd:从应用场景到实现原理的全方位解读
原文来自于:http://www.infoq.com/cn/articles/etcd-interpretation-application-scenario-implement-principle ...