Open the Lock Time Limit: 2 Seconds Memory Limit: 65536 KB 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
#include<stdio.h> #include<stdlib.h> #include<math.h> typedef struct node{ int data; int floor; }node; typedef struct QNode{ node data; struct QNode *next; }QNode,*QueueP; typedef struct { QueueP font; QueueP rear; }LinkQueue; LinkQueue Q; void InitQueue(LinkQueue &Q){ Q.font=Q.rear=(QueueP )malloc(sizeof(QNode)); Q.font->next=NULL; } void EnQueue(LinkQueue &Q,node e){ QueueP p; p=(QueueP )malloc(sizeof(QNode)); p->data=e; p->next=NULL; Q.rear->next=p; Q.rear=p; } void DeQueue(LinkQueue &Q,node &head){ QueueP p; p=Q.font->next; head=p->data; Q.font->next=p->next; if(Q.rear==p)Q.rear=Q.font;//如果队列中的最后一个元素被删除的时候,队尾指针也会被删除所以要将队头指针赋给队尾,然后再释放p free(p); } int QueueEmpty(LinkQueue Q){ if(Q.font==Q.rear)return 1; else return 0; } #define SIZE 10000 void add(node head,int *key){ node rear; int temp[4],org[4]; for(int i=0;i<4;i++){ org[3-i]=head.data/(int)pow(10.0,i)%10; } for(int i=0;i<4;i++){ if(org[3-i]==9){ temp[3-i]=1; rear.data=0; for(int j=0;j<4;j++){ if(j!=3-i)temp[j]=org[j]; rear.data+=temp[j]*(int)pow(10.0,3-j); } } else rear.data=head.data+(int)pow(10.0,i); rear.floor=head.floor+1; if(!key[rear.data]){ key[rear.data]=1; EnQueue(Q,rear); } } } void minus(node head,int *key){ node rear; int org[4],temp[4]; for(int j=0;j<4;j++) org[3-j]=head.data/(int)pow(10.0,j)%10; for(int i=0;i<4;i++){ if(org[3-i]==1){ temp[3-i]=9; rear.data=0; for(int j=0;j<4;j++){ if(j!=3-i)temp[j]=org[j]; rear.data+=temp[j]*(int)pow(10.0,3-j); } } else rear.data=head.data-(int)pow(10.0,i); rear.floor=head.floor+1; if(!key[rear.data]){ key[rear.data]=1; EnQueue(Q,rear); } } } void exchange(node head,int *key){ node rear; int temp[4],temp1,org[4]; for(int i=0;i<4;i++){ org[3-i]=(head.data/(int)pow(10.0,i))%10; } for(int i=0;i<=2;i++){ rear.data=0; for(int k=0;k<4;k++)temp[k]=org[k]; if(temp[i]!=temp[i+1]){ temp1=temp[i]; temp[i]=temp[i+1]; temp[i+1]=temp1; } for(int j=0;j<4;j++) rear.data+=temp[j]*(int)pow(10.0,3-j); rear.floor=head.floor+1; if(!key[rear.data]){ key[rear.data]=1; EnQueue(Q,rear); } } } int main(){ int T,N,M,i; node head; int key[SIZE]; scanf("%d",&T); while(T--){ InitQueue(Q); for(i=0;i<SIZE;i++) key[i]=0; scanf("%d%d",&N,&M); head.data=N; head.floor=0; key[N]=1; EnQueue(Q,head); while(!QueueEmpty(Q)){ DeQueue(Q,head); if(head.data==M)break; else{ add(head,key); minus(head,key); exchange(head,key); } } printf("%d\n",head.floor); } }