博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 1195 Open the Lock
阅读量:5088 次
发布时间:2019-06-13

本文共 3363 字,大约阅读时间需要 11 分钟。

题目连接

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

简单的广索题。。

1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 using std::cin; 10 using std::cout; 11 using std::endl; 12 using std::swap; 13 using std::sort; 14 using std::map; 15 using std::pair; 16 using std::queue; 17 using std::vector; 18 using std::multimap; 19 #define pb(e) push_back(e) 20 #define sz(c) (int)(c).size() 21 #define mp(a, b) make_pair(a, b) 22 #define all(c) (c).begin(), (c).end() 23 #define iter(c) decltype((c).begin()) 24 #define cls(arr,val) memset(arr,val,sizeof(arr)) 25 #define cpresent(c, e) (find(all(c), (e)) != (c).end()) 26 #define rep(i, n) for (int i = 0; i < (int)(n); i++) 27 #define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i) 28 const int N = 10010; 29 typedef unsigned long long ull; 30 bool vis[N]; 31 int start, end; 32 struct Node { 33 int v, s; 34 Node(int i = 0, int j = 0) :v(i), s(j) {} 35 }; 36 inline void calc_1(int *arr, int v) { 37 int i, t, j = 0; 38 do arr[j++] = v % 10; while (v /= 10); 39 for (i = 0, --j; i < j; i++, j--) { 40 t = arr[i]; 41 arr[i] = arr[j]; 42 arr[j] = t; 43 } 44 } 45 inline int calc_2(int *arr) { 46 int sum = 0; 47 rep(i, 4) sum = sum * 10 + arr[i]; 48 return sum; 49 } 50 inline void calc_3(queue
&q, int k, int s) { 51 if (!vis[k]) { 52 q.push(Node(k, s + 1)); 53 vis[k] = true; 54 } 55 } 56 int bfs() { 57 int v, k, tmp[10]; 58 cls(vis, false); 59 queue
q; 60 q.push(Node(start, 0)); 61 vis[start] = true; 62 while (!q.empty()) { 63 Node t = q.front(); q.pop(); 64 if (t.v == end) return t.s; 65 calc_1(tmp, t.v); 66 rep(i, 4) { 67 v = tmp[i]; 68 if (v + 1 == 10) tmp[i] = 1; 69 else tmp[i] = v + 1; 70 k = calc_2(tmp); 71 calc_3(q, k, t.s); 72 tmp[i] = v; 73 if (v - 1 == 0) tmp[i] = 9; 74 else tmp[i] = v - 1; 75 k = calc_2(tmp); 76 calc_3(q, k, t.s); 77 tmp[i] = v; 78 } 79 rep(i, 3) { 80 calc_1(tmp, t.v); 81 swap(tmp[i], tmp[i + 1]); 82 k = calc_2(tmp); 83 calc_3(q, k, t.s); 84 } 85 } 86 return 0; 87 } 88 int main() { 89 #ifdef LOCAL 90 freopen("in.txt", "r", stdin); 91 freopen("out.txt", "w+", stdout); 92 #endif 93 int t; 94 scanf("%d", &t); 95 while (t--) { 96 scanf("%d %d", &start, &end); 97 printf("%d\n", bfs()); 98 } 99 return 0;100 }
View Code

转载于:https://www.cnblogs.com/GadyPu/p/4635111.html

你可能感兴趣的文章
【3.1】Cookiecutter安装和使用
查看>>
【2.3】初始Django Shell
查看>>
Linux(Centos)之安装Redis及注意事项
查看>>
VC(VISUAL_C++)虚拟键VK值列表
查看>>
《风笛》-林白
查看>>
Android 网络请求框架Retrofit
查看>>
GeoServer手动发布本地Shapefile地图
查看>>
KMP之我见
查看>>
BZOJ1119[POI2009]SLO && BZOJ1697[Usaco2007 Feb]Cow Sorting牛排序
查看>>
Django组件——分页器和中间件
查看>>
scala 14 trait
查看>>
You need to run build with JDK or have tools.jar问题解决
查看>>
BZOJ 1030: [JSOI2007]文本生成器 [AC自动机 DP]
查看>>
HDU 3949 XOR [高斯消元XOR 线性基]
查看>>
for-each用法误区(不能改变数组元素值)
查看>>
f.select
查看>>
SSH2各部分作用
查看>>
不设置默认网关,导致traceroute无法获取途经路由信息原因
查看>>
MySql优化—删除操作
查看>>
三天打渔两天晒网
查看>>