题目连接

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. 慕课网-安卓工程师初养成-4-9 Java循环语句之 for

    来源:http://www.imooc.com/code/1425 Java 的循环结构中除了 while 和 do...while 外,还有 for 循环,三种循环可以相互替换. 语法: 执行过程: ...

  2. socket 和 SocketServer 模块

    一 .Socket 网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket socket(TCP,IP)通常也称作"套接字",用于描述IP地址和端 ...

  3. 6 个优秀的开源 OCR 光学字符识别工具

    转自:http://sigvc.org/bbs/thread-870-1-1.html 纸张在许多地方已日益失宠,无纸化办公谈论40多年,办公环境正限制纸山的生成.而过去几年,无纸化办公的概念发生了显 ...

  4. TCP/IP详解学习笔记(8)-- UDP:用户数据报协议

    1.UDP概述 UDP是一种无连接的, 即发送数据前不需要建立连接,因此减小的开销和发送数据的延迟. UDP使用尽最大努力交付,即不保证可靠交付,因此主机不需要维持复杂的连接状态表. UDP是面向报文 ...

  5. C#中DataTable转换JSON

    #region 将DataTable转换为json public string dt2json(DataTable dt) { JavaScriptSerializer jss = new JavaS ...

  6. 利用Ossim系统进行主机漏洞扫描

    利用Ossim系统进行主机漏洞扫描 企业中查找漏洞要付出很大的努力,不能简单的在服务器上安装一个漏洞扫描软件那么简单,那样起不了多大作用.这并不是因为企业中拥有大量服务器和主机设备,这些服务器和设备又 ...

  7. C++三种内存分配方式

    从静态存储区域分配:内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量.静态分配的区域的生命期是整个软件运行期,就是说从软件运行开始到软件终止退出.只 ...

  8. PHP isset()与empty()的区别

    PHP的isset()函数 一般用来检测变量是否设置 格式:bool isset ( mixed var [, mixed var [, ...]] ) 功能:检测变量是否设置 返回值: 若变量不存在 ...

  9. flask-admin

    初始化 class Admin(app=None, name=None, url=None, subdomain=None, index_view=None, translations_path=No ...

  10. c++ builder TreeView控件节点遍历

    void __fastcall TForm1::GetRootNodes(TTreeView *DestTreeView)//得到所有根节点 { TTreeNode *vNode = NULL; vN ...