HDU 1195 Open the Lock
来源:互联网 发布:好看的校园网络电影 编辑:程序博客网 时间:2024/06/05 06:44
Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6627 Accepted Submission(s): 2986
Problem 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.
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.
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
21234214411119999
Sample Output
24
Author
YE, Kai
Source
Zhejiang University Local Contest 2005
Recommend
Ignatius.L | We have carefully selected several similar problems for you: 1175 1072 1026 1180 1240
简单BFS,但是改了很久就是没找到错,最后发现是if(next.num[i]=0)这样只写了一个=
当时就想掐死自己啊!!!
三个方向 两两互换,+1,-1,注意只在横向改变。当数字9+1=10时记为1, 1-1=0时记为9
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>#include<queue>#include<algorithm>using namespace std;struct point{int num[4],step;};int a[4];int vis[11][11][11][11];void bfs(point p){queue<point>que; int i;que.push(p);point next;while(!que.empty()){p=que.front(); que.pop();if(p.num[0]==a[0]&&p.num[1]==a[1]&&p.num[2]==a[2]&&p.num[3]==a[3]){printf("%d\n",p.step);return ;}for(i=0;i<3;i++){//交换 next=p;next.num[i]=p.num[i+1]; next.num[i+1]=p.num[i];if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]){vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;next.step++;que.push(next);}}for(i=0;i<4;i++){//+1 next=p;next.num[i]++;if(next.num[i]==10)next.num[i]=1;if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]){vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;next.step++;que.push(next);}}for(i=0;i<4;i++){//-1 next=p;next.num[i]--;if(next.num[i]==0)next.num[i]=9;if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]){vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;next.step++;que.push(next);}}}}int main(){int n;char a1[4],a2[4];scanf("%d",&n);while(n--){memset(vis,0,sizeof(vis));point p;scanf("%s%s",a1,a2);a[0]=a1[0]-'0'; a[1]=a1[1]-'0';a[2]=a1[2]-'0'; a[3]=a1[3]-'0';p.num[0]=a2[0]-'0'; p.num[1]=a2[1]-'0';p.num[2]=a2[2]-'0'; p.num[3]=a2[3]-'0';vis[p.num[0]][p.num[1]][p.num[2]][p.num[3]]=1;p.step=0;bfs(p);}return 0;}
0 0
- HDU-1195-Open the Lock
- HDU 1195 Open the Lock
- HDU 1195 OPEN THE LOCK
- HDU-Open the Lock-1195
- hdu 1195 Open the Lock
- hdu-1195-Open the Lock
- HDU 1195 Open the Lock
- hdu 1195 Open the Lock
- HDU 1195 Open the Lock
- hdu(1195) Open the Lock
- HDU 1195 Open the Lock
- hdu 1195 Open the Lock
- HDU 1195 Open the Lock
- HDU 1195 Open the Lock
- HDU 1195 Open the Lock
- HDU - 1195 Open the Lock
- hdu 1195 Open the Lock
- HDU 1195 Open the Lock
- Jupyter Notebook 的快捷键
- NYOJ--2--括号配对问题
- POJ
- Mysql索引介绍及常见索引类别(主键索引、唯一索引、普通索引、全文索引、组合索引)的区别
- CSU 1030 素数槽
- HDU 1195 Open the Lock
- 获得训练集和XML通过opencv自带程序opencv_createsamples.exe&opencv_traincascade.exe的问题
- c++输入输出总结
- 集合框架-Collection集合-2
- sprintf()和itoa()的区别
- maven setting.xml配置
- [字典树 最小树形图] Codeforces Gym 100307 NEERC 13 D. Dictionary
- android studio快捷键
- 用JS获取地址栏参数的方法(超级简单)