题目连接

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. Flex Alert的匿名回调函数如何得到正确的this

    Flex中经常使用Alert来弹出提示或确认窗口,为了方便省事,会直接用匿名函数作为回调,但有时如果要调用外部的this,你会发现匿名函数中的this无法指向外部父类,可以使用e.target获取pa ...

  2. eclipse 弹出智能提示、代码自动换行

    在eclipse 中编写java 程序时,为了快速查找变量或搜索方法调用,在智能窗口的帮助下,程序的编写速度会更快,但eclipse 默认下并不弹出智能窗口,这就需要自己进行配置,设置的步骤如下: 打 ...

  3. mysql中explain看性能

    select distinct col_name from table where a=X and b=Y and date(time)='xx-xx-xx';执行时间 27.9772 秒 expla ...

  4. 终于解决了PHP调用SOAP过程中的种种问题。(转)

    最近在做公司和第三方的一个合作项目,需要调用统一验证接口和统一支付接口.由于牵涉公司机密,所以我要单独写一层PHP的接口给第三方用.前面那个验证接口主要卡在了des加密的方式上,这个有时间再说.这篇主 ...

  5. NHibernate输出SQL语句

    用了NHierbate之后,很少需要写原生的SQL语句,由于总是看不到SQL语句,所以有时候对SQL调优非常不利.因此产生了让NHibernate输出它所生成的SQL语句的想法,以便于后续调优. 一. ...

  6. DataGridview焦点不移开不保存数据问题

    this.datagridLeft.ClearSelection();                this.datagridLeft.Refresh();                this. ...

  7. Datatable导出Excel

    ; IRow headerRow = sheet.CreateRow(); ; ; ; iRowIndex++; } ; i < icolIndex; i++) { sheet.AutoSize ...

  8. WinForm中使用AnyCAD三维控件 の 初始化

    在WinForm中可以方便的集成AnyCAD.Net三维控件,只需要以下几部即可完成. 一.添加DLL程序集 AnyCAD.Foundation.Net.dll AnyCAD.Presentation ...

  9. s3c6410_u-boot-2010.03移植【续】

    本文接上一篇:http://www.cnblogs.com/tanghuimin0713/p/3965528.html 6.3)重新编译,烧写,运行 U-Boot - ::) for SMDK6410 ...

  10. 遍历input。select option 选中的值

    <label> <input name="Fruit" type="radio" value="0" class=&quo ...