Open the Lock
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6484 Accepted Submission(s): 2903
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
题意:解开密码锁,从初始状态到开锁状态问最少做几次操作。操作可以是上下转动(上下加减1,特别的:9 + 1 = 1, 1 - 0 = 9 )和交换两个相邻位(最左面的和最右面的不相邻)。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define LL long long
struct node
{
char s[5];
int step;
};
char ch1[5],ch2[5];
int vis[10][10][10][10];
int bfs()
{
queue<node>q;
node cur,nt;
strcpy(cur.s,ch1);
cur.step=0;
vis[ch1[0]-'0'][ch1[1]-'0'][ch1[2]-'0'][ch1[3]-'0']=1;
q.push(cur);
while(!q.empty())
{
cur=q.front();
q.pop();
if(strcmp(cur.s,ch2)==0) return cur.step;
nt.step=cur.step+1;
for(int i=0;i<4;i++)
{
strcpy(nt.s,cur.s);
if(nt.s[i]=='9') nt.s[i]='1';
else nt.s[i]+=1;
if(!vis[nt.s[0]-'0'][nt.s[1]-'0'][nt.s[2]-'0'][nt.s[3]-'0'])
{
vis[nt.s[0]-'0'][nt.s[1]-'0'][nt.s[2]-'0'][nt.s[3]-'0']=1;
q.push(nt);
}
strcpy(nt.s,cur.s);
if(nt.s[i]=='1') nt.s[i]='9';
else nt.s[i]-=1;
if(!vis[nt.s[0]-'0'][nt.s[1]-'0'][nt.s[2]-'0'][nt.s[3]-'0'])
{
vis[nt.s[0]-'0'][nt.s[1]-'0'][nt.s[2]-'0'][nt.s[3]-'0']=1;
q.push(nt);
}
if(i<3)
{
strcpy(nt.s,cur.s);
nt.s[i]=cur.s[i+1];
nt.s[i+1]=cur.s[i];
if(!vis[nt.s[0]-'0'][nt.s[1]-'0'][nt.s[2]-'0'][nt.s[3]-'0'])
{
vis[nt.s[0]-'0'][nt.s[1]-'0'][nt.s[2]-'0'][nt.s[3]-'0']=1;
q.push(nt);
}
}
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%s %s",ch1,ch2);
if(strcmp(ch1,ch2)==0)
{
printf("0\n");
continue;
}
memset(vis,0,sizeof(vis));
printf("%d\n",bfs());
}
return 0;
}