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. thinkcmf5更新模板代码分析,解决模板配置json出错导致数据库保存的配置项内容丢失问题

    private function updateThemeFiles($theme, $suffix = 'html') { $dir = 'themes/' . $theme; $themeDir = ...

  2. python爬虫基础10-selenium大全4/8-Webelement

    Selenium笔记(4)Webelement 本文集链接:https://www.jianshu.com/nb/25338984 这是通过find方法找到的页面元素,此对象提供了多种方法,让我们可以 ...

  3. day24 02 单继承(派生)

    day24 02 单继承(派生) 1.首先来看一个简单的例子 比如: 狗类的属性有:吃,喝,看门 鸟类的属性有:吃,喝,下蛋 看门和下蛋就是这两种动物不同的属性,而吃喝是两个共同的属性 以下代码实现了 ...

  4. 有关nmap的5个常用的扫描指令

    [以下IP可替换成需要被测试的IP网段] 1.ping扫描:扫描192.168.0.0/24网段上有哪些主机是存活的: nmap -sP 192.168.0.0/24   2.端口扫描:扫描192.1 ...

  5. 前端之bootstrap

    一.响应式介绍 众所周知,电脑.平板.手机的屏幕是差距很大的,假如在电脑上写好了一个页面,在电脑上看起来不错,但是如果放到手机上的话,那可能就会乱的一塌糊涂,这时候怎么解决呢?以前,可以再专门为手机定 ...

  6. 离线安装eclipse maven插件

    最近用到maven,所以按照官网http://www.eclipse.org/m2e/的教程http://download.eclipse.org/technology/m2e/releases/,在 ...

  7. 剑指offer算法编程题目部分汇总(解法略)

    总结一下本书中遇到的大部分面试题.面试题3:二维数组中的查找 题目:在一个二维数组中,每一行都按照从左到右的递增顺序排列,每一列都按照从上到下递增的顺序排列,请完成一个函数,输入这样的一个整数,判断数 ...

  8. Leetcode4--->求两个排序数组的中位数

    题目:给定两个排序数组,求两个排序数组的中位数,要求时间复杂度为O(log(m+n)) 举例: Example 1: nums1 = [1, 3] nums2 = [2] The median is ...

  9. php 操作excel

    <?php $dir=dirname(__FILE__);//查找当前脚本所在路径 require $dir."/db.php";//引入mysql操作类文件 require ...

  10. jQuery实现当按下回车键时绑定点击事件

    jQuery实现当按下回车键时绑定点击事件 <script> $(function(){ $(document).keydown(function(event){ if(event.key ...