Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5970    Accepted Submission(s): 2666

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<cstdio>
#include<cstring>
#include<algorithm>
#include<math.h>
#include<queue>
#include<iostream>
using namespace std;
typedef long long LL;
bool vis[][][][];
struct Node{
int v[];
int step;
};
Node s,t;
bool _equal(Node a,Node b){
for(int i=;i<;i++){
if(a.v[i]!=b.v[i]) return false;
}
return true;
}
Node operate(int x,Node now){
Node next;
for(int i=;i<;i++){
next.v[i] = now.v[i];
}
next.step=now.step+;
if(x<){ ///+
if(now.v[x]==) next.v[x]=;
else next.v[x]=now.v[x]+;
}else if(x<){ ///-
if(now.v[x%]==) next.v[x%]=;
else next.v[x%]=now.v[x%]-;
}else{ ///exchange
int a = now.v[x%];
int b = now.v[x%+];
next.v[x%]=b;
next.v[x%+] = a;
}
return next;
}
int bfs(Node s){
memset(vis,false,sizeof(vis));
queue <Node>q;
vis[s.v[]][s.v[]][s.v[]][s.v[]]=true;
q.push(s);
s.step = ;
while(!q.empty()){
Node now = q.front();
q.pop();
if(_equal(now,t)){
return now.step;
}
for(int i=;i<;i++){ ///总共11种操作,[1-4]+ [1-4]- exwchange[1,2][2,3][3,4]
Node next=operate(i,now);
if(vis[next.v[]][next.v[]][next.v[]][next.v[]]==false){
vis[next.v[]][next.v[]][next.v[]][next.v[]]=true;
q.push(next);
}
}
}
return -;
}
int main()
{
char s1[],s2[];
int tcase;
scanf("%d",&tcase);
while(tcase--){ scanf("%s",s1);
scanf("%s",s2);
for(int i=;i<;i++){
s.v[i]=s1[i]-'';
t.v[i]=s2[i]-'';
}
int res = bfs(s);
printf("%d\n",res);
} return ;
}

hdu 1195(搜索)的更多相关文章

  1. hdu 5887 搜索+剪枝

    Herbs Gathering Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  2. hdu 5636 搜索 BestCoder Round #74 (div.2)

    Shortest Path  Accepts: 40  Submissions: 610  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: ...

  3. Square HDU 1518 搜索

    Square HDU 1518 搜索 题意 原题链接 给你一定若干个木棒,让你使用它们组成一个四边形,要求这些木棒必须全部使用. 解题思路 木棒有多种组合方式,使用搜索来进行寻找,这里需要进行优化,不 ...

  4. HDU 1195 Open the Lock (双宽搜索)

    意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...

  5. hdu 1195 广度搜索

    这题我们可以用优先队列,每次弹出队列中操作次数最少的一个,那么当找到匹配数时,该值一定是最优的.需要注意的时,加个vi[]数组,判读当前数是否已经存在于队列中.我做的很烦啊~~~ #include&l ...

  6. hdu 1195 Open the Lock

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1195 Open the Lock Description Now an emergent task f ...

  7. hdu 4848 搜索+剪枝 2014西安邀请赛

    http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...

  8. poj 1198 hdu 1401 搜索+剪枝 Solitaire

    写到一半才发现能够用双向搜索4层来写,但已经不愿意改了,干脆暴搜+剪枝水过去算了. 想到一个非常水的剪枝,h函数为  当前点到终点4个点的最短距离加起来除以2.由于最多一步走2格,然后在HDU上T了, ...

  9. hdu 1495 (搜索) 非常可乐

    http://acm.hdu.edu.cn/showproblem.php?pid=1495 搜索模拟出每此倒得情况就好,详情见代码\ (好困啊!!!!1) #include<cstdio> ...

随机推荐

  1. H5 移动APP - 面包店

    使用jquerymobile.html5.css3实现移动APP 流程图 打包 用hbuilder实现打包 效果截图展示 图2-1 APP图标 图2-2 主页 图2-3 面包列表&详细信息 图 ...

  2. GTX 750等低配显卡如何玩转Deepfakes?

    这里说的Deepfakes软件还是DeepFaceLab,人工智能换脸,是使用深度学习方法来实现的.而深度学习程序对电脑配置要求是非常高的,尤其是跑模型这个环节.很多低配电脑,根本就跑步起来.比如像G ...

  3. yield关键字有什么作用

    所属网站分类: python基础 > 语句 作者:goodbody 链接: http://www.pythonheidong.com/blog/article/10/ 来源:python黑洞网  ...

  4. leetcode-24-exercise

    506. Relative Ranks 解题思路: 使用priority_queue.它在插入时会将数据按照由大到小的顺序插入,自然排序了.所以插入时考虑插入pair<nums[i],i> ...

  5. IAR生成bin,HEX文件

    1.生成bin,hex文件 options->output converter->output format binary:.bin文件:intel extended:hex文件. 生成的 ...

  6. CRC点滴

    研究了一个晚上,大致看懂了crc校验的方法.这里记录一下,因为can总线中需要用到crc校验的. 举例说明CRC校验码的求法:(此例子摘自百度百科:CRC校验码) 信息字段代码为: 1011001:对 ...

  7. centos7 安全配置

    CentOS是最多人用来运行服务器的 Linux 版本,最新版本是 CentOS 7.当你兴趣勃勃地在一台主机或 VPS 上安装 CentOS 7 后,首要的工作肯定是加强它的安全性,以下列出的七件事 ...

  8. IIS发布网站Microsoft JET Database Engine 错误 '80004005'的解决办法,基于Access数据库

    在网站发布后,访问网站会有80004005的错误提示. 项目环境 项目基于Access数据库,server2012,文件系统为NTFS格式. 错误信息 Microsoft JETDatabase En ...

  9. clr(Windows 运行时和公共语言运行时)

    Windows 运行时   编译器使用 COM 引用计数机制来确定对象是否不再使用并可以删除. 因为从 Windows 运行时接口派生的对象实际上是 COM 对象,所以这是可行的. 在创建或复制对象时 ...

  10. dwr介绍及配置

    DWR 编辑 DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站.它可以允许在浏览器里的 ...