您的位置:首页 > 运维架构

HDU 1195 Open the Lock(双向BFS)

2015-07-14 23:02 399 查看


Open the Lock

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

Total Submission(s): 5108    Accepted Submission(s): 2251


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

思路:双向BFS

分别从开始项和结束项出发BFS遍历,相遇时输出答案并跳出搜索。

代码:

//0MS	1804K
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
int dis[10005];//改变需要的步数
int color[10005];//正向,反向搜索标记
queue<int> que1,que2;
int num[5],num1[5];

void tochar(int x)
{
num[0]=x/1000;
num[1]=x%1000/100;
num[2]=x%100/10;
num[3]=x%10;
}

int toint(int *num)
{
return num[0]*1000+num[1]*100+num[2]*10+num[3];
}

int bfs(int sta,int en)
{
while(!que1.empty()) que1.pop();
while(!que2.empty()) que2.pop();
memset(color,0,sizeof(color));
memset(dis,0,sizeof(dis));
que1.push(sta); que2.push(en);
color[sta] = 1; color[en] = 2;
while(!que1.empty() || !que2.empty())
{
if(!que1.empty())
{
int t = que1.front();
que1.pop();
tochar(t);
for(int i=0;i<4;i++)
{
num1[0]=num[0];
num1[1]=num[1];
num1[2]=num[2];
num1[3]=num[3];
num1[i]=num[i]+1;
if(num1[i]==10)
num1[i]=1;
int ans=toint(num1);
if(color[ans]==0)
{
color[ans]=1;
dis[ans]=dis[t]+1;
que1.push(ans);
}
else if(color[ans]==2)
return (dis[ans]+dis[t]+1);
num1[i]=num[i]-1;
if(num1[i]==0)
num1[i]=9;
ans=toint(num1);
if(color[ans]==0)
{
color[ans]=1;
dis[ans]=dis[t]+1;
que1.push(ans);
}
else if(color[ans]==2)
return (dis[ans]+dis[t]+1);
}
for(int i=0;i<3;i++)
{
num1[0]=num[0];
num1[1]=num[1];
num1[2]=num[2];
num1[3]=num[3];
int k = num1[i];
num1[i] = num1[i+1];
num1[i+1] = k;
int ans = toint(num1);
if(color[ans]==0)
{
color[ans]=1;
dis[ans]=dis[t]+1;
que1.push(ans);
}
else if(color[ans]==2)
return (dis[ans]+dis[t]+1);
}
}
if(!que2.empty())
{
int t = que2.front();
que2.pop();
tochar(t);
for(int i=0;i<4;i++)
{
num1[0]=num[0];
num1[1]=num[1];
num1[2]=num[2];
num1[3]=num[3];
num1[i]=num[i]+1;
if(num1[i]==10)
num1[i]=1;
int ans=toint(num1);
if(color[ans]==0)
{
color[ans]=2;
dis[ans]=dis[t]+1;
que2.push(ans);
}
else if(color[ans]==1)
return (dis[ans]+dis[t]+1);
num1[i]=num[i]-1;
if(num1[i]==0)
num1[i]=9;
ans=toint(num1);
if(color[ans]==0)
{
color[ans]=2;
dis[ans]=dis[t]+1;
que2.push(ans);
}
else if(color[ans]==1)
return (dis[ans]+dis[t]+1);
}
for(int i=0;i<3;i++)
{
num1[0]=num[0];
num1[1]=num[1];
num1[2]=num[2];
num1[3]=num[3];
int k = num1[i];
num1[i] = num1[i+1];
num1[i+1] = k;
int ans = toint(num1);
if(color[ans]==0)
{
color[ans]=2;
dis[ans]=dis[t]+1;
que2.push(ans);
}
else if(color[ans]==1)
return (dis[ans]+dis[t]+1);
}
}
}
}

int main()
{
int t,ss,ee;
cin>>t;
while(t--)
{
cin>>ss>>ee;
if(ss==ee)
{
cout<<0<<endl;
continue;
}
cout<<bfs(ss,ee)<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: