Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7113 Accepted Submission(s): 3211
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到9编号。
每次您可以对任何数字加1或减1。当加1到'10'时,数字将变为'1',当减1到'0'时,数字将变为'9'。您也可以与邻居交换数字。每个动作都将采取一步。
现在你的任务是使用最小的步骤来打开锁。注意:最左边的数字不是最右边数字的邻居。
输入
输入文件以整数T开头,表示测试用例的数量。
每个测试用例都以四位数N开头,表示密码锁的初始状态。然后跟着另一个四行的M行,表示可以打开锁的密码。每个测试用例后面都有一行空行。
原本一看最小步骤数,就知道用bfs做的,但是思路没想起来,所以用了dfs,想多开几个dfs,样例都过不了,快150行看的都快吐了,看了看别人的bfs思路,自己又敲了一遍。
//先将字符串转换成int数组好处理一些
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct node{
int num[10];
int temp;
};
int vis[10][10][10][10];//保存四位数的状态
int lock[10];//原来的乱码
char s[10];
char str[10];
int key[10];//密码
void bfs()
{
node s,t;
queue<node>q;
s.temp = 0;
for(int i=0;i<4;i++)
{
s.num[i]=lock[i];
}
vis[s.num[0]][s.num[1]][s.num[2]][s.num[3]]=1;//代表这四位数出现过
q.push(s);
while(!q.empty())
{
t=q.front();
q.pop();
if(t.num[0]==key[0]&&t.num[1]==key[1]&&t.num[2]==key[2]&&t.num[3]==key[3])
{
printf("%d\n",t.temp);
return;
}
for(int i=0;i<4;i++)// +
{
s=t;
s.temp =t.temp +1;
s.num[i]++;
if(s.num[i]==10)//加到10,变为1
s.num[i]=1;
if(!vis[s.num[0]][s.num[1]][s.num[2]][s.num[3]])//看这四位数搜索过没
{
vis[s.num[0]][s.num[1]][s.num[2]][s.num[3]]=1;
q.push(s);
}
else
continue;
}
for(int i=0;i<4;i++)// -
{
s=t;
s.temp =t.temp +1;
s.num[i]--;
if(s.num[i]==0)
s.num[i]=9;
if(!vis[s.num[0]][s.num[1]][s.num[2]][s.num[3]])
{
vis[s.num[0]][s.num[1]][s.num[2]][s.num[3]]=1;
q.push(s);
}
else
continue;
}
for(int i=0;i<3;i++)//交换
{
s=t;
s.temp =t.temp +1;
int ans=s.num[i];
s.num[i]=s.num[i+1];
s.num[i+1]=ans;
if(!vis[s.num[0]][s.num[1]][s.num[2]][s.num[3]])
{
vis[s.num[0]][s.num[1]][s.num[2]][s.num[3]]=1;
q.push(s);
}
}
}
}
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
memset(vis,0,sizeof(vis));
scanf("%s",str);
getchar();
scanf("%s",s);
for(int i=0;i<4;i++)//转换成int数组
{
lock[i]=str[i]-'0';
key[i]=s[i]-'0';
}
bfs();
}
}
顺便贴上我的Wa代码:
每次都分开加减了,样例结果是10、32
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
int inf;
struct node{
char str[10];
};
char key[10];
int vis[10][10][10][10];
void dfs(node a,int k)
{
//printf("%s***********\n",a.str );
if(strcmp(a.str,key)==0)
{
if(k < inf )
{
inf = k ;
return;
}
}
if(a.str[0]>='1'&&a.str[0]<='9'&&!vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0'])
{
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=1;
char c=a.str[0];
if(a.str[0]=='9')
a.str[0]='1';
else a.str[0]+=1;
dfs(a,k+1);
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=0;
a.str[0]=c;
}
if(a.str[1]>='1'&&a.str[1]<='9'&&!vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0'])
{
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=1;
char c=a.str[1];
if(a.str[1]=='9')
a.str[1]='1';
else a.str[1]+=1;
dfs(a,k+1);
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=0;
a.str[1]=c;
}
if(a.str[2]>='1'&&a.str[2]<='9'&&!vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0'])
{
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=1;
char c=a.str[2];
if(a.str[2]=='9')
a.str[2]='1';
else a.str[2]+=1;
dfs(a,k+1);
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=0;
a.str[2]=c;
}
if(a.str[3]>='1'&&a.str[3]<='9'&&!vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0'])
{
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=1;
char c=a.str[3];
if(a.str[3]=='9')
a.str[3]='1';
else a.str[3]+=1;
dfs(a,k+1);
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=0;
a.str[3]=c;
}
if(a.str[0]>='1'&&a.str[0]<='9'&&!vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0'])
{
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=1;
char c=a.str[0];
a.str[0]-=1;
if(a.str[0]=='0')
a.str[0]='9';
dfs(a,k+1);
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=0;
a.str[0]=c;
}
if(a.str[1]>='1'&&a.str[1]<='9'&&!vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0'])
{
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=1;
char c=a.str[1];
a.str[1]-=1;
if(a.str[1]=='0')
a.str[1]='9';
dfs(a,k+1);
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=0;
a.str[1]=c;
}
if(a.str[2]>='1'&&a.str[2]<='9'&&!vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0'])
{
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=1;
char c=a.str[2];
a.str[2]-=1;
if(a.str[2]=='0')
a.str[2]='9';
dfs(a,k+1);
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=0;
a.str[2]=c;
}
if(a.str[3]>='1'&&a.str[3]<='9'&&!vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0'])
{
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=1;
char c=a.str[3];
a.str[3]-=1;
if(a.str[3]=='0')
a.str[3]='9';
//a.temp +=1;
dfs(a,k+1);
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=0;
a.str[3]=c;
}
for(int i=0;i<3;i++)
{
if(!vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0'])
{
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=1;
char c=a.str[i];
a.str[i]=a.str[i+1];
a.str[i+1]=c;
dfs(a,k+1);
c=a.str[i];
a.str[i]=a.str[i+1];
a.str[i+1]=c;
vis[a.str[0]-'0'][a.str[1]-'0'][a.str[2]-'0'][a.str[3]-'0']=0;
}
}
}
int main()
{
int t;
scanf("%d",&t);
getchar();
while(t--)
{
inf=0x3f3f3f3f;
memset(vis,0,sizeof(vis));
char str[10];
scanf("%s",str);
getchar();
scanf("%s",key);
node a;
strcpy(a.str,str);
//printf("%s\n",a.str);
dfs(a,0);
printf("%d\n",inf);
}
return 0;
}