hdu 1195 Open the Lock
题目连接
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的更多相关文章
- hdu - 1195 Open the Lock (bfs) && hdu 1973 Prime Path (bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1195 这道题虽然只是从四个数到四个数,但是状态很多,开始一直不知道怎么下手,关键就是如何划分这些状态,确保每一个 ...
- hdu 1195:Open the Lock(暴力BFS广搜)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1195 Open the Lock (BFS)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1195 Open the Lock(广搜,简单)
题目 猜密码,问最少操作多少次猜对,思路很简单的广搜,各种可能一个个列出来就可以了,可惜我写的很搓. 不过还是很开心,今天第一个一次过了的代码 #define _CRT_SECURE_NO_WARNI ...
- HDU 1195 Open the Lock (双宽搜索)
意甲冠军:给你一个初始4数字和目标4数字,当被问及最初的目标转换为数字后,. 变换规则:每一个数字能够加1(9+1=1)或减1(1-1=9),或交换相邻的数字(最左和最右不是相邻的). 双向广搜:分别 ...
- [HUD 1195] Open the Lock
Open the Lock Problem Description Now an emergent task for you is to open a password lock. The passw ...
- hdu 1195(搜索)
Open the Lock Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- hdu 1195
题意:就是给你n组的四位数,在一次变化中又一位数字可以变化,而变化的方式为加一减一或者是与隔壁的互换,注意,是每一个数字都可以, 求最少的变化次数到达目标的数字 一看这个就应该知道这是一个bfs的题目 ...
- hdu 1195 广度搜索
这题我们可以用优先队列,每次弹出队列中操作次数最少的一个,那么当找到匹配数时,该值一定是最优的.需要注意的时,加个vi[]数组,判读当前数是否已经存在于队列中.我做的很烦啊~~~ #include&l ...
随机推荐
- opecv轮廓匹配,可以用于去噪
一个跟轮廓相关的最常用到的功能是匹配两个轮廓.如果有两个轮廓,如何比较它们;或者如何比较一个轮廓和另一个抽象模板. 矩 比较两个轮廓最简洁的方式是比较他们的轮廓矩.这里先简短介绍一个矩的含义.简单的说 ...
- 解决spawn-fcgi child exited with: 1
spawn-fcgi -d /data/web/ad/ -f /data/web/ad/code.py -a -P /data/openresty_81/nginx/pid/ad.pid 出错的时候请 ...
- MFC TOOLBAR
m_imagelist.Create(,,ILC_COLOR24|ILC_MASK,,); CBitmap bmp; ;i<;i++) { int a= bmp.LoadBitmapW(IDB_ ...
- h5 吸顶效果 顶部悬浮
window.onscroll = function(){ var scrollTop = document.documentElement.scrollTop || document.body.sc ...
- Android开发中常用到方法总结
1.判断服务是否在运行中 public static boolean isServiceRunning(Context context, String serviceName) { boolean ...
- PAT1013
#include<cstdio>#include<cstring>#include<vector>using namespace std;const int max ...
- Json.net实现方便的Json转C#(dynamic动态类型)对象
以前需要将一段json字符串转换为C#对象时,一般都是定义一个与之对应的实体类来接收.这样做有一个很大的缺点,就是当字符串特别长,属性特别多,又有嵌套时,手敲这个实体类就非常痛苦. 比如之前做的一个接 ...
- VS2013连接不上TFS,TF31002记录
之前vs2013连接好好的,昨天就发现不行,类似如下错误 可能原因及解决办法: 1:C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG 下的 Ma ...
- [zt]Which are the 10 algorithms every computer science student must implement at least once in life?
More important than algorithms(just problems #$!%), the techniques/concepts residing at the base of ...
- XSS CSRF 攻击
XSS:跨站脚本(Cross-site scripting) CSRF:跨站请求伪造(Cross-site request forgery)定义: 跨网站脚本(Cross-site scripting ...