Open the Lock
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
给出初始密码和最终密码,求最少需要几步使初始密码变为最终密码
对于每种状态的每位数字有三种走法:+1,-1,左右交换
对于每种状态的每位数字有三种走法:+1,-1,左右交换
//单向bfs----31MS
#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
char s1[10],s2[10];
int a1[10],a2[10];
int vis[20][20][20][20],sum;
struct node
{
int step;
int b[10];
};
void bfs()
{
node u;
u.step=0;
vis[a1[0]][a1[1]][a1[2]][a1[3]]=1;
for(int i=0 ; i < 4; i++)
u.b[i]=a1[i];
queue<node> q;
q.push(u);
while( !q.empty() )
{
node v=q.front();
q.pop();
if(v.b[0]==a2[0]&&v.b[1]==a2[1]&&v.b[2]==a2[2]&&v.b[3]==a2[3])
{
printf("%d\n",v.step);
break;
}
for(int i=0 ; i < 4; i++)
{
node g=v;
g.b[i]=v.b[i]+1;
if(v.b[i] == 9)
g.b[i]=1;
if(!vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]])
{
g.step=v.step+1;
vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=1;
q.push(g);
}
g=v;
g.b[i]=v.b[i]-1;
if(v.b[i] == 1)
g.b[i]=9;
if(!vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]])
{
g.step=v.step+1;
vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=1;
q.push(g);
}
g=v;
int tt;
if(i-1>=0)
{
tt=g.b[i-1];
g.b[i-1]=g.b[i];
g.b[i]=tt;
if(!vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]])
{
g.step=v.step+1;
vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=1;
q.push(g);
}
}
g=v;
if(i+1<4)
{
tt=g.b[i+1];
g.b[i+1]=g.b[i];
g.b[i]=tt;
if(!vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]])
{
g.step=v.step+1;
vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=1;
q.push(g);
}
}
}
}
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
sum=0;
memset(vis,0,sizeof(vis));
scanf("%s%s",s1,s2);
for(i=0 ; i < 4 ; i++)
{
a1[i] = s1[i]-'0';
a2[i] = s2[i]-'0';
}
bfs();
}
return 0;
}
起点和终点一起搜,当相遇时将两点的时间加起来
//双向bfs------15MS
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int a1[10],a2[10];
int vis[20][20][20][20];
int bu[20][20][20][20];
struct node
{
int step,b[10];
};
void bfs()
{
int re=0;
node u1,u2;
u1.step=u2.step=0;
for(int i=0 ; i < 4 ; i++)
{
u1.b[i]=a1[i];
u2.b[i]=a2[i];
}
vis[u1.b[0]][u1.b[1]][u1.b[2]][u1.b[3]]=1;
vis[u2.b[0]][u2.b[1]][u2.b[2]][u2.b[3]]=2;
bu[u1.b[0]][u1.b[1]][u1.b[2]][u1.b[3]]=1;
bu[u2.b[0]][u2.b[1]][u2.b[2]][u2.b[3]]=1;
queue<node> q1,q2;
q1.push(u1);
q2.push(u2);
while(!q1.empty() && !q2.empty())
{
//单向搜索
while(q1.front().step==re)
{
node v1=q1.front();
q1.pop();
for(int i=0 ; i < 4; i++)
{
node g=v1;
g.b[i]=v1.b[i]+1;
if(v1.b[i] == 9) g.b[i]=1;
g.step=v1.step+1;
if( !vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] )
{
vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=1;
bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=g.step;
q1.push(g);
}
if( vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] == 2 )
{
int ans=g.step+bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]];
printf("%d\n",ans);
return ;
}
g=v1;
g.b[i]=v1.b[i]-1;
if(v1.b[i] == 1) g.b[i]=9;
g.step=v1.step+1;
if( !vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] )
{
vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=1;
bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=g.step;
q1.push(g);
}
if( vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] == 2 )
{
int ans=g.step+bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]];
printf("%d\n",ans);
return ;
}
g=v1;
if(i<3)
{
int tt;
tt=g.b[i+1];
g.b[i+1]=g.b[i];
g.b[i]=tt;
g.step=v1.step+1;
if( !vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] )
{
vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=1;
bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=g.step;
q1.push(g);
}
if( vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] == 2 )
{
int ans=g.step+bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]];
printf("%d\n",ans);
return ;
}
}
}
}
//双向搜索
while(q2.front().step==re)
{
node v2=q2.front();
q2.pop();
for(int i=0 ; i < 4; i++)
{
node g=v2;
g.b[i]=v2.b[i]+1;
if(v2.b[i] == 9) g.b[i]=1;
g.step=v2.step+1;
if( !vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] )
{
vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=2;
bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=g.step;
q2.push(g);
}
if( vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] == 1 )
{
int ans=g.step+bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]];
printf("%d\n",ans);
return ;
}
g=v2;
g.b[i]=v2.b[i]-1;
if(v2.b[i] == 1) g.b[i]=9;
g.step=v2.step+1;
if( !vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] )
{
vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=2;
bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=g.step;
q2.push(g);
}
if( vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] == 1 )
{
int ans=g.step+bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]];
printf("%d\n",ans);
return ;
}
g=v2;
if(i<3)
{
int tt;
tt=g.b[i+1];
g.b[i+1]=g.b[i];
g.b[i]=tt;
g.step=v2.step+1;
if( !vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] )
{
vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=2;
bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]]=g.step;
q2.push(g);
}
if( vis[g.b[0]][g.b[1]][g.b[2]][g.b[3]] == 1 )
{
int ans=g.step+bu[g.b[0]][g.b[1]][g.b[2]][g.b[3]];
printf("%d\n",ans);
return ;
}
}
}
}
re++;
}
}
int main()
{
int T,i;
char s1[10],s2[10];
scanf("%d",&T);
while(T--)
{
memset(vis,0,sizeof(vis));
memset(bu,0,sizeof(bu));
scanf("%s%s",s1,s2);
for(i=0 ; i < 4; i++)
{
a1[i]=s1[i]-'0';
a2[i]=s2[i]-'0';
}
bfs();
}
return 0;
}