ACM--steps--dyx--4.2.5--Open the lock(多向广搜)
来源:互联网 发布:淘宝手机端怎么做推广 编辑:程序博客网 时间:2024/04/30 19:20
Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 136 Accepted Submission(s): 80Problem 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
#include<iostream>#include<cstring>#include<queue>using namespace std;struct node{ int num[4],step;}fir,fin;//表示开始的锁位置,和最末的锁位置.int vis[11][11][11][11];int s_num,e_num;bool wyx(node now){ //vis数组初始化为0,1表示已经被访问过了. if(vis[now.num[0]][now.num[1]][now.num[2]][now.num[3]]) return true; return false;}int bfs(){ queue<node> que; node now,next; now=fir; now.step=0; vis[now.num[0]][now.num[1]][now.num[2]][now.num[3]]=1;//表示状态已经被访问过. que.push(now); while(!que.empty()) { now=que.front(); que.pop(); if(now.num[0]==fin.num[0]&&now.num[1]==fin.num[1]&&now.num[2]==fin.num[2]&&now.num[3]==fin.num[3]) return now.step; //分为3个模块分别进行广搜 //第一个方向。数字+; for(int i=0;i<4;i++) { next=now; next.num[i]++; //注意,在进行加法的广搜时,加到10的时候,意即到1; if(next.num[i]==10) next.num[i]=1; if(wyx(next)) continue; vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1; next.step++; que.push(next); } //第二个方向,数字-; for(int i=0;i<4;i++) { next=now; next.num[i]--; if(next.num[i]==0) next.num[i]=9; if(wyx(next)) continue; vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1; next.step++; que.push(next); } //第三个方向,交换 for(int i=0;i<3;i++) { next=now; //锁的相邻两个位置可以实现交换. next.num[i]=now.num[i+1]; next.num[i+1]=now.num[i]; if(wyx(next)) continue; vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1; next.step++; que.push(next); } } return 0;}int main(){ char s1[10],s2[10]; int T; cin>>T; while(T--) { cin>>s1>>s2; for(int i=0;i<4;i++) { fir.num[i]=s1[i]-'0'; fin.num[i]=s2[i]-'0'; } memset(vis,0,sizeof(vis)); cout<<bfs()<<endl; } return 0;}
0 0
- ACM--steps--dyx--4.2.5--Open the lock(多向广搜)
- ACM--steps--dyx--5.1.5--Code Lock
- ACM--steps--dyx--1.2.5--The Seven Percent Solution
- HDU 1195 Open the Lock (双向广搜)
- HDU 1195 Open the Lock(广搜)
- HDU 1195 Open the Lock(广搜)
- hdu1195 Open the Lock BFS 广搜
- ACM--steps--dyx--1.3.3--Tian Ji -- The Horse Racing
- ACM--steps--dyx--5.1.4--find the most comfortable road
- ACM--steps--dyx--1.2.1
- ACM--steps--dyx--1.2.2--
- ACM--steps--dyx--1.3.5--开门人和关门人
- hdu 1195 Open the Lock(单向广搜)
- hdu 1195 Open the Lock(bbfs(双向广搜))
- 【BFS/DBFS】Open the Lock【双向广搜】
- ACM--steps--dyx--1.2.4--Buildings
- ACM--steps--dyx--1.2.6--Quicksum
- ACM--steps--dyx--1.2.7--Lowest Bit
- 关于Java中Runnable和Thread的一些使用
- 生成100000以内的素数XML文件的Demo(Jdom写成)
- JQuery中$.ajax()方法参数详解
- hdoj-1214-圆桌会议
- C++文件读写总结
- ACM--steps--dyx--4.2.5--Open the lock(多向广搜)
- sql连接查询问题总结
- 蓝桥杯 顺时针螺旋填入
- day016 Label and Button
- 唇诮私胖仑掠纪部核胖卸蒂胖肚籽
- 2014 UESTC Training for Data Structures—— E - 休生伤杜景死惊开
- 汤艘谛懦痈抖痰晕繁皆钩亮堤鼐有
- Bootstrap panel
- 飞牟帘星收殖星烈掠叵坟关谘斯啃