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. mysql 在线添加字段

    使用工具pt-online-schema-change #! /bin/bash stime=`date +%s` echo "增加字段开始测试时间为:`date +%H:%M:%S`&qu ...

  2. Eclipse上进行java web项目的打包

    以下是一个基于maven搭建的Spring Boot项目的目录结构 版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/ ...

  3. 补之前 如何改变jupyter打开文件的路径

    目录 如何改变jupyter打开文件的路径 第一种方法: 第二种方法 第三种方法 如何改变jupyter打开文件的路径 当我们直接打开jupyter时,直接加载的是我们的C盘文件 现在我们想打开其他盘 ...

  4. 掌握这些Python代码技巧,编程至少快一半!

    被人工智能捧红的 Python 已是一种发展完善且非常多样化的语言,其中肯定有一些你尚未发现的功能.本文或许能够让你学到一些新技巧. ​ Python 是世界上最流行.热门的编程语言之一,原因很多,比 ...

  5. poj-2488 a knight's journey(搜索题)

    Time limit1000 ms Memory limit65536 kB Background The knight is getting bored of seeing the same bla ...

  6. UVa - 1592 Database(STL,优化)

    给一个n行m列的数据库表格,问有没有两个行 r1,r2 和 c1,c2,满足(r1,r2)的元素=(c1,c2)的元素. n≤10000,m≤10. 直接枚举4个肯定会T的.可以只枚举c1 c2,然后 ...

  7. Linux磁盘分区介绍

    分区?我们不是已经在BIOS界面分区好了吗?如果领导给你一块磁盘,你怎么用呢?所以就有了分区工具(fdisk和parted),fdisk工具只针对小于2T磁盘分区,且是交互式的:parted很强大,通 ...

  8. Mysql显示所有数据库

    show databases; mysql> show databases; +--------------------+ | Database | +--------------------+ ...

  9. 【MySQL】MySQL基础

    一.基本语法 [MySQL目录结构]●bin目录,存储可执行文件●data目录,存储数据文件●docs,文档●include目录,存储包含的头文件●lib目录,存储库文件●share,错误信息和字符集 ...

  10. [转] WEB前端学习资源清单

    常用学习资源 JS参考与基础学习系列 [MDN]JS标准参考 es6教程 JS标准参考教程 编程类中文书籍索引 深入理解JS系列 前端开发仓库 <JavaScript 闯关记> JavaS ...