Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6627 Accepted Submission(s): 2986
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
Recommend
简单BFS,但是改了很久就是没找到错,最后发现是if(next.num[i]=0)这样只写了一个=
当时就想掐死自己啊!!!
三个方向 两两互换,+1,-1,注意只在横向改变。当数字9+1=10时记为1, 1-1=0时记为9
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
struct point{
int num[4],step;
};
int a[4];
int vis[11][11][11][11];
void bfs(point p){
queue<point>que;
int i;
que.push(p);
point next;
while(!que.empty()){
p=que.front();
que.pop();
if(p.num[0]==a[0]&&p.num[1]==a[1]&&p.num[2]==a[2]&&p.num[3]==a[3]){
printf("%d\n",p.step);
return ;
}
for(i=0;i<3;i++){//交换
next=p;
next.num[i]=p.num[i+1]; next.num[i+1]=p.num[i];
if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]){
vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
next.step++;
que.push(next);
}
}
for(i=0;i<4;i++){//+1
next=p;
next.num[i]++;
if(next.num[i]==10)
next.num[i]=1;
if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]){
vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
next.step++;
que.push(next);
}
}
for(i=0;i<4;i++){//-1
next=p;
next.num[i]--;
if(next.num[i]==0)
next.num[i]=9;
if(!vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]){
vis[next.num[0]][next.num[1]][next.num[2]][next.num[3]]=1;
next.step++;
que.push(next);
}
}
}
}
int main(){
int n;
char a1[4],a2[4];
scanf("%d",&n);
while(n--){
memset(vis,0,sizeof(vis));
point p;
scanf("%s%s",a1,a2);
a[0]=a1[0]-'0'; a[1]=a1[1]-'0';
a[2]=a1[2]-'0'; a[3]=a1[3]-'0';
p.num[0]=a2[0]-'0'; p.num[1]=a2[1]-'0';
p.num[2]=a2[2]-'0'; p.num[3]=a2[3]-'0';
vis[p.num[0]][p.num[1]][p.num[2]][p.num[3]]=1;
p.step=0;
bfs(p);
}
return 0;
}