Open the Lock
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 7
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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
//开锁题,转到9时可以直接到1,1也可以反过来直接到9。相邻之间可以互换。两种代码,一种bfs,深搜看的别人的代码,理解有点难,广搜很简单。判断三个情况全部加入队列就行了
广搜:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
int vist[10][10][10][10],fin[4];
char str[2][4];
struct node
{
int n[4],step;
} ;
int bfs(node q)
{
queue<node>Q;
node st,ed;
int flag,i;
Q.push(q);
while(!Q.empty())
{
st=Q.front();
Q.pop();
flag=1;
for(i=0; i<4; i++)
{
if(st.n[i]!=fin[i])
{
flag=0;
break;
}
}
if(flag)
return st.step;
for(i=0; i<4; i++)
{
ed=st;
if(st.n[i]==9)
ed.n[i]=1;
else
ed.n[i]=st.n[i]+1;
if(!vist[ed.n[0]][ed.n[1]][ed.n[2]][ed.n[3]])
{
vist[ed.n[0]][ed.n[1]][ed.n[2]][ed.n[3]]=1;
ed.step=st.step+1;
Q.push(ed);
}
}
for(i=0; i<4; i++)
{
ed=st;
if(st.n[i]==1)
ed.n[i]=9;
else
ed.n[i]=st.n[i]-1;
if(!vist[ed.n[0]][ed.n[1]][ed.n[2]][ed.n[3]])
{
vist[ed.n[0]][ed.n[1]][ed.n[2]][ed.n[3]]=1;
ed.step=st.step+1;
Q.push(ed);
}
}
for(i=0; i<3; i++)//交换
{
ed=st;
ed.n[i]=st.n[i+1];
ed.n[i+1]=st.n[i];
if(!vist[ed.n[0]][ed.n[1]][ed.n[2]][ed.n[3]])
{
vist[ed.n[0]][ed.n[1]][ed.n[2]][ed.n[3]]=1;
ed.step=st.step+1;
Q.push(ed);
}
}
}
return 0;
}
int main()
{
int i,n;
node q;
scanf("%d",&n);
while(n--)
{
scanf("%s",str[0]);
scanf("%s",str[1]);
for(i=0; i<4; i++)
{
q.n[i]=str[0][i]-'0';
fin[i]=str[1][i]-'0';
}
memset(vist,0,sizeof(vist));
int ans=0;
q.step=0;
ans=bfs(q);
printf("%d\n",ans);
}
}
深搜:
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cstdio>
using namespace std;
int vist[4],maxx,a[4],b[4];
int cmp(int s)
{
int i,step,aa[4]= {s/1000,s/100%10,s/10%10,s%10};
step=0;
for(i=0; i<4; i++)
{
if(abs(aa[i]-b[i])>4)
step+=9-abs(aa[i]-b[i]);
else
step+=abs(aa[i]-b[i]);
}
return step;
}
void dfs(int sum,int s)
{
int min;
if(sum>999)
{
min=cmp(sum)+s;
if(min<maxx)
maxx=min;
return;
}
for(int i=0; i<4; i++)
{
if(!vist[i])
{
vist[i]=1;
dfs(sum*10+a[i],s++);//这个s比较难理解
vist[i]=0;
}
}
}
int main ()
{
int i,j,n,m,t;
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
scanf("%d%d",&n,&m);
for(i=3,j=1; i>=0; i--,j*=10)
{
a[i]=n/j%10;
b[i]=m/j%10;
}
maxx=99999;
memset(vist,0,sizeof(vist));
dfs(0,0);
printf("%d\n",maxx);
}
}
return 0;
}