题目连接

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. 【LeetCode】6. ZigZag Conversion 锯齿形转换

    题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...

  2. c# 读取XML数据

    1.首先调用接口,要有一个post数据到指定url并返回数据的函数: protected string PostXmlToUrl(string url, string postData) { stri ...

  3. python中时间和时区

    1.时区 http://blog.csdn.net/cz157733055/article/details/38319195 2.时间 datetime.timedelta代表两个时间之间的的时间差 ...

  4. Android基础总结(7)——异步消息处理

    服务(Service)是Android中实现程序后台运行的解决方案,它非常适合用于去执行哪些不需要和用户交互而且还要长期运行的任务.服务的运行不依赖任何用户界面,即使当程序被切换到后台,或者用户打开了 ...

  5. java服务器

    WebLogic BEA公司开发的(被Oracle收购了)收费的 支持JavaEE所有的规范(ejb servlet/jsp规范) java  mysql(oracle)      2.WebSphe ...

  6. ANTLR4权威参考手册

    ANTLR4权威参考手册(一) - 活在当下 乐在其中 - 博客频道 - CSDN.NET ANTLR4权威参考手册

  7. angular $apply()以及$digest()讲解

    重点的东西放上面,说三遍: 记住的最重要的是ng是否能检测到你对于model的修改.如果它不能检测到,那么你就需要手动地调用$apply()! 记住的最重要的是ng是否能检测到你对于model的修改. ...

  8. Recover damage pictures to see the crime scene

    Few people know that when you take photos there is also a thumbnail embeded inside the file, even so ...

  9. Linux 之dhcp服务搭建

    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议 dhcp服务器端监控端口67 涉及的配置文件:/etc/dhcp/dhcpd ...

  10. DirectDraw打造极速图形引擎(Alpha混合)

    显然DirectDraw是Windows下写2D图形程序的最好选择,虽然Direct3D也可以写,但是没DirectDraw简单方便,特别对于初学者,一来就接触那么多函数和参数总不是件愉快的事,所以我 ...