Open the Lock

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2919    Accepted Submission(s): 1276


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
 

Recommend
Ignatius.L


解题思路:该题为广度优先搜索题,咋的一看可能想不到。但确实是求最短路劲的,不过和一般的迷宫搜索的方向不一样。这里的搜索树是根据题意走的,不是上下左右,而是加,减,左换,右换。始终记住广度优先搜索的特点,最短路径,遍历标记。一定要对搜索过得状态进行标记,不然等待的结果将是:超时。望大家以我为前车之鉴。。。。。。



#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; int a[4]; int b[4]; int f[10][10][10][10]; struct node {     int x[4];     int step; }; int bfs() {     int i,j;     node s,e;     queue<node> q;     memset(f,0,sizeof(f));     s.x[0]=a[0];     s.x[1]=a[1];     s.x[2]=a[2];     s.x[3]=a[3];     s.step=0;     q.push(s);     //int n=20;     f[a[0]][a[1]][a[2]][a[3]]=1;     while(!q.empty())     {         s=q.front();         q.pop();         if(s.x[0]==b[0]&&s.x[1]==b[1]&&s.x[2]==b[2]&&s.x[3]==b[3])             return s.step;         //printf("%d%d%d%d\n",s.x[0],s.x[1],s.x[2],s.x[3]);         for(i=0;i<4;i++)         {             //左边交换             if(i!=0)             {                 e.x[0]=s.x[0];                 e.x[1]=s.x[1];                 e.x[2]=s.x[2];                 e.x[3]=s.x[3];                 j=e.x[i-1];                 e.x[i-1]=e.x[i];                 e.x[i]=j;                 e.step=s.step+1;                 if(!f[e.x[0]][e.x[1]][e.x[2]][e.x[3]])                 {                     q.push(e);                     f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]=1;                 }             }             if(e.x[0]==b[0]&&e.x[1]==b[1]&&e.x[2]==b[2]&&e.x[3]==b[3])             return e.step;             //右边交换             if(i!=3)             {                 e.x[0]=s.x[0];                 e.x[1]=s.x[1];                 e.x[2]=s.x[2];                 e.x[3]=s.x[3];                 j=e.x[i];                 e.x[i]=e.x[i+1];                 e.x[i+1]=j;                 e.step=s.step+1;                 if(!f[e.x[0]][e.x[1]][e.x[2]][e.x[3]])                 {                     q.push(e);                     f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]=1;                 }             }             if(e.x[0]==b[0]&&e.x[1]==b[1]&&e.x[2]==b[2]&&e.x[3]==b[3])             return e.step;             //x[i]+1;             //printf""();             e.x[0]=s.x[0];             e.x[1]=s.x[1];             e.x[2]=s.x[2];             e.x[3]=s.x[3];             e.x[i]+=1;             if(e.x[i]==10)                 e.x[i]=1;             e.step=s.step+1;             if(!f[e.x[0]][e.x[1]][e.x[2]][e.x[3]])             {                 q.push(e);                 f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]=1;             }             if(e.x[0]==b[0]&&e.x[1]==b[1]&&e.x[2]==b[2]&&e.x[3]==b[3])             return e.step;             //x[i]-1             e.x[0]=s.x[0];             e.x[1]=s.x[1];             e.x[2]=s.x[2];             e.x[3]=s.x[3];             e.x[i]-=1;             if(e.x[i]==0)                 e.x[i]=9;             e.step=s.step+1;             if(!f[e.x[0]][e.x[1]][e.x[2]][e.x[3]])             {                 q.push(e);                 f[e.x[0]][e.x[1]][e.x[2]][e.x[3]]=1;             }             if(e.x[0]==b[0]&&e.x[1]==b[1]&&e.x[2]==b[2]&&e.x[3]==b[3])             return e.step;         }     } } int main() {     int t;     scanf("%d",&t);     while(t--)     {         scanf("%1d%1d%1d%1d",&a[0],&a[1],&a[2],&a[3]);         scanf("%1d%1d%1d%1d",&b[0],&b[1],&b[2],&b[3]);         //printf("%d %d %d %d\n",a[0],a[1],a[2],a[3]);        // printf("%d %d %d %d\n",b[0],b[1],b[2],b[3]);         printf("%d\n",bfs());     }     return 0; }