题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1195

Open the Lock

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<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::swap;
using std::sort;
using std::map;
using std::pair;
using std::queue;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
typedef unsigned long long ull;
bool vis[N];
int start, end;
struct Node {
int v, s;
Node(int i = , int j = ) :v(i), s(j) {}
};
inline void calc_1(int *arr, int v) {
int i, t, j = ;
do arr[j++] = v % ; while (v /= );
for (i = , --j; i < j; i++, j--) {
t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
}
inline int calc_2(int *arr) {
int sum = ;
rep(i, ) sum = sum * + arr[i];
return sum;
}
inline void calc_3(queue<Node> &q, int k, int s) {
if (!vis[k]) {
q.push(Node(k, s + ));
vis[k] = true;
}
}
int bfs() {
int v, k, tmp[];
cls(vis, false);
queue<Node> q;
q.push(Node(start, ));
vis[start] = true;
while (!q.empty()) {
Node t = q.front(); q.pop();
if (t.v == end) return t.s;
calc_1(tmp, t.v);
rep(i, ) {
v = tmp[i];
if (v + == ) tmp[i] = ;
else tmp[i] = v + ;
k = calc_2(tmp);
calc_3(q, k, t.s);
tmp[i] = v;
if (v - == ) tmp[i] = ;
else tmp[i] = v - ;
k = calc_2(tmp);
calc_3(q, k, t.s);
tmp[i] = v;
}
rep(i, ) {
calc_1(tmp, t.v);
swap(tmp[i], tmp[i + ]);
k = calc_2(tmp);
calc_3(q, k, t.s);
}
}
return ;
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int t;
scanf("%d", &t);
while (t--) {
scanf("%d %d", &start, &end);
printf("%d\n", bfs());
}
return ;
}

hdu 1195 Open the Lock的更多相关文章

  1. hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...

  2. hdu 1195:Open the Lock(暴力BFS广搜)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  3. hdu 1195 Open the Lock (BFS)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  4. hdu 1195 Open the Lock(广搜,简单)

    题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...

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

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

  6. [HUD 1195] Open the Lock

    Open the Lock Problem Description Now an emergent task for you is to open a password lock. The passw ...

  7. hdu 1195(搜索)

    Open the Lock Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  8. hdu 1195

    题意:就是给你n组的四位数,在一次变化中又一位数字可以变化,而变化的方式为加一减一或者是与隔壁的互换,注意,是每一个数字都可以, 求最少的变化次数到达目标的数字 一看这个就应该知道这是一个bfs的题目 ...

  9. hdu 1195 广度搜索

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

随机推荐

  1. SDUT 3345 数据结构实验之二叉树六:哈夫曼编码

    数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 字符的编 ...

  2. Xcode清缓存

    前往-->按住option键进入资源库-->Developer-->Xcode-->DerivedData   删除里面的文件就行了

  3. 在 mvc 中使用下拉列表

    在mvc中经常会使用到下拉列表,以下以两种方式来实现,一种是以  @Html.DropDownList 扩展方法,一种是以 <select><option></optio ...

  4. ORA-00933: SQL command not properly ended

    今天写了一个小的SQL语句类似下面的这句: UPDATE A SET ID=B.ID FROM A,B WHERE A.NAME=B.NAME 在执行时居然报了“ORA-00933: SQL comm ...

  5. hadoop2.5.1搭建(一)

    1.1配置 1.1.1修改hosts vi /etc/hosts 192.168.220.64 cluster4 192.168.220.63 cluster3 1.2安装jdk rpm安装 rpm ...

  6. 洛谷P2085 最小函数值(minval)

    P2085 最小函数值(minval) 218通过 487提交 题目提供者该用户不存在 标签堆高级数据结构 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目描述 有n个函数, ...

  7. noip2010提高组题解

    NOIP2010提高组题解 T1:机器翻译 题目大意:顺序输入n个数,有一个队列容量为m,遇到未出现元素入队,求入队次数. AC做法:直接开1000的队列模拟过程. T2:乌龟棋 题目大意:有长度为n ...

  8. VC与JavaScript交互(三) --- CWebPage类调用javascript函数(给js函数传参,并取得返回值)

    ①需要一个别人写好的类CWebPage,将其对于的两个文件WebPage.h和WebPage.cpp添加到工程中. ②添加WebBrowser控件,在视图/对话框类的头文件中#include &quo ...

  9. Cordova V3.0.0中config.xml配置文件的iOS Configuration

    http://www.cnblogs.com/lovecode/articles/3305655.html   轉載這個 <preference> 关于这个标签的可用设置有: Disall ...

  10. C# 加密算法

     public static class Common     {         #region MD5加密         /// <summary>            /// M ...