Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4324 Accepted Submission(s): 1896
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
2 1234 2144 1111 9999
Sample Output
2 4
Author
YE, Kai
Source
解题思路:给你一个长度为4位的锁,有3种操作,+1,-1,交换相邻的两位,直接BFS即可
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
using namespace std;
long long depth[1000000],Count,s;
bool visit[10][10][10][10];
typedef struct node
{
char num[5];
node()
{
memset(num,'\0',sizeof(num));
}
};
int BFS(node a,node b)
{
int i,j,k,t=1,n;
visit[a.num[0]-'0'][a.num[1]-'0'][a.num[2]-'0'][a.num[3]-'0']=true;
node x0,str[11];
queue<node> x;
x.push(a);
while(!x.empty())
{
if(Count==depth[t-1])
{
t++;
Count=s=0;
}
x0=x.front();x.pop();
j=0;
for(i=0;i<4;i++)
{
strcpy(str[j].num,x0.num);
str[j].num[4]='\0';
if(x0.num[i]=='9')
str[j].num[i]='1';
else
str[j].num[i]=x0.num[i]+1;
if(strcmp(str[j].num,b.num)==0)
return t;
if(!visit[str[j].num[0]-'0'][str[j].num[1]-'0'][str[j].num[2]-'0'][str[j].num[3]-'0'])
{
visit[str[j].num[0]-'0'][str[j].num[1]-'0'][str[j].num[2]-'0'][str[j].num[3]-'0']=true;
s++;
x.push(str[j++]);
}
strcpy(str[j].num,x0.num);
str[j].num[4]='\0';
if(x0.num[i]=='1')
str[j].num[i]='9';
else
str[j].num[i]=x0.num[i]-1;
if(strcmp(str[j].num,b.num)==0)
return t;
if(!visit[str[j].num[0]-'0'][str[j].num[1]-'0'][str[j].num[2]-'0'][str[j].num[3]-'0'])
{
visit[str[j].num[0]-'0'][str[j].num[1]-'0'][str[j].num[2]-'0'][str[j].num[3]-'0']=true;
s++;
x.push(str[j++]);
}
}
for(k=0;k<3;k++)
{
strcpy(str[j].num,x0.num);
str[j].num[4]='\0';
swap(str[j].num[k],str[j].num[k+1]);
if(strcmp(str[j].num,b.num)==0)
return t;
if(!visit[str[j].num[0]-'0'][str[j].num[1]-'0'][str[j].num[2]-'0'][str[j].num[3]-'0'])
{
visit[str[j].num[0]-'0'][str[j].num[1]-'0'][str[j].num[2]-'0'][str[j].num[3]-'0']=true;
s++;
x.push(str[j++]);
}
}
depth[t]=s;
Count++;
}
return t;
}
int main()
{
int n,ans;
node p,q;
scanf("%d",&n);
getchar();
while(n--)
{
memset(visit,false,sizeof(visit));
scanf("%s%s",&p.num,&q.num);
p.num[4]='\0';q.num[4]='\0';
getchar();
depth[0]=1;
Count=s=0;
ans=BFS(p,q);
printf("%d\n",ans);
}
return 0;
}