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

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1195

 

Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2587    Accepted Submission(s): 1140

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.
 

 

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
 

 

Author
YE, Kai
 

 

Source
 

 

Recommend
Ignatius.L
 
 
BFS可以水过。。。。PE四次,没看清题,囧......据说双向BFS更快,可惜不会。。。。
 
贴下渣代码,等以后有机会改进下:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 int vis[10000],end; 9 char start[4];10 struct node11 {12 char num[5];13 int time;14 };15 int convert(char s[])16 {17 int i,j,sum=0;18 for(i=0,j=strlen(s)-1;i
q;25 node p,s;26 int i,temp;27 char sh;28 memset(vis,0,sizeof(vis));29 p.time=0;30 strcpy(p.num,start);31 q.push(p);32 while(!q.empty())33 {34 p=q.front();35 q.pop();36 if(convert(p.num)==end)//strcpy(p.num,end)37 {38 printf("%d\n",p.time);39 return ;40 }41 for(i=0;i<4;i++)42 {43 strcpy(s.num,p.num);44 if(s.num[i]<'9'&&s.num[i]>='1')s.num[i]+=1;45 else if(s.num[i]=='9')s.num[i]='1';46 s.num[4]='\0';47 temp=convert(s.num);48 if(!vis[temp])49 {50 vis[temp]=1;51 s.time=p.time+1;52 q.push(s);53 }54 strcpy(s.num,p.num);55 if(s.num[i]<='9'&&s.num[i]>'1')s.num[i]-=1;56 else if(s.num[i]=='1')s.num[i]='9';57 s.num[4]='\0';58 temp=convert(s.num);59 if(!vis[temp])60 {61 vis[temp]=1;62 s.time=p.time+1;63 q.push(s);64 }65 if(i!=3)66 {67 strcpy(s.num,p.num);68 sh=s.num[i];69 s.num[i]=s.num[i+1];70 s.num[i+1]=sh;71 s.num[4]='\0';72 temp=convert(s.num);73 if(!vis[temp])74 {75 vis[temp]=1;76 s.time=p.time+1;77 q.push(s);78 }79 }80 }81 }82 }83 int main()84 {85 int t;86 cin>>t;87 while(t--)88 {89 cin>>start>>end;90 getchar();91 bfs();92 }93 return 0;94 }

 

转载于:https://www.cnblogs.com/lfeng/archive/2013/04/27/3048105.html

你可能感兴趣的文章
javase基础10
查看>>
Qt Font
查看>>
Unity中Awake与Start函数的调用情况总结(转)
查看>>
UILabel设置富文本格式显示
查看>>
[洛谷P3379]【模板】最近公共祖先(LCA)
查看>>
java程序——随机数求和
查看>>
HTML5的浏览器支持方案
查看>>
在Asp.Net MVC中使用Repeater控件
查看>>
应用程序已被安全设置阻止
查看>>
找球号(一)
查看>>
开发小计(3)
查看>>
[Codevs] 1001 舒适的路线
查看>>
Deep Learning相关
查看>>
读书推荐:2017 第二期
查看>>
MySQL 树形结构 根据指定节点 获取其所有父节点序列
查看>>
hdu_5773_The All-purpose Zero(LIS)
查看>>
HDFS的Java客户端编写
查看>>
WinForm打印
查看>>
题目选择 GitHub帐号
查看>>
流程控制之while循环
查看>>